Thread: Allocating memory for array of structures

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    9

    Allocating memory for array of structures

    My program always seem to crash, and I bet its shortage of memory.
    So here are my declarations of my structures:

    Code:
    struct ItemsDataStruct
    {
        char cItemName[32];
        char cItemDesc[128];
        int nItemCount;
    structItemsDataStruct *pNext;
    structItemsDataStruct *pPrev;
    };
    
    
    struct IPDataStruct
    {
        char cIPName[32];
        char cIPDesc[128];
        char cIPLocation[32];
        struct ItemsDataStruct sItemInIP[10];
        struct IPDataStruct *pNext;
        struct IPDataStruct *pPrev;
    };
    
    
    struct InventoryStruct
    {
        struct ItemsDataStruct sInventory;
    };
    
    
    struct GameplayStruct
    {
        char cCharName[32];
        struct InventoryStruct sUserInventory[50];
    };
    
    
    struct MapDataStruct
    {
        char cMapName[32];
        char cMapDesc[128];
        struct IPDataStruct sIPInMap[10];
        struct ItemsDataStruct sMapItems;
        struct MapDataStruct *pNext;
        struct MapDataStruct *pPrev;
    };
    In Main:

    Code:
    struct MapDataStruct OrigMapData[30];
    struct MapDataStruct MapData[30];
    struct IPDataStruct IPData[30];
    struct ItemsDataStruct ItemData[30];
    struct LoginStruct AdminAccount;


    My program keeps crashing, and I bet its because I didn't malloc it. So please show me how to malloc these structures, it will be really helpful to learn from an example.

    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    You don't need to use malloc since you are declaring it as an array. What is the actual error message that you are getting and on what line?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Quote Originally Posted by mnd22 View Post
    You don't need to use malloc since you are declaring it as an array. What is the actual error message that you are getting and on what line?
    I'm using xCode 4 in mac.

    At first my declaration of my struct in main is:
    Code:
    struct MapDataStruct OrigMapData[500];struct MapDataStruct MapData[500];struct IPDataStruct IPData[500];struct ItemsDataStruct ItemData[500];struct LoginStruct
    and I got an error saying:
    Cannot load previous frame
    No Memory to program now, unsafe to call malloc


    So when I changed it to 30
    Code:
    struct MapDataStruct OrigMapData[30];struct MapDataStruct MapData[30];struct IPDataStruct IPData[30];struct ItemsDataStruct ItemData[30];struct LoginStruct
    The problem was solved, so I'm betting my program ran out of memory. O.O

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Since you have limited memory you need to use malloc then. This will allow you to dynamically allocate memory only when you need it @@,

    Here is a sample of how you can malloc your structures.
    Code:
    struct MapDataStruct OrigMapData* = (struct MapDataStruct *) malloc(NbrOfItems * sizeof(struct MapDataStruct));

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Quote Originally Posted by mnd22 View Post
    Since you have limited memory you need to use malloc then. This will allow you to dynamically allocate memory only when you need it @@,

    Here is a sample of how you can malloc your structures.
    Code:
    struct MapDataStruct OrigMapData* = (struct MapDataStruct *) malloc(NbrOfItems * sizeof(struct MapDataStruct));

    I got an error. You sure thats how I can malloc this kind of struct?
    whats NbrOfItems? Its my [30] array right?
    The error is: "Redefinition of 'OrigMapData' with a different type."

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by HardOverdrive View Post
    I'm using xCode 4 in mac.
    and I got an error saying:
    Cannot load previous frame
    No Memory to program now, unsafe to call malloc

    So when I changed it to 30
    The problem was solved, so I'm betting my program ran out of memory. O.O


    Do a little math on the size of your structs and the resulting sizes of your arrays... Add up the sizes of your variables... for example your ipdatastruct is at least 210 bytes... 500 of them will occupy at least 105k of memory ... add in all your other arrays and it's very likely you've overflowed your program's stack... It happens rather often actually.

    The answer is to use malloc...
    Code:
    struct data *array = malloc(number * sizeof(data));
    This will allocate your arrays on the program's heap, placing only pointers on the stack.... so a 105k array can be replaced by an 8 byte pointer.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    I am sure that's how you use malloc. Based on the error I think you declared OrigMapData twice. Yes NbrOfItems is the number of elements you want your dynamic array to have.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mnd22 View Post
    I am sure that's how you use malloc. Based on the error I think you declared OrigMapData twice. Yes NbrOfItems is the number of elements you want your dynamic array to have.
    You should not (have to) typecast the return of malloc in C. It returns a void pointer that can be assigned to anything.
    If your compiler complains about it that's because you're compiling in C++ mode...

    (Which may mean you've never actually written or compiled a true C program)
    Last edited by CommonTater; 12-05-2011 at 12:14 PM.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Sorry ut do you declare it in main like this?
    Code:
    struct MapDataStruct OrigMapData[30];
    struct MapDataStruct *OrigMapData = malloc(30 * sizeof(MapDataStruct));
    Cuz I still get an error: "Redefinition of 'OrigMapData' with a different type".

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lose the first one, keep the second one...

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by CommonTater View Post
    You should not (have to) typecast the return of malloc in C. It returns a void pointer that can be assigned to anything.
    If your compiler complains about it that's because you're compiling in C++ mode...

    (Which may mean you've never actually written or compiled a true C program)
    I know you don't need to typecast. Most of the C code that I have read have typecast. That's why I am doing it for best practice. If you don't want to typecast that's fine it will still compile and it's still a valid code.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read this: Cprogramming.com FAQ > Casting malloc.

    Casting it is not "for best practice". Most likely, you have old examples. In the old days, C did not have void *, so malloc returned char *, which needed to be cast. Now, things are different, so current best practice is generally not to cast malloc. Actually, casting of anything, not just malloc, should generally be an "only if you really, really need it" thing. Casting is like saying you're smarter than the compiler, since you're basically hiding errors or warnings. That is rarely the case.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Got it. Thanks guys, happy programming, and merry xmas!

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Just one last question, my struct is only limited to 30.
    Is there any way to make it infinite?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mnd22 View Post
    I know you don't need to typecast. Most of the C code that I have read have typecast. That's why I am doing it for best practice. If you don't want to typecast that's fine it will still compile and it's still a valid code.
    That's what I am telling you... it is NOT best practice.
    In fact, it's a source of error since an incorrect typecast can cause program failures.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a problem allocating space for an array of structures
    By Jesse20ghet in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2011, 05:02 AM
  2. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  3. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  4. Allocating memory of VL structures
    By darsatdusk in forum C Programming
    Replies: 8
    Last Post: 07-09-2008, 03:43 PM
  5. allocating memory for char* array
    By creeping_death in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2003, 04:49 AM