In article <··········@news.cs.tu-berlin.de> ···@cs.tu-berlin.de (Markus Freericks) writes:
From: ···@cs.tu-berlin.de (Markus Freericks)
Date: 04 Aug 1995 11:27:53 GMT
Organization: TU Berlin Fachbereich Informatik
Lines: 32
References: <······@bmtech.demon.co.uk> <············@wildcard.demon.co.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
In article <············@wildcard.demon.co.uk> Cyber Surfer <············@wildcard.demon.co.uk> writes:
> It could be written as:
>
> class str {
> int iCount;
> char achData[1];
> };
>
> Then you can allocate the extra memory. C++, like C, doesn't stop
> you from doing this.
No, but its wrong, wrong, wrong. Imagine a compiler that uses different
pointer types for differently-sized objects, e.g. a PC compiler.
str* foo=malloc(sizeof(str)+7000)
will give you a near pointer, while
str* foo=malloc(sizeof(str)+70000)
will give you some kind of far pointer, and likely break your program.
The Correct way is
class str {
int iCount;
char achData[MAXDATA]; /* whatever maximum size you want */
};
#define alloc_str_of_size(N) (str*)malloc(sizeof(str)-MAXDATA+N)
I will try to be extremely annoying in this post. Please bear with me
and be prepared to grin....
Here comes the flame bait.
If I have to allocate a string of lenght N (where N is a variable),
the best way to do it is
(make-string N)
Cheers
--
Marco G. Antoniotti - Resistente Umano
-------------------------------------------------------------------------------
Robotics Lab | room: 1220 - tel. #: (212) 998 3370
Courant Institute NYU | e-mail: ·······@cs.nyu.edu
| WWW: http://found.cs.nyu.edu/marcoxa
...e` la semplicita` che e` difficile a farsi.
...it is simplicity that is difficult to make.
Bertholdt Brecht