sturcture and malloc

This is a discussion on sturcture and malloc within the C Programming forums, part of the General Programming Boards category; Hello, I have a stucture like this... struct test_score { /*structure definition*/ char subject_name[MAX_STRING]; int score; }; what I want ...

  1. #1
    Unregistered
    Guest

    Unhappy sturcture and malloc

    Hello,
    I have a stucture like this...

    struct test_score { /*structure definition*/
    char subject_name[MAX_STRING];
    int score;
    };

    what I want is a structure array using malloc.
    I came up with a following code, but I don't think this is the right way to do it.
    Could anyone give me some tips about the right code?
    Thank you in advance.

    void input(struct test_score *); /*prototype*/

    int main() {

    struct test_score * temp = (struct test_score *)malloc(sizeof(struct test_score) * NUM_OF_SUBJECT);

    struct test_score test_score[NUM_OF_SUBJECT];

    temp = test_score;

    input(temp);
    ....

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Here's the idea, replace my choice of data type with yours and you're set.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static void input ( int *a )
    {
      int i;
      for ( i = 0; i < 10; i++ )
        a[i] = i;
    }
    
    static void print ( int *a )
    {
      int i;
      for ( i = 0; i < 10; i++ )
        printf ( "%d ", a[i] );
    }
    
    int main ( void )
    {
      int *array = malloc ( 10 * sizeof ( int ) );
      /* Always check that malloc succeeded */
      if ( array != NULL ) {
        /* If so the array is ready for use */
        input ( array );
        print ( array );
        /* Always free your memory */
        free ( array );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> what I want is a structure array using malloc.

    ok

    Code:
    struct test_score
    { 
        char subject_name[MAX_STRING];
        int score; 
    };
    //...
    //...
    
    int main()
    {
        struct test_score* temp = malloc(sizeof(struct test_score) * NUM_OF_SUBJECT);
        struct test_score test[NUM_OF_SUBJECT]; 
        /*temp = test_score;*/
        free(temp);
        return 0;
    }
    the declaration for temp is fine. note that in C, malloc doesn't have to be casted.

    the statement that was commented doesn't make sense though.
    if you're going to have a pointer to struct and make it point to different structs, then you don't need to allocated memory for it.
    what you were trying to do, was to reseat temp (make it point elsewhere), so you were losing the allocated memory.

  4. #4
    Sayeh
    Guest
    I know I sound like a broken record, but whenever you declare a structure like this:

    Code:
    struct test_score
    { 
        char subject_name[MAX_STRING];
        int score; 
    };
    You create extra work for yourself because you have to use the 'struct' keyword excessively, like so:

    Code:
        struct test_score* temp = malloc(sizeof(struct test_score) * NUM_OF_SUBJECT);
        struct test_score test[NUM_OF_SUBJECT];
    ---

    I mean, doing it that was is the way it was done with the 1.0 C standard of years an years ago- clumsy, overly verbose.

    If you tell it it is an actual 'type' of data, it's much simpler-- like this:

    Code:
    typedef struct
       { 
       char   subject_name[MAX_STRING];
       int      score; 
       }test_score;
    See how much simpler and easier this is:

    Code:
        test_score* temp = malloc(sizeof(test_score) * NUM_OF_SUBJECT);
        test_score test[NUM_OF_SUBJECT];

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >typedefs
    The bad thing I see about using too many typedefs, is when you come back to the code later, and all you are confronted with is this:
    >test_score test[NUM_OF_SUBJECT];

    Now, what type of variable is test_score? It could be a struct, but it could also be any other type of variable, for example an int.

    Here are a couple of examples:
    >int test[NUM_OF_SUBJECT];
    >char test[NUM_OF_SUBJECT];

    Now, if you do this:
    >struct test_score test[NUM_OF_SUBJECT];
    you know immediately that you are dealing with a struct.

    It just saves hunting back through the header files to find the definition.

    I'm not saying typedef is bad, just suggesting you use it with care, and give your variables meaningful names.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Sayeh
    Guest
    That's kind of a weak argument, don't you think?

    I mean, it's not like it isn't obvious whenever you're working with the structure because most places it's accessed will look like this:

    "test_score->score", or
    "test_score.score".

    And if you _can't_ tell what it is, that's why you _don't_ scatter your type declarations throughout your code. They belong at the beginning of a source file and in an accompanying header file, if they are used externally to that sourcefile.

    organization organization organization.

    > any other type of variable, for example an int.

    True, but rare or unlikely unless say, your a Pascal programmer required to work in C and just can't stand the way C looks so you retypedef everything in a Pascalese fashion (I've actually seen this done).

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    > That's kind of a weak argument, don't you think?
    I wasn't arguing... just highlighting another aspect that must be considered when designing.... in fact we agree:
    >organization organization organization.
    is where my point was coming from, but I suppose I didn't explain myself too well

    I've seen plenty of code that has been typedef'd too far imho, and it makes it v hard to read/understand/debug.

    >True, but rare or unlikely unless say....
    Again, it's common in the stuff I have to work with. Maybe the original programmers were Pascal people, I don't know! I'm just left looking after their left overs
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've seen plenty of code that has been typedef'd too far imho, and it makes it v hard to read/understand/debug.
    typedef is a wonderful feature that should be used liberally to make code easier to understand. The problem is that most people have trouble creating distinctive and informative names. This causes code to be much more difficult to understand, completely defeating the purpose of typedef.

    Once again we drop back to design. Before you start coding, come up with a list of good typedef names that you will use and structure your code so that everything is easy to locate and follow. Sadly, design is a foreign concept to many programmers.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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