Skip to contentSkip to conversation

malloc(1/0) ??

DISCUSSION Jesse Pollard 2 participants 2 messages Nov 7, 2000

lore
Share & tools
lore
#0 lore raw
Quoted 20 lines
Quoted 16 lines

hi,
why does this program works. when executed, it doesnt give a segmentation fault. when the program requests memory, is a standard chunk is allocated irrespective of the what the user specifies. please explain.

main()

{
   char *s;
   s = (char*)malloc(0);
   strcpy(s,"fffff");
   printf("%s\n",s);
}
Quoted 3 lines

NOTE:
i know its a 'C' problem. but i wanted to know how
this works

Quoted 7 lines

The program does not work. A program works if it does what it's supposed to
do. If you want to argue that this program is supposed to print "ffffff" then explain to me why the 'malloc' contains a zero in parenthesis.

The program can't possibly work because it invokes undefined behavior. It
is impossible to determine what a program that invokes undefined behavior is 'supposed to do'.

All true, but the reason it "works" is that malloc WILL allocate some memory, even if it's only a few bytes of header.:

       |       |   (other memory block controled by malloc/free...)
       |-------|
       | header|
       |       |    - address returned to program
       | next  |
       | header|    (next memory block...)

Now the strcpy may have copied the string "fffff" over the next header. The copy worked, the printf worked (its buffers were already allocated...) BUT... If you allocate more memory via malloc, you will get an error (eventually). I believe malloc(0) allocates 4 bytes as a minimum, though this particular call IS undefined. You also did not check to see if malloc did return something (It did, or you would have gotten a segmentation fault from writing to location 0 with strcpy).

-------------------------------------------------------------------------
Jesse I Pollard, II
Email: [email protected]

Any opinions expressed are solely my own.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#1 lore raw
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

main()

{
   char *s;
   s = (char*)malloc(0);
   strcpy(s,"fffff");
   printf("%s\n",s);
}

I rather suspect that the strcpy() scribbled over malloc()s record keeping data. However, that memory was in the processes allowed address space so it didn't SIGSEGV. Now, when you call printf(), there is a very good chance that printf() tried to allocate some sort of buffer space since it is the first call to printf() in the program. Now, since malloc()s heap is messed up from the strcpy(), it crashes. (Probably because malloc() followed a pointer off into never-never land.) Hence, the crash appears in printf() instead of strcpy() or malloc(). I won't repeat the discussion about why malloc(0) succeeded.

William Astle
finger [email protected] for further information

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/M/S d- s+:+ !a C++ UL++++$ P++ L+++ !E W++ !N w--- !O !M PS PE V-- Y+ PGP t+@ 5++ X !R tv+@ b+++@ !DI D? G e++ h+ y?
------END GEEK CODE BLOCK------

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/