Thread: Dynamic Structures size

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Dynamic Structures size

    Hello,

    I have an application in which Data from xml is populated into structures. The structure is as below:

    Code:
    typedef struct
    	{
    	char Name[2][10];
    	char Address[2][10];
    	char Street[2][10];
    	char City[2][10];
    	char State[2][10];
    	char Country[2][10];
    	}DETAILS;
    However, the problem with this structure is that the Maximum number of variables it can hold is limited by the Array size I define.

    I need to define a structure whose elements have no upper limit on the number of character strings it can hold. In other words, I need to have a dynamic array size.How do I implement it?

    Kindly Help.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Just like you would set aside storage for any variable dynamically.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The easy way is to switch to C++ and use std::vector.

    The hard (C) way is to write a struct (that contains a pointer and 2 sizes) and a few functions that emulate a std::vector with malloc/realloc/free.

    The lazy way is to just make them bigger... be careful not to overflow the stack, though.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    May I suggest also that:
    Code:
    typedef struct
    	{
    	char Name[2][10];
    	char Address[2][10];
    	char Street[2][10];
    	char City[2][10];
    	char State[2][10];
    	char Country[2][10];
    	}DETAILS;
    probably makes more sense as:
    Code:
    typedef struct
    	{
    	char Name[10];
    	char Address[10];
    	char Street[10];
    	char City[10];
    	char State[10];
    	char Country[10];
    	}DETAILS;
    
    DETAILS detailsArray[2];
    What I've done is move the [2] to the declaration of an array variable.

    You can now use dynamic sizing if you feel like there isn't a sensible maximum size you can choose directly. If you choose this way, then you have further choices to make - what size do you start with, and how often and how much do you "grow" the array if/when it needs to grow (you could of course read through the entire file and see how many entries there are, and then allocate the right amount of memory).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Thanks matsp. Your suggestion sounds good. I shall try that out.

    However, I have another question. As the array of structures would hold large data, I would like to free the structures of their memory as soon as the function end is reached. How do I achieve this?

    Kindly help.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    It depends on how you malloc() them. You can allocate the array as contiguous memory if that's needed, or just allocate all willy-nilly like.

    http://c-faq.com/aryptr/dynmuldimary.html

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dunxton View Post
    Thanks matsp. Your suggestion sounds good. I shall try that out.

    However, I have another question. As the array of structures would hold large data, I would like to free the structures of their memory as soon as the function end is reached. How do I achieve this?

    Kindly help.
    Like saeculum says, it sort of depends on what you are doing, but are you really going to save memory doing that? Freeing the memory when you are finished with the data is what you really need if for - if that coincides with the end of a function, that's fine. If you do malloc(), you pass the original value returned by malloc() into free(), and the memory is no available again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    This is what I have tried. Please let me know if this is fine:

    Code:
    typedef struct
    	{
    	char Name[10];
    	char Address[10];
    	char Street[10];
    	char City[10];
    	char State[10];
    	char Country[10];
    	}DETAILS;
    Code:
    int main ()
    	{
    DETAILS *detailsArray = NULL;
    int i = //Find out the maximum number of rows the structure needs to hold.
    detailsArray = (DETAILS *) calloc (i , sizeof(DETAILS));
    	
    for (int j=1 ;j<i ; j++)
    		{
    		strcpy(detailsArray[j].Name , intputaddress[j]);
    		strcpy(detailsArray[j].Address , intputaddress[j]);
                   ....
                   .....
    		}
    
    
    //At the End of the function
    free(detailsArray);
    return 0;
    }
    Also , does the free( ) deallocate ALL members of the structure ( The char arrays . Note that they are char arrays and not char pointers) ?


    Thanks.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    There's a rule of thumb, which states that for each call to malloc/calloc, there should be a corresponding free call.

    It looks fine.

    Also, if you're running Linux, you might like to use valgrind, it should help find any memory leaks.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by saeculum View Post
    There's a rule of thumb, which states that for each call to malloc/calloc, there should be a corresponding free call.
    That is more than a "rule of thumb" - it is a strict rule. Of course, you don't necessarily have to have matched pairs, but for every (successful) call to malloc and friends, there should be a call to free.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Thumbs up

    To reiterate ,

    Also , does the free( ) deallocate ALL members of the structure ( The char arrays . Note that they are char arrays and not char pointers) ?
    Please clarify this one last question. And thanks so much guys for all the help.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, assuming that you used it correctly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also note:
    Code:
    detailsArray = (DETAILS *) calloc (i , sizeof(DETAILS));
    If you HAVE to have the cast there, it probably means that you are compiling your code in a C++ compiler instead of a C compiler [which is OK to an extent sort of]. If you don't have to have it there to make the code compile, then remove it - it avoids problems where you forgot to include the headerfile that provides calloc(), which in turn causes portability problems (e.g. on a 64-bit machine the default int type will be extended to 64 bits when converted to a pointer, and your actual pointer value will only have the lower 32 bits correct. Which undoubtedly will NOT do what you want].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Thanks so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to know dynamic size of struct
    By unikgila in forum C Programming
    Replies: 16
    Last Post: 10-20-2008, 08:56 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. How to get the size of an dynamic array?
    By sept in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2007, 02:26 PM
  4. Size of structures is messed up
    By maxorator in forum C++ Programming
    Replies: 13
    Last Post: 01-03-2007, 03:47 AM
  5. size of dynamic array
    By tommy_gunn in forum C Programming
    Replies: 3
    Last Post: 12-30-2004, 08:01 PM