www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/11/20:29:05

Sender: nate AT cartsys DOT com
Message-ID: <36E86D58.20B61E0E@cartsys.com>
Date: Thu, 11 Mar 1999 17:26:48 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.1 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Replacing malloc(size) with new and delete - is that possible
References: <Pine DOT GSO DOT 3 DOT 95 DOT 990311095808 DOT 9547A-100000 AT hlrz18>
Reply-To: djgpp AT delorie DOT com

Holger Wahlen wrote:
> 
> rpolzer AT gmx DOT de asked:
> 
> > Can I do the following: (in C++)
> >
> > struct PACKET {
> > // ... some data
> > char P_DATA[0];
>              ^^^
> > };
> 
> Typo, I guess.

Not necessarily.  It's a GCC extension to support a hack that's seen
occasionally to implement variable-length structures.  For instance:

struct message {
	int length;
	char body[0];
};

void pass_message(char *s)
{
	struct message *p;
	p = malloc (sizeof(struct message) + strlen(s) + 1 /* null */ );
	strcpy(p->body, s);
	p->length = strlen(s);
	transmit(p);
}

In traditional implementations you would have to use `char body[1]',
which is a bit more awkward.
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019