C language

Q1: Memory allocation

Now we have the following statements and we see how STACK & HEAP memory is allocated to different variables. We have the empty HEAP memory & STACK memory as:

NOTE: For this question we have used a heap memory & allocated space serially while in real, memory is allocated randomly by operating system.

www.exploreroots.com

char* x;

x= (char*)malloc(2);

char* y= (char*)malloc(4);

free(x);

char* z= (char*)calloc(3, sizeof(char));

free(y);

free(z);

Now we represent the HEAP and STACK memory after each statement.

char* x;

Assuming X would occupy 4-bytes for storing address. Hence 4 bytes are allocated for x in the STACK but only some random value that is already preset at the allocated memory gets stored in the pointer.

www.exploreroots.com

x= (char*)malloc(2);

With this instruction, firstly 2 bytes are allocated from the HEAP and then the starting address of the allocated memory is stored in the pointer x but the values stored in the memory allocated are random. Now note that in the pointer there is no information stored about where the allocated memory ends. So how would we know where the memory ends while accessing memory? Actually we can’t know the end. The programmer has to take care of himself otherwise it’ll cause ERROR. While storing characters a NULL character ’\0’ is stored to detect the end of memory.

www.exploreroots.com

Char* y= (char*)malloc(4);

Similar to the above case, 4 bytes are allocated for the pointer in the STACK and then the 4 bytes are allocated from HEAP and starting address is stored in the pointer.

www.exploreroots.com

free(x);

www.exploreroots.com

Now there ia no memory allocated to the pointer x and hence if now we try to use the memory then it may cause ERROR as we’ll referencing to the deallocated memory. Now one may ask that what the problem in referencing that memory is. Actually as we have de-allocated the memory, the operating system can allocate that space to some other proess and then if we refer to that space then it may cause a serious ERROR. HOW CAN WE AVOID THIS PROBLEM? We can avoid this problem by putting NULL to the pointer after de-allocating the memory as shown:

free(x);

x= NULL; So that now if we mistakenly try to refer to the memory then it causes no serious problem.

Char* z= (char*)calloc(3, sizeof(char));                         SIZE OF CHAR WE TAKE AS 1 BYTE.

calloc() function is same as malloc() but it also initializes the allocated memory to zero. Hence 3 bytes are allocated from the HEAP memory and zero (0) is stored in the allocated memory 2006 – 2008 and starting address is stored in the pointer z.

www.exploreroots.com

free(y);

The memory allocated to pointer y is freed and hence that memory is available for re-use.

www.exploreroots.com

free(z);

The memory allocated to pointer z is freed and hence that memory is available for re-use.

www.exploreroots.com

The STACK memory would be freed when the function containing the above statements returns or if statements are in the main () function, then the memory in the STACK would be freed when the program ends.

16 Replies to “Q1: Memory allocation

  1. Pingback: teslatoto
  2. Pingback: read
  3. Pingback: dultogel rtp
  4. Pingback: treesap thc
  5. Pingback: once human cheats
  6. Pingback: Bobs SEO
  7. Pingback: Event venue phuket
  8. Pingback: som777

Leave a Reply

Your email address will not be published. Required fields are marked *