Thread: dynamic memory

  1. #1
    Registered User
    Join Date
    Jan 2009

    dynamic memory

    how do i do dynamic memory allocation for pointer

    i mean this would work fine, now i can access arr[]
    int *arr;
    but how do i do it for this
    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
    segmentation fault
    If you have a struct definition:
    struct mystruct {
          char *ptr;
          int array[10];
    You can malloc a pointer to such a struct this way:
    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:
    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, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM