Thread: Memory allocation at runtime?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Smile Memory allocation at runtime?

    I wonder if someoen can help me in the right direction with this one.

    I have a memory area allocated with malloc. The memory area is used to hold pointers to other memory areas, ok. I am allocating the memory area for the pointers in the beginning of my app. But how do I allocate new memory blocks at runtime when I need it and store the pointers?
    Last edited by electrolove; 02-05-2003 at 07:31 AM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    10

    Question

    I really didn't get the question.

    What do u mean by "How do i allocate new memory blocks at runtime"....malloc does memory allocation at runtime. ????

    For what do u want to alloacte new memory blocks...for the pointers stored in the memory block u had allocated thru malloc at the. ???...

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Cool

    Originally posted by gotyatech
    I really didn't get the question.

    What do u mean by "How do i allocate new memory blocks at runtime"....malloc does memory allocation at runtime. ????

    For what do u want to alloacte new memory blocks...for the pointers stored in the memory block u had allocated thru malloc at the. ???...
    Example: I want to allocate a memory area when I press a button. The memory area must be 100 bytes. After that I want to store the pointer to that memory area in the first position of the memory area I allocated at the beginning of my app. The next time I press the button I want to allocate a new memory area with 100 bytes, and store the pointer to that memory area at position 2 at the memory area I allocated at the beginning of my app and so on...

    So the memory area I allocated at the beginning of my app is only used to store pointers to different 100 byte sized memory areas that is allocated when I press my button.
    You cantīt teach an old dog new tricks.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    A small example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       unsigned char **map = NULL;
    
       /* allocate space for 10 pointers */
       if((map = malloc(sizeof(*map) * 10)) == NULL)
       {
          printf("malloc map failed\n");
          return 1;
       }
    
       /* allocate 100 bytes */
       if((map[0] = malloc(sizeof(**map) * 100)) == NULL)
       {
          printf("malloc map[0] failed\n");
          free(map);
          return 2;
       }
    
       /* map[0] is now pointing to 100 bytes of allocated memory */
    
       free(map[0]);
       free(map);
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Monster
    A small example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       unsigned char **map = NULL;
    
       /* allocate space for 10 pointers */
       if((map = malloc(sizeof(*map) * 10)) == NULL)
       {
          printf("malloc map failed\n");
          return 1;
       }
    
       /* allocate 100 bytes */
       if((map[0] = malloc(sizeof(**map) * 100)) == NULL)
       {
          printf("malloc map[0] failed\n");
          free(map);
          return 2;
       }
    
       /* map[0] is now pointing to 100 bytes of allocated memory */
    
       free(map[0]);
       free(map);
       return 0;
    }
    Thanks, it seems that this was exactly what I needed
    You cantīt teach an old dog new tricks.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Monster
    A small example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       unsigned char **map = NULL;
    
       /* allocate space for 10 pointers */
       if((map = malloc(sizeof(*map) * 10)) == NULL)
       {
          printf("malloc map failed\n");
          return 1;
       }
    
       /* allocate 100 bytes */
       if((map[0] = malloc(sizeof(**map) * 100)) == NULL)
       {
          printf("malloc map[0] failed\n");
          free(map);
          return 2;
       }
    
       /* map[0] is now pointing to 100 bytes of allocated memory */
    
       free(map[0]);
       free(map);
       return 0;
    }
    Just one question:

    This line > malloc(sizeof(*map) * 10)

    This means that pointer size is 32 bits, right? So this memory area is in fact 40 bytes?
    You cantīt teach an old dog new tricks.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Yes, you're right:

    malloc(sizeof(*map) * 10) = malloc(sizeof(char *) * 10) = malloc(4 * 10) = 40 bytes of memory

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MAX memory allocation from c runtime heap
    By sandylovesgnr in forum C Programming
    Replies: 2
    Last Post: 10-23-2008, 11:02 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM