1
#include <stdio.h>
#include <malloc.h>
#include <conio.h>

typedef struct node
{
    int info;
    struct node* next;
} node;

node* fi = NULL, *la = NULL, *ptr;

void insertfi()
{
    ptr = (node*)malloc(sizeof(node));
    printf("\nEnter info\n");
    scanf("%d", &ptr->info);
    ptr->next = fi;
    if (fi == NULL && la == NULL)
    {
        fi = la = ptr;
    }
    else
    {
        fi = ptr;
    }
}

void print()
{
    ptr = fi;
    while (ptr != NULL)
    {
        printf("|%d|-->", ptr->info);
        ptr = ptr->next;
    }
}

int main()
{
    int i, ch;
    do
    {
        printf("\nEnter your choice \n1.insert node\n2.view node\n3.exit\n");
        scanf("%d", &ch);
        if (ch == 1)
        {
            insertfi();
        }
        else if (ch == 2)
        {
            print();
        }
        else
            printf("Exiting\n");
    } while (ch != 3);
    return 0;
}

I want to save space in a program that I am working on. This program creates nodes and joins them by storing address of the node in the next pointer in the structure. I want some way to not use the *next pointer to go to the next node - if I do this I will be able to save 2 bytes of memory.

I have been thinking if there is a way of contiguous allocation of address in dynamic memory allocation, so that I will store the address of the first node in a pointer then traverse or do whatever with it after tinkering with the address.

And how do addresses work - I mean how can I access them by inputting an address in the pointer and then printing it?

Here is a sample program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main ()
{
    int a[10],i,*ptr;
    for(i=0;i<10;i++)
    {
        a[i]=rand()%15;
    }
    for(i=0;i<10;i++)
    {
        printf("%6x\n",&a[i]);
    }
    ptr=&a[0];
    printf("\n\n\n%d\n",*ptr);

}

It prints addresses in a contiguous manner but I don't know how to allocate them manually, like ptr=0022ff04 and then using `printf("%d",*ptr);


#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<stdbool.h>

#define max 20

typedef struct node
{
    int info;
    int row;
    int colum;
    struct node *next;
}node;

node *ptr,*fi=NULL,*la=NULL;

int r,c,i,j,sparse[max][max],decompmatx[max][max];

void makenode(int getrow,int getcolum,int getinfo)

{
ptr=(node*)malloc(sizeof(node));
ptr->next=NULL;
if(fi==NULL)
{
    ptr->info=getinfo;
    ptr->row=getrow;
    ptr->colum=getcolum;
    fi=la=ptr;  
}
else
{
    la->next=ptr;
    la=ptr;
    ptr->info=getinfo;
    ptr->row=getrow;
    ptr->colum=getcolum;
}

}

void decompress()

{
int temp;
temp=0;
ptr=fi;
while(ptr!=NULL)
{
    decompmatx[ptr->row][ptr->colum]=ptr->info;
    ptr=ptr->next;
}
for(i=0;i<=r-1;i++)
    {
        for (j=0;j<=c-1;j++)
        {
            if(decompmatx[i][j]>0)
            {

            }
            else
            decompmatx[i][j]=0;
        }
    }
printf("\nValue decompressed\n");
for(i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    printf("%d  ",decompmatx[i][j]);
    if((temp%c)==0)
    printf("\n");
    else
    temp++;

}

}

void showsparse()

{
int temp;
temp=0;
for(i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    printf("%d  ",sparse[i][j]);
    if((temp%c)==0)
    printf("\n");
    else
    temp++;

}

}

void print()

{
ptr=fi;
while(ptr!=NULL)
{
    printf("|%d|%d||%d|--->",ptr->info,ptr->row,ptr->colum);
    ptr=ptr->next;
}   


}

int main()

{
int ch;
do
{
printf("\nInput choice\n1.Make Sparse Matrix\n2.compress Matrix\n3.Decompress 
Matrix\n4.Exit...");
scanf("%d",&ch);
if(ch==1)
{
    printf("Input the rows\n");
    scanf("%d",&r);
    printf("Enter colum\n");
    scanf("%d",&c);
    printf("Printing the %dx%d matrix\n",r,c);
    for(i=0;i<=r-1;i++)
    {
        for (j=0;j<=c-1;j++)
        {
            sparse[i][j]=rand()%2;
        }
    }
    showsparse();
    printf("\nTotal Byte comsuming is =%d\n",r*c*2);
}
else if(ch==2)
{
    for(i=0;i<=r-1;i++)
    {
        for (j=0;j<=c-1;j++)
        {
            if(sparse[i][j]!=0)
            {
                makenode(i,j,sparse[i][j]);
            }

        }
    }
    printf("Data compressed\n");
    print();
    printf("size of node =%d",sizeof(node));

}
else if(ch==3)
{
    decompress();   
}

else
{
    printf("\nExiting..............\n");
}
}while(ch!=4);

getch();


}
4

1 回答 1

0

以下内容已由 jim mcnamara 和 japreiss 在评论中提出。

使用预分配对象池代替动态分配。例如:

node my_pool = malloc(10000 * sizeof(pool));

然后用数组索引替换指针:

typedef struct node
{
    int info;
    int next_index;
} node;

因此,您现在可以使用数组索引,而不是使用指针。为什么这样好?

  • 您可以确定您的对象分配在连续的内存中(这提高了缓存局部性并最终提高了性能)
  • 您可以减小对象的大小,例如uint16_t用于索引(如果您使用的是 64 位平台,即使 uint32_t 也会有所改进)
  • 如果malloc通过存储管理/调试数据浪费内存,您将对其进行改进
  • 在数组中分配数据比使用更快malloc
  • 如果您不关心何时释放,您可以一次释放所有对象:free(my_pool)

当然,没有什么是无条件的好。使用池有缺点(例如,它浪费了可用于其他目的的内存)。

于 2014-08-06T15:26:18.073 回答