Sasha Valentine
Sasha Valentine

Reputation: 39

What is the best way to memory allocate

I am trying to allocate memory in this situation, actually i did what i wanted but im wondering is there any better way or is there something i did wrong .

I just trying to code functional as much as possible what else can i do for this .

void createDATABASE(int uint_val,int int_val,int str_val,int dbl_val)
{
    int *decision=(int*)malloc(4*sizeof(int));
    int i;
    dbStore tdbStore;
    t_CreateDatabase tCreateDatabase;
    signal(SIGINT,err_memleak); // SIGNAL PROCESSING

    memcpy(tCreateDatabase.filename,"test.bin",32); // database name copied

    decision[0] = uint_val; decision[1] = int_val;
    decision[2] = str_val; decision[3] = dbl_val;
    for(i=0;i<4;i++){
        if(decision[i] > 0){
            switch(i){
                case 0:
                    tdbStore.unsig_int = u_intAllocate(uint_val);
                    if(tdbStore.unsig_int==NULL) raise(SIGINT);
                    break;
                case 1:
                    tdbStore.sig_int = n_intAllocate(int_val);
                    if(tdbStore.sig_int==NULL) raise(SIGINT);
                    break;
                case 2:
                    tdbStore.strings = strAllocate(str_val);
                    if(tdbStore.strings==NULL) raise(SIGINT);
                    break;
                case 3:
                    tdbStore.big_int = d_intAllocate(dbl_val);
                    if(tdbStore.big_int==NULL) raise(SIGINT);
                    break;
            }
        }
    }


}

char **strAllocate(int val)
{
    int column=1024,l;
    char **node = (char**)malloc(val*sizeof(char*));
    for(l=0;l<val;l++)
        node[l] = (char*)malloc(column*sizeof(char));
    return node;
}
double *d_intAllocate(int val)
{
    double *node = (double*)malloc(val*sizeof(double));
    return node;
}
unsigned int *u_intAllocate(int val)
{
    unsigned int *node = (unsigned int*)malloc(val*sizeof(unsigned int));
    return node;
}
int *n_intAllocate(int val)
{
    int *node = (int*)malloc(val*sizeof(int));
    return node;
}

Thanks for advises..

Upvotes: 1

Views: 111

Answers (1)

R Sahu
R Sahu

Reputation: 206567

Suggestion 1

You can easily replace

int *decision=(int*)malloc(4*sizeof(int));

by

int decision[4];

When the size of an array is fixed, it makes no sense to use malloc.

Suggestion 2

Do not use explicit cast on the value returned from malloc.

Use

char **node = malloc(val*sizeof(char*));

instead of

char **node = (char**)malloc(val*sizeof(char*));

Using explicit cast on the returned value of malloc are known to be problematic. Checkout Do I cast the result of malloc? for details.

Suggestion 3

You can reduce the overhead of the string allocations by calling malloc only twice.

char **strAllocate(int val)
{
    int column=1024,l;
    char **node = malloc(val*sizeof(char*));
    char* data = malloc(val*column); // No need for sizeof(char). It is always 1.
    for(l=0;l<val;l++)
        node[l] = data + i*column;
    return node;
}

If you use this, you have to remember to call free only twice when the time comes to deallocate the memory.

free(*node);
free(node);

Upvotes: 1

Related Questions