struct dynamic memory allocation

This is a discussion on struct dynamic memory allocation within the C Programming forums, part of the General Programming Boards category; happy monday all. please can you verify this life cycle of a struct: Code: struct test { char *test_string; }; ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    struct dynamic memory allocation

    happy monday all.

    please can you verify this life cycle of a struct:

    Code:
    struct test
    {
        char *test_string;
    };
    
    typedef struct test my_test;
    ...
    my_test *the_test;
    the_test = malloc(sizeof(my_test));
    
    the_test->test_string = malloc(sizeof(char) * 15);
    strcpy(the_test->test_string,"this is a test");
    ...
    free(the_test->test_string);
    free(the_test);
    Last edited by rotis23; 12-09-2002 at 04:32 AM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    Thumbs up

    Looks good. You are missing a semi-colon though.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    yeah, that was just a test for you guys.... really!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  2. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 10:34 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 06:04 PM
  5. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 01:19 PM

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