Dynamic memory allocation and pointers related concept doubts

Viewed 59

On the first note: it is a new concept to me!! I studied pointers and dynamic memory allocations and executed some program recently and was wondering in statement char*p="Computers" the string is stored in some memory location and the base address, i.e the starting address of the string is stored in p, now I noticed I can perform any desired operations on the string, now my doubt is why do we use a special statement like malloc and calloc when we can just declare a string like this of the desired length.

If my understanding of the concept Is wrong please explain.

Thanks in advance.

4 Answers

In this declaration

char*p="Computers";

the pointer p is initialized by the address of the first character of the string literal "Computers".

String literals have the static storage duration. You may not change a string literal as for example

p[0] = 'c';

Any attempt to change a string literal results in undefined behavior.

The function malloc is used to allocate memory dynamically. For example if you want to create dynamically a character array that will contain the string "Computers" you should write

char *p = malloc( 10 ); // the same as `malloc( 10 * sizeof( char ) )`
strcpy( p, "Computers" );

You may change the created character array. For example

p[0] = 'c';

After the array is not required any more you should free the allocated memory like

free( p );

Otherwise the program can have a memory leak.

A simple answer to that would be by doing char *p = "Computers"; you are basically declaring a fixed constant string. With that means you cannot edit anything inside the string. Trying to do so may result in Segmentation Fault. Using malloc and calloc would allow us to edit the string.

Simply do this on p[0] = 'c' and you will see the result

A statement like

char *p = "Computers";

is not an example of dynamic memory allocation. The memory for the string literal is set aside when the program starts up and held until the program terminates. You can’t resize that memory, and you’re not supposed to modify it (the behavior on doing so is undefined - it may work as expected, it may crash outright, it may do anything in between).

We use malloc, calloc, and realloc to allocate memory at runtime that needs to be writable, resizable, and doesn’t go away until we explicitly release it.

We have to use pointers to reference dynamically-allocated memory because that’s just how the language is designed, but pointers play a much larger role in C programming than just tracking dynamic memory.

as a novice, I described below as my own thinking…

Dynamic memory completely depends on the pointer. I mean without pointer knowledge you are able to cope up with dynamic memory allocation. (stdlib) library function where store calloc, malloc, relalloc and free.

malloc initialized no bit mentioned, calloc mainly used for the array. realloc used to increase or decrease size.

To simply say it is not as hard as what you first think. if you declare an array[500] initial declare but you used 100 and the rest of 400 bits remove to use dynamic memory.

Related