Thread: why the output of malloc is zero

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    why the output of malloc is zero

    You can see bellow my code where i try to understand how to use malloc. But, i do not understand something. Why the output of A array is all zero? I thought that calloc does this "trick" and not malloc. Can anyone help me? thanks in advance!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    	int i;
    	int *A;
    		A = malloc(5);
    		if(A==NULL){
    			printf("Error in allocating space for A array!\n");
    			exit(1);
    		}else{
    			printf("Allocating space for A array succeed!\n");
    		}
    		// printing the array without any changes
    		for(i=0; i<5; i++){
    			printf("A[%d]: %d\n", i, A[i]);
    		}
    		printf("\n");
    		// made some changes
    		A[0] = 4;
    		// again printing the array
    		for(i=0; i<5; i++){
    			printf("A[%d]: %d\n", i, A[i]);
    		}
    		printf("\n");
    	return 0;
    }

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Malloc(5) allocated 5 bytes not 5 int. What you meant was malloc(5*sizeof(int)). What you code print out is just gibberish because you were accessing unallocated memory.
    Last edited by nimitzhunter; 02-12-2011 at 05:27 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If it's zero it's because it happened to be zero and not something caused by malloc. This should be evident by what nimitzhunter said, you are reaching out of the area that you have malloced. In effect only A[0] + one byte is memory you own.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thanks! i understood the difference...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first time malloc gets memory from the OS, it seems likely (on your machine at least) that it is filled with zeros.

    But if you use the memory, free it, then allocate it again, you're more likely to see the "junk" from previous usage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM