C/C++: The pointer on line

Objectives : In one of the previous document (compare_c_cplus1.htm), we understood the steps of dynamic memory allocation from heap, either with "malloc()/free()" functions or with "new/delete" objects.

This example will run in IDE supporting C99. I noticed that Eclipse CDT, did not support "gets(buffer)" function. Please use Quincy 2005 or Visual Studio 2008, 10.

In this example we would reallocate an input string of larger size ("ch1 = (char *)malloc(100);" ), to a smaller size on the fly using realloc() function, which could not be done with "new/delete" objects.

I also intend to reemphasize the importance of the pointer, denoted as * , in dynamic memory allocation from Heap.

  • File Name : msize_realloc_malloc1.cpp
    C library provides malloc() and realloc() functions,  and we can assign a larger block of memory reserve to handle the user's input and on demand reallocate or resize the block to release unused space to the system.
  • Syntax and Uses :
    ch1 = (char *)malloc(100);
    chptr1 = (char*) calloc((strlen(buff)+1),sizeof(char));
    ch1 =(char *)realloc(ch1,strlen(ch1)+1);
  • Function gets(ch1) : char * gets ( char * ch1 );
  • Code : msize_realloc_malloc1.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step 3 Runtime Views: C++ has backend compatibility of supporting C functions, like malloc(), calloc() or realloc() function to manipulate memories at a lower level.

a) Reallocation of memory : The size of the input was fixed to 100 bytes/characters, with realloc() function, and most of it remained used. The function, ralloc(), was used to adjust to it's required length than the size you started with.  In this case you entered 36 bytes/characters copied to the

Please note the allocated size before (100 bytes) and after (36 bytes)in the display as shown below.

b) Without reallocating memory: memory size will remain same

As we chose not to reallocate the memory block (100 bytes), the size will remain same as declared and initiated.

 

Step: Brief discussion: In one of the example in this chapter (compare_c_cplus1.htm) we briefly compared malloc/calloc() function with new/delete objects. In this example we used the function realloc(), which must succeed either calloc() or malloc() function,  to reallocate the memory on Heap. At the end we used  a function,free(ch1) with a void pointer as a parameter, to free the memory hold-ups by calloc/realloc() functions.

In this example, I allocated a 100 bytes of memory in the first place, freed unused memories and sent those back to heap.

The use of pointer is a step towards dynamic memory allocation, but we can't work like an anarchist.

FAQ :

  • Size of a character : sizeof(char) is always 1.
  • Allocation with malloc: char *ch1;
    •  "ch1 = (char *)malloc(100); " allocates 100 characters.
    • If fails, a NULL pointer is returned , and if succeeds a pointer to the block of memory would be returned.
  • Allocation with calloc():
    • chptr1 = (char *)calloc(10,sizeof(char));// allocates 10 characters
    • calloc() initializes the memory block to 0; while malloc() does not initialize the memory allocated.