Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 15 Jan 2013 23:28:14 +0100
From: Igmar Palsenberg <>
Subject: Re: malloc(0) behaviour

>> That's there to access if size is 0 ? Sure, you can access :
>> struct foo {
>> };
> This is a constraint violation. C does not allow empty structs, and
> even if it did, they would not have size 0, since no type or object
> ever has size 0 in C.

GCC thinks otherwise : 

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

struct test {

int main(int argc, char **argv)
	char *x = NULL;

	printf("sizeof test : %d\n", sizeof(struct test));

	return 0;

[igmar@...el ~]$ ./x
sizeof test : 0

It gives me a warning, but doesn't error out. Olders version might behave differently, I don't have those installed. The LLVM compiler does the same.
No idea what the standard says, but your remarks sounds correct to me. 

>> which is size 0. I do wonder what that gives me in practice. That is, not counting the fact that :
>> if (size == 0)
>> 	size = 1;
>> was a common practice in malloc() implementations a while ago.
> Of course, this is the canonical, simplest way to make malloc(0)
> return a unique pointer.

Enough for this thread. I did got the answer I wanted, and the result I want is easy to realise. Not mu intention to irritate people.


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.