Thread: Where is the best place to define llarge arrays of structs?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    53

    Where is the best place to define llarge arrays of structs?

    Hello,

    Programmers say it is better to define arrays of structs in a function or in main instead of defining them at the top of a .c file having global scope to the file.

    So here it is ... which way is better or which way would you guys do this...

    Would you guys prefer to do this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    //================================================ Common/h
    typedef struct myX{
    int a;
    } MYX;
    
    //================================================= Main.c
    typedef struct myStruct ms;
    
    struct myStruct{
    	MYX MXScore1[10];
    	MYX MXScore2[10];
    	MYX MXScore3[10];
    };
    
    
    ms* newStruct()
    {
    ms* pTables;
    pTables = (ms*) malloc(sizeof(struct myStruct));
    
    pTables->MXScore1[0].a = 11;
    pTables->MXScore1[1].a = 16;
    pTables->MXScore1[2].a = 17;
    pTables->MXScore1[3].a = 12;
    pTables->MXScore1[4].a = 13;
    pTables->MXScore1[5].a = 131;
    pTables->MXScore1[6].a = 15;
    pTables->MXScore1[7].a = 17;
    pTables->MXScore1[8].a = 19;
    pTables->MXScore1[9].a = 111;
    
    pTables->MXScore2[0].a = 11;
    pTables->MXScore2[1].a = 16;
    pTables->MXScore2[2].a = 17;
    pTables->MXScore2[3].a = 12;
    pTables->MXScore2[4].a = 13;
    pTables->MXScore2[5].a = 131;
    pTables->MXScore2[6].a = 15;
    pTables->MXScore2[7].a = 17;
    pTables->MXScore2[8].a = 19;
    pTables->MXScore2[9].a = 111;
    
    pTables->MXScore3[0].a = 11;
    pTables->MXScore3[1].a = 16;
    pTables->MXScore3[2].a = 17;
    pTables->MXScore3[3].a = 12;
    pTables->MXScore3[4].a = 13;
    pTables->MXScore3[5].a = 131;
    pTables->MXScore3[6].a = 15;
    pTables->MXScore3[7].a = 17;
    pTables->MXScore3[8].a = 19;
    pTables->MXScore3[9].a = 111;
    
    return pTables;
    }
    
    int main(void)
    {
    ms* m;
    int w;
    
    m = newStruct();
    w = m->MXScore1[0].a;
    free(m);
    return 0;
    }

    OR THIS:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    //================================================ Common/h
    typedef struct myX{
    int a;
    } MYX;
    
    //================================================= Main.c
    MYX MXScore1[10] = {11, 16, 17, 12, 13, 131, 15, 17, 19, 111};
    MYX MXScore2[10] = {11, 16, 17, 12, 13, 131, 15, 17, 19, 111};
    MYX MXScore3[10] = {11, 16, 17, 12, 13, 131, 15, 17, 19, 111};
    
    typedef struct myStruct ms;
    
    struct myStruct{
    	MYX* MXScores;
    };
    
    
    ms* newStruct()
    {
    ms* pTables;
    pTables = (ms*) malloc(sizeof(struct myStruct));
    pTables->MXScores = MXScore1;
    pTables->MXScores = MXScore2;
    pTables->MXScores = MXScore3;
    return pTables;
    }
    
    int main(void)
    {
    ms* m;
    int w;
    
    m = newStruct();
    w = m->MXScores[0].a;
    free(m);
    return 0;
    }
    All feedbacks appreciated!

    PS... please bear in mind that there may be 100 or so of these tables and that they may be 50 or even 100 items as opposed to 10!!!!
    r
    Last edited by see the big C; 12-12-2011 at 01:44 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In the first instance, I would delete malloc.h, since it isn't a standard header file. malloc() is declared in stdlib.h, which you're including.

    Second, remove the casts on the return result of malloc - this is C code, not C++ code.

    Third, I'd go for the 2nd option, IF all your arrays can be statically initialised (as per your example).

    Or perhaps the 1st option, and read all the data from a file (and not inline assignments as you've shown).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Hello Salem,

    >In the first instance, I would delete malloc.h, since it isn't a standard header file. malloc() is declared in stdlib.h, which you're including.

    Understood! So while on the subject... why does C supply a malloc.h file if malloc() is already declared in stdlib.h ?

    >Second, remove the casts on the return result of malloc - this is C code, not C++ code.
    As an article says:

    "Return Value:
    On success, a pointer to the memory block allocated by the function.
    The type of this pointer is always void*, which can be casted to the desired type of data pointer in order to be dereferenceable.
    If the function failed to allocate the requested block of memory, a null pointer is returned."

    For the above reason, I have always casted my returns from malloc. I figure it insures proper type casting to the pointer .... no?

    >Third, I'd go for the 2nd option, IF all your arrays can be statically initialised (as per your example).
    Yes that's what I figured too. But I was unsure!


    >Or perhaps the 1st option, and read all the data from a file (and not inline assignments as you've shown).
    aaaahhh! Now there's an angle I never tought about. You are right... what if these values come from a file...uuumm! For now though
    I don't see these values comming from a file... but in the future ... absolutely!!!!! Good point Salem!

    So for now I will go for the second options since it is a lot more compact. Will keep the second option for future file I/O operations.

    Thanks for your insight... very appreciated!

    Regards
    Ross

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Check this link regarding casting the return of malloc.

    FAQ > Casting malloc - Cprogramming.com

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Understood! So while on the subject... why does C supply a malloc.h file if malloc() is already declared in stdlib.h ?
    Standard C does not supply a malloc.h, this is an implementation specific file. Below is a list of the include files required by the standard:
    <assert.h>
    <complex.h>
    <ctype.h>
    <errno.h>
    <fenv.h>
    <float.h>
    <inttypes.h>
    <iso646.h>
    <limits.h>
    <locale.h>
    <math.h>
    <setjmp.h>
    <signal.h>
    <stdarg.h>
    <stdbool.h>
    <stddef.h>
    <stdint.h>
    <stdio.h>
    <stdlib.h>
    <string.h>
    <tgmath.h>
    <time.h>

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    >Check this link regarding casting the return of malloc.
    >FAQ > Casting malloc - Cprogramming.com

    Interesting ... Thanks for the link!

    Thanks guys!

    r

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you place two arrays in one loop?
    By blindchicken11 in forum C Programming
    Replies: 2
    Last Post: 11-13-2011, 09:10 PM
  2. #define and structs
    By surefire in forum C Programming
    Replies: 1
    Last Post: 12-05-2009, 05:53 PM
  3. Replies: 7
    Last Post: 11-12-2008, 11:00 AM
  4. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 10:59 AM
  5. Structs and arrays...
    By Verdagon in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:28 AM