Thread: five questions about dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    five questions about dynamic memory allocation

    Q0: What is the real purpose of malloc(), realloc() and free() functions?

    I have read that they enable to allocate memory in cases when the amount of memory to be allocated is unknown. But in that case, ( Q1: ) why does the function malloc() has an argument then? (if the amounf of memory is unknown)

    Here is a code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
    
       char name[100];
       char *description;
    
       strcpy(name, "Zara Ali");
    
       /* allocate memory dynamically */
       description = malloc( 30 * sizeof(char) );
    	
       if( description == NULL ) {
          fprintf(stderr, "Error - unable to allocate required memory\n");
       }
       else {
          strcpy( description, "Zara ali a DPS student.");
       }
    	
       /* suppose you want to store bigger description */
       description = realloc( description, 100 * sizeof(char) );
    	
       if( description == NULL ) {
          fprintf(stderr, "Error - unable to allocate required memory\n");
       }
       else {
          strcat( description, "She is in class 10th");
       }
       
       printf("Name = %s\n", name );
       printf("Description: %s\n", description );
    
       /* release memory using free() function */
       free(description);
    }
    from C Memory Management .

    Q2: Can I also have a variable of different type than just chars (eg double, int)?

    Q3: What is the following code? Is it allocating a memory for more variables of an array? Why do not they use the calloc() function then?

    Code:
    int array[10];
    int * array = malloc(10 * sizeof(int));
    Q4: What is the maximum number I can write inside the argument of the malloc() function?

    eg can I write

    Code:
       char *a;
       a=malloc(100000*sizeof(char));
    ?

    Could you please help me in getting to know the dynamic memory allocation in C?

    Thank you.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    Q0 & Q1: Unknown at compile-time, not at run-time. In your example, malloc() is used for the sole purpose of using realloc() and free() on the pointer. It isn't really needed.

    Q2: Yes, you can. You just allocate a bigger chunk of memory via "sizeof(type)*amount".

    Q3: That code should never compile, you have two variables with the same name inside the same scope. The first is a static array( of 10 ints) and the second a pointer to int.

    Q4: malloc() argument is of type "size_t", which usually is "unsigned long"( the actual maximum value depends on your hardware and OS )
    Devoted my life to programming...

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Q4: What is the maximum number I can write inside the argument of the malloc() function?
    You can find out by executing something like:
    Code:
     printf("%zu\n", (size_t)-1);
    No assumptions are being made - size_t doesn't have to be the same as unsigned long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation
    By mp252 in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2013, 05:23 PM
  2. dynamic memory allocation
    By silver_comet in forum C Programming
    Replies: 2
    Last Post: 05-30-2011, 12:05 PM
  3. Dynamic Memory Allocation
    By JamesD in forum C Programming
    Replies: 10
    Last Post: 03-12-2011, 12:08 AM
  4. Memory dynamic allocation
    By elvio.vicosa in forum C Programming
    Replies: 8
    Last Post: 10-16-2007, 03:30 AM
  5. Help with dynamic memory allocation
    By malooch in forum C Programming
    Replies: 2
    Last Post: 12-13-2006, 01:26 PM