C Tutorial Dynamic strings
Advanced C Arrays
Plus Syntax
Pointer Style and strcpy()
Pointer Type Effects
Arrays and Pointers
Array Names Are Const
Heap Memory
Memory Management
Dynamic Arrays
Advantages of being in the heap
Disadvantages of being in the heap
Dynamic Strings

 
 

 

HOME
 

The dynamic allocation of arrays works very well for allocating strings in the heap. The

advantage of heap allocating a string is that the heap block can be just big enough to store

the actual number of characters in the string. The common local variable technique such

 as  char string[1000]; allocates way too much space most of the time, wasting the unused bytes, and yet fails if the string ever gets bigger than the variable's fixed size.

 

#include <string.h>

 

/*

Takes a c string as input, and makes a copy of that string

in the heap. The caller takes over ownership of the new string

and is responsible for freeing it.

*/

char* MakeStringInHeap(const char* source) {

char* newString;

 

newString = (char*) malloc(strlen(source) + 1); // +1 for the '\0'

assert(newString != NULL);

strcpy(newString, source);

return(newString);

}

 

Want To Know more with

Video ???

Contact for more learning: webmaster@freehost7com