Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocate memory for a struct with a character pointer in C

I was struggling to fix a code today, then I come across something similar to:

typedef struct {
int a; 
int b; 
int c;
int d;
char* word;
} mystruct;

int main(int argc, char **argv){

    mystruct* structptr = malloc(sizeof(mystruct));
    if (structptr==NULL) {
        printf("ERROR!")
        ...
    }
    ...
    free(structptr);

    return 0;
}

the code was giving lots of memory errors due to the fact, that char* word is a string of variable length, and malloc was not allocating enough memory for it. In fact it was only allocating 20 Bytes for the whole struct. Is there a way around this issue, without turning the char* into sth like char word[50]?

like image 605
H_squared Avatar asked Aug 15 '13 07:08

H_squared


People also ask

How do you allocate memory to a pointer in a struct?

To allocate the memory for n number of struct person , we used, ptr = (struct person*) malloc(n * sizeof(struct person)); Then, we used the ptr pointer to access elements of person .

Will memory be allocated for a structure if declare a structure pointer?

However, the declaration for a pointer to structure is only allocates memory for pointer NOT for structure. So accessing the member of structure we must allocate memory for structure using malloc() function and we could also give the address of the already declared structure variable to structure pointer.

How do you allocate memory to the array of char pointers?

In C, the RAM should use the following code to allocate the two-dimensional array: *contents = (char**) malloc(sizeof(char*) * numRows); **contents = (char*) malloc(sizeof(char) * numColumns * numRows); for(i = 0; i < numRows; i++) (*contents)[i] = ( (**contents) + (i * numColumns) );

Does pointer allocate memory?

Pointers (along with memory allocation) allow you to create programs that can use as much memory as they need (and no more). A pointer is a reference to some other piece of data.


3 Answers

You are allocating only memory for the structure itself. This includes the pointer to char, which is only 4 bytes on 32bit system, because it is part of the structure. It does NOT include memory for an unknown length of string, so if you want to have a string, you must manually allocate memory for that as well. If you are just copying a string, you can use strdup() which allocates and copies the string. You must still free the memory yourself though.

 mystruct* structptr = malloc(sizeof(mystruct));
 structptr->word = malloc(mystringlength+1);

 ....

 free(structptr->word);
 free(structptr);

If you don't want to allocate memory for the string yourself, your only choice is to declare a fixed length array in your struct. Then it will be part of the structure, and sizeof(mystruct) will include it. If this is applicable or not, depends on your design though.

like image 152
Devolus Avatar answered Oct 18 '22 17:10

Devolus


Add a second malloc for whatever length (N) you need for word

   mystruct* structptr = malloc(sizeof(mystruct));

   structptr->word = malloc(sizeof(char) * N);
like image 3
JackCColeman Avatar answered Oct 18 '22 17:10

JackCColeman


as you can read here you need to allocate the char * separately :

mystruct* structptr = malloc(sizeof(mystruct));
structptr->word = malloc(sizeof(WhatSizeYouWant));
like image 3
No Idea For Name Avatar answered Oct 18 '22 18:10

No Idea For Name