To malloc or not realloc on struct members

This is a discussion on To malloc or not realloc on struct members within the C Programming forums, part of the General Programming Boards category; I got a question about sizing a struct member with malloc or realloc. Code: stuct tst{ int iIndex; char *szMember; ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    To malloc or not realloc on struct members

    I got a question about sizing a struct member with malloc or realloc.

    Code:
    stuct tst{
      int iIndex;
      char *szMember;
    } *p_tststructs;
    
    int iCounter = 0;
    
    p_tststructs = malloc(sizeof(struct tst) * (iCounter +1));
    
    // how do I size szMember here?
    
    p_tststructs[iCounter].szMember = malloc(sizeof(char) * 10);
    
    // or do I need to use realloc on szMember??
    
    iCounter++;
    
    p_tststructs = realloc(p_tststructs,sizeof(struct tst) * (iCounter+1));
    
    // how do I size szMember here?
    
    p_tststructs[iCounter].szMember = malloc(sizeof(char) * 10);
    
    // or do I need to use realloc on szMember??
    I left out free() and memory allocation error checks for readability. I couldn't find good tutorials about allocation of struct members so if someone got a good link then it's very welcome.

    Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    You need to use array of pointers or pointer-to-pointer to your struct, if you want to create multiple pointers to a give struct

    i.e.

    Code:
    stuct tst{
      int iIndex;
      char *szMember;
    } **p_tststructs;             /* pointer to pointer for dynamic growth of array using malloc and realloc*/
    or

    Code:
    stuct tst{
      int iIndex;
      char *szMember;
    } *p_tststructs[10];             /* array of "n" pointers for defined growth */
    If you want to use pointer to pointer, try this post

    Need help with pointer to pointer

    Thanks,

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Is static allocation of the struct with:

    Code:
    *p_tststructs[10];
    different then dynamic allocation with malloc/realloc the way I use it then ?

    Code:
    p_tststructs = malloc(sizeof(struct tst) * (iCounter +1));
    I will look into pointer to pointer posting thanks!

    Any tips on the szMember malloc/realloc ?

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    // how do I size szMember here?

    p_tststructs[iCounter].szMember = malloc(sizeof(char) * 10);

    // or do I need to use realloc on szMember??
    malloc is correct. You can use realloc if you want as long as you set szMember to NULL first, however that will have the same effect as malloc so it's unnecessary.
    p_tststructs = realloc(p_tststructs,sizeof(struct tst) * (iCounter+1));
    realloc can fail, and in that case it returns NULL but doesn't free the previously allocated memory so you've got yourself a memory leak there if that would happen.
    // how do I size szMember here?

    p_tststructs[iCounter].szMember = malloc(sizeof(char) * 10);

    // or do I need to use realloc on szMember??
    Use realloc to resize the memory.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nkhambal
    You need to use array of pointers or pointer-to-pointer to your struct, if you want to create multiple pointers to a give struct
    Well that's debatable, and incidently, I've been meaning to for some time now. I see it often stated that the above is the case, but it's really not true.

    You don't use pointers-to-pointers when you want multiple characters. And, in fact, you can resize that. So why should it be any different with structures?
    Code:
    struct foo *ptr;
    ptr = malloc( sizeof *ptr );
    The above will allocate one structure instance and point to it.
    Code:
    char *ptr = malloc ( 10 * sizeof *ptr );
    This will allocate 10 characters. Now usually everyone leaves off the "sizeof *ptr" part when allocating characters, because the size of a character is always 1. However, I'm including it as an illustration of how the two allocations are identical.
    Code:
    struct foo *ptr = malloc( 10 * sizeof *ptr );
    Will allocate 10 structure instances, and point to them. (All of these assume malloc doesn't fail.)

    You can walk through a list of pointers with a pointer to that type, simply by doing:
    Code:
    walk = ptr;
    walk++;
    This holds true for characters as well as any other data type where you're allocating in said fashion.

    There are only two reasons you need pointers to pointers: First, you're trying to update what a pointer you pass to a function points at. That is, pass the address of a pointer to a function so you can make it point to something new. Second, to pretend you've got a multiple dimension array.

    There is no need for either of this in this example. A simple realloc will suffice here.
    Code:
    struct foo *tmp = realloc( ptr, newsize * sizeof *ptr );
    [edit]
    Curses, foiled again. That's it, no more proof reading for me!
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 12:10 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 12:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21