cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/2003/01/16/16:15:43

From: Richard Dawe <rich AT phekda DOT freeserve DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: sizeof(struct x) doesn't compile -- how to do it ?
Date: Thu, 16 Jan 2003 21:08:16 +0000
Lines: 57
Message-ID: <3E271F40.D20EC4DD@phekda.freeserve.co.uk>
References: <b07318$3eg$1 AT news DOT online DOT de>
NNTP-Posting-Host: 62.137.53.123
Mime-Version: 1.0
X-Trace: news5.svr.pol.co.uk 1042751346 25594 62.137.53.123 (16 Jan 2003 21:09:06 GMT)
NNTP-Posting-Date: 16 Jan 2003 21:09:06 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.23 i586)
X-Accept-Language: de,fr
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hello.

Lars Hansen wrote:
> 
> #include "stdlib.h"

Unless you have your own stdlib.h, I would write this:

    #include <stdlib.h>

> 
> struct x
>  {
>   double x;
>  };
> 
> int main()
>  {
>   x* n=malloc(2*sizeof(struct x));
>  }
> 
> produces the following compiler error message:
> 
> test.c: In function `main':
> test.c:10: `x' undeclared (first use in this function)
> test.c:10: (Each undeclared identifier is reported only once
> test.c:10: for each function it appears in.)
> test.c:10: `n' undeclared (first use in this function)

If you are writing a C program, you need to write this:

    struct x* n=malloc(2*sizeof(struct x));
 
With C++ struct x { ... }; defines 'x' as a type in itself. In C you need a
typedef.

> So how do I get the size of structs with djgpp?

You have the right method.
 
> And also: if i have an array of a struct with several elements how can i
> know at which byte an element of the nth struct is in that array with djgpp
> (eg after writing a struct array to file and then loading this data without
> knowing how to "synchronize" djgpps compiler struct array generation and
> saving and an other compilers array struct generation and loading means one
> best knows the byte position (and length))

You can find out using the address of each member. But I wouldn't do that.
It's unportable between compilers. Different compilers may align the members
differently. Instead write out each member individually. You can also make the
data portable across different endian systems, if you always write the data in
big-endian or little-endian (as you wish) independent of the platform.

Regards,

-- 
Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]

- Raw text -


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