1

I have a static array and in a function I create a new struct in a loop and assign it to each index in an array. In the function I can see the values but in a different function I see junk for the array values. Do I have to use malloc for something like this?

struct file_types
{
    char * typename;
    char * MIMEtype;
};

static struct file_types *file_type_table; //Table of parameters
static int file_type_table_num=0;

int add_to_filetype_table(char *param, int param_len, char *value, int val_len, char* value2)
{   if ((param == NULL) || (value==NULL) || (value2 == NULL))
        return 0;
    if ((strcmp(param,"type") != 0) || (strcmp(value,"") == 0) || (strcmp(value2,"") == 0))
        return 0;

    if (file_type_table==NULL)
        file_type_table =   emalloc(sizeof(struct file_types));
    else
        file_type_table =  erealloc(file_type_table, (file_type_table_num*sizeof(struct file_types)+ sizeof(struct file_types)));

    file_type_table_num += 1;
    int index = file_type_table_num -1;

    struct file_types new_struct;
    new_struct.typename = value;
    new_struct.MIMEtype = value2;

    file_type_table[index] = new_struct;

    return 1;
}

Problem is in accessing the structs here:

char* get_table_value(char * key)
{   logg("In get_table_value");
    int i;

    char* value;

    for (i=0;i<file_type_table_num;i++)
    {   
        if (strcmp(((file_type_table)[i]).typename, key) == 0)
        {   
            return (file_type_table[i]).MIMEtype;
        }
    }
    return value;
}

1 Answer 1

2

There are two problems in your code:

Problem 1:

The structure new_struct itself is located on the stack and it gets deallocated once the scope of the function ends, So what your array elements point to beyond the scope of the function is something that doesn't exist, aka garbage.

Solution:
The structure needs to reside on the heap memory to be accessed beyond the scope.


Problem 2:

new_struct.typename = value;
new_struct.MIMEtype = value2;

Creates a shallow copy of the pointers being passed to the function add_to_filetype_table(), From the example it is not clear who owns the pointers being passed to the function & what is their lifetime, If these pointers are deallocated before you call get_table_value() then your global static structure is left with dangling pointers and hence you would get garbage values when you output them.

Solution:

You need to make a Deep copy of the pointers being passed.
Allocate memory to the structure members and then copy(strcpy()) the strings in to allocated memory.

Sign up to request clarification or add additional context in comments.

2 Comments

I malloced the new_struct (struct file_types *new_struct = emalloc(sizeof(struct file_types));) but I get segmentation error when I do strcpy(new_struct->typename,value). If I only do "=" then I still have the original problem.
@user994165: You need to malloc the structure pointer members(typename & MIMEtype) too! If you just allocate memory to structure pointer and not the pointer members then the structure members don't have enough memory to hold the strings you copy using strcpy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.