delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/02/16:31:01

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Size of a struct is not correct
Date: Thu, 02 Jul 1998 19:58:39 GMT
Organization: Customer of EUnet Austria
Lines: 45
Message-ID: <35a7e356.2691607@news.Austria.EU.net>
References: <01bda5a5$ce48b420$0100a8c0 AT dismuntel DOT ctv DOT es>
NNTP-Posting-Host: e103.dynamic.vienna.at.eu.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: "Javier Calleja" <dismuntel AT apdo DOT com>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Thu, 2 Jul 1998 12:40:18 +0200:

>Hi world
>
>  I have built the following struct:
>
>  struct
>  {
>    char one;
>    short two,three;
>    long  four
>  } foo;
>
>    But when I do sizeof of this struct, the result is wrong (it says the
>struct has 12 bytes).
>    I think gcc tryes to allocate the struct variables in blocks of 4 bytes.
>    But I have a problem, because I use the following sentence:
>
>    memcpy(pointer_to_memory,&foo,sizeof(foo));
>
>   And I must to allocate each variable in a memory position of 9 bytes,
>when the first byte is one variable, the following two bytes are a short
>variable, ...
>
>  Does anything have a solution?

Yes. move that char one; to the end of the struct. It is not sure that this
really works, but I think usually most compilers add pad bytes to the end of a
structure.

Why you woud have a problem with memcpy I don't understand. As long as you use
everywhere sizeof(foo) to get the size, the code should work on all machines.
The only problem you might get is, if you write a datafile on one machine and
move it to a different machine with another OS (or CPU).
--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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