Thread: Memory allocation for an array in a struct pointer

  1. #1
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57

    Memory allocation for an array in a struct pointer

    Hello there,
    this might be a silly question but I am a bit confised about the case and I wanted to ask around. I need to allocate memory for an object that looks like this:

    Code:
    typedef struct{
    	uint32_t		len;
    	uint8_t* 		data;
    } cdcRaw_t;
    It contains pointer to data, with should be also alocated with length of value len.

    So my aproach looks like this (test program)
    Code:
    #include <stdio.h>#include <stdint.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct
    {
    	uint32_t		len;
    	uint8_t* 		data;
    } cdcRaw_t;
    
    
    int main(void)
    {
    	uint8_t data[] = "abcdefghijklmn";
    	uint32_t len = 14;
    
    
    	// first allocate memory for the struct
    	cdcRaw_t* item = malloc(sizeof(cdcRaw_t));
    	
    	item->len = len;
    	
    	// then allocate memory for data within struct
    	item->data = malloc(sizeof(item->len));
    
    
    	// copy the data
    	memcpy(item->data, data, item->len);
    
    
    	int i;
    	for (i = 0; i < item->len; i++)
    	{
    		printf("item %c at index %i \n", item->data[i], i);
    	}
    
    
    	// double free
    	free(item->data);
    	free(item);
    
    
    	return 0;
    }
    :

    In this aproach I have to malloc twice and free twice... Is there a more efficient way to allocate and free memory for this struct? Important note- for each struct object, the length of data varies.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Apart from using

    Code:
    uint32_t len = 14;
    You might consider
    Code:
    uint32_t len = sizeof(data) / sizeof(data[0]);

    But apart from that your general approach looks correct (ignoring that you should be making sure that malloc() does not return NULL before using the pointers).

  3. #3
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    uint32_t len = sizeof(data) / sizeof(data[0]);
    But does the compiller know the size of allocated data like that? If yes, then I dont need to store length in variable len.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    If the declaration of the array is in scope (like it is in the example code), then yes it does.

    Edit: It's a bit more nuanced than that but if the array is not a "variable length array" (VLA), and it's "complete" by either initialising the elements (which you've done), or given the array a specific size (e.g. uint8_t data[15]) then it's fine. I'll leave you to work out why I chose 15 instead of 14 for your example. Related to that, you've actually set len to an incorrect value... your array (data[]) has 15 elements in it and not 14. The fact that in this example the bug is benign is not relevant.
    Last edited by Hodor; 11-27-2015 at 03:51 AM.

  5. #5
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    I see. Eventually it wont be so len variable has to stay. So is there no other way to allocate memory for the scruct (and the containing vector) using only 1 malloc?

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by bremenpl View Post
    I see. Eventually it wont be so len variable has to stay. So is there no other way to allocate memory for the scruct (and the containing vector) using only 1 malloc?
    Using only one malloc and keeping the struct the same? No. You need both calls to malloc(). You'd normally encapsulate this into two functions... e.g. allocate_struct() and free_struct() ... or something like that, passing a pointer to the struct to the functions (and how much memory you want to allocate for .data of course, for your init function)
    Last edited by Hodor; 11-27-2015 at 03:56 AM. Reason: made something clearer

  7. #7
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    Ok, i understand everything. Thank you for help .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-04-2010, 11:43 PM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  3. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  4. Struct/pointer/memory allocation
    By markusvinsa in forum C Programming
    Replies: 16
    Last Post: 06-10-2005, 09:45 AM
  5. struct dynamic memory allocation
    By rotis23 in forum C Programming
    Replies: 2
    Last Post: 12-09-2002, 04:31 AM

Tags for this Thread