* malloc with size 0
@ 2005-12-22 4:43 Suma C
2005-12-22 6:21 ` Hareesh Nagarajan
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Suma C @ 2005-12-22 4:43 UTC (permalink / raw)
To: C programming list
Hi all
When a 0 is passed to malloc, I get a valid pointer instead of a NULL
.What does this mean?
thanks in advance
regards
suma
PS:
code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr = NULL;
ptr = malloc(0);
printf("ptr :%u \n",ptr);
strcpy(ptr,"abcdefghijkl");
printf("%s\n",ptr); //no seg fault here????
return 0;
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: malloc with size 0
2005-12-22 4:43 malloc with size 0 Suma C
@ 2005-12-22 6:21 ` Hareesh Nagarajan
2005-12-22 7:42 ` Suma C
2005-12-22 13:02 ` Neil Horman
2005-12-30 14:16 ` mail-lists
2 siblings, 1 reply; 5+ messages in thread
From: Hareesh Nagarajan @ 2005-12-22 6:21 UTC (permalink / raw)
To: Suma C, linux-c-programming
On 12/22/05, Suma C <gsumac@gmail.com> wrote:
> When a 0 is passed to malloc, I get a valid pointer instead of a NULL
> .What does this mean?
A search on Google would have given you this thread:
http://www.theone.ru/lists/comp.lang.c/msg00504.html
hth,
./hareesh
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: malloc with size 0
2005-12-22 6:21 ` Hareesh Nagarajan
@ 2005-12-22 7:42 ` Suma C
0 siblings, 0 replies; 5+ messages in thread
From: Suma C @ 2005-12-22 7:42 UTC (permalink / raw)
To: Hareesh Nagarajan; +Cc: linux-c-programming
http://www.ussg.iu.edu/hypermail/linux/kernel/0011.0/1078.html got
this one...the thread discusses the same situation
I guess it talks about malloc allocating a minimum memory (the header
space !!??)...
and so the next question will be: wt about the unique pointer which is
given in the man page!!!
regards
suma
PS
snip from the link above...
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).
On 12/22/05, Hareesh Nagarajan <hareesh.nagarajan@gmail.com> wrote:
> On 12/22/05, Suma C <gsumac@gmail.com> wrote:
>
> > When a 0 is passed to malloc, I get a valid pointer instead of a NULL
> > .What does this mean?
>
> A search on Google would have given you this thread:
> http://www.theone.ru/lists/comp.lang.c/msg00504.html
>
> hth,
>
> ./hareesh
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: malloc with size 0
2005-12-22 4:43 malloc with size 0 Suma C
2005-12-22 6:21 ` Hareesh Nagarajan
@ 2005-12-22 13:02 ` Neil Horman
2005-12-30 14:16 ` mail-lists
2 siblings, 0 replies; 5+ messages in thread
From: Neil Horman @ 2005-12-22 13:02 UTC (permalink / raw)
To: Suma C; +Cc: C programming list
On Thu, Dec 22, 2005 at 10:13:57AM +0530, Suma C wrote:
> Hi all
>
> When a 0 is passed to malloc, I get a valid pointer instead of a NULL
> .What does this mean?
>
> thanks in advance
>
> regards
> suma
>
> PS:
> code:
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char *ptr = NULL;
> ptr = malloc(0);
> printf("ptr :%u \n",ptr);
> strcpy(ptr,"abcdefghijkl");
> printf("%s\n",ptr); //no seg fault here????
> return 0;
> }
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
http://www.opengroup.org/onlinepubs/000095399/toc.htm
If you pass 0 into malloc, you will get back NULL or a unique pointer suitable
for passing to free. There is no guarantee about the amount of memory you can
write to beyond the pointer, and it is in fact not allocated to you (allocation
of 0). As such, _dont_write_to_it_, as a subseuqent allocation may be
destroyed. All the pointer is good for (if you get one back) is being passed to
free.
Regards
Neil
--
/***************************************************
*Neil Horman
*Software Engineer
*gpg keyid: 1024D / 0x92A74FA1 - http://pgp.mit.edu
***************************************************/
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: malloc with size 0
2005-12-22 4:43 malloc with size 0 Suma C
2005-12-22 6:21 ` Hareesh Nagarajan
2005-12-22 13:02 ` Neil Horman
@ 2005-12-30 14:16 ` mail-lists
2 siblings, 0 replies; 5+ messages in thread
From: mail-lists @ 2005-12-30 14:16 UTC (permalink / raw)
To: 'Suma C', 'C programming list'
Hi,
This was a strange problem I came across about 2 years ago.
Some systems return NULL some return a valid result.
I would treat it as a case where you would not try to alloc a size of 0 its
kind of unpredictable depending on lib version and which system your on.
James
> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-
> programming-owner@vger.kernel.org] On Behalf Of Suma C
> Sent: 22 December 2005 04:44
> To: C programming list
> Subject: malloc with size 0
>
> Hi all
>
> When a 0 is passed to malloc, I get a valid pointer instead of a NULL
> .What does this mean?
>
> thanks in advance
>
> regards
> suma
>
> PS:
> code:
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char *ptr = NULL;
> ptr = malloc(0);
> printf("ptr :%u \n",ptr);
> strcpy(ptr,"abcdefghijkl");
> printf("%s\n",ptr); //no seg fault here????
> return 0;
> }
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-
> programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-12-30 14:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-12-22 4:43 malloc with size 0 Suma C
2005-12-22 6:21 ` Hareesh Nagarajan
2005-12-22 7:42 ` Suma C
2005-12-22 13:02 ` Neil Horman
2005-12-30 14:16 ` mail-lists
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).