malloc ( )

Avinash Sonawane avinash.sonawane1990@gmail.com
Wed Aug 31 12:43:00 GMT 2011


Respected Sir,
I am using gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

I have one doubt regarding malloc () in gcc.
When I tried to execute the program given below, it got executed.

But since 256 = 1 0000 0000 (9 bits) , How malloc can store it at 'p'
since I have allocate only 1 byte of memory (8 bits of memory) ?
Please guide me.

Program:

# include <stdio.h>
# include <stdlib.h>

 int  main(void)

 {
  int *p;
  p=(int *) malloc(1);

  *p=256;

  printf("%d \t %ld \n",*p,p);

  free(p);
  return 0;
 }

Terminal:

avinash@titanic:/Remastersys/Documents/Programs/c$ gcc malloc.c -o malloc
malloc.c: In function ‘main’:
malloc.c:12:3: warning: format ‘%ld’ expects type ‘long int’, but
argument 3 has type ‘int *’
avinash@titanic:/Remastersys/Documents/Programs/c$ ./malloc
256 	 8044560

-- 
Avinash Sonawane
PICT Pune
India



More information about the Gcc-help mailing list