dynamic memory

This is a discussion on dynamic memory within the C Programming forums, part of the General Programming Boards category; how do i do dynamic memory allocation for pointer i mean this would work fine, now i can access arr[] ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    dynamic memory

    how do i do dynamic memory allocation for pointer

    i mean this would work fine, now i can access arr[]
    Code:
    int *arr;
    arr=realloc(..);
    but how do i do it for this
    Code:
    struct *children;
    i need to access children like this T->children[i]

    Can any 1 help ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you have a struct definition:
    Code:
    struct mystruct {
          char *ptr;
          int array[10];
    };
    You can malloc a pointer to such a struct this way:
    Code:
    struct mystruct *example = malloc(sizeof(struct mystruct));
    HOWEVER, note that this creates space for a char POINTER and an array of ints. It does not attach anything to the char pointer; you must do that seperately:
    Code:
    example->ptr = malloc(64);
    You must also free the internel pointers before you free() the struct pointer itself.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 03:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

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