[ale] Stupid question

Joseph A Knapka jknapka at earthlink.net
Mon Oct 29 12:59:32 EST 2001


Christopher Fowler wrote:
> 
> I though of using one big buffer and storying everything there.  I would
> know the exact locations
> of everything.
> 
> Problem.
> 
> If my int is at 1 and hostname is 2-33 (32 bytes) , how to I access that
> easily.
> 
> I.E:
> 
> char *buffer;
> 
> buffer = (char *)malloc(CONFIGSIZE);
> 
> /* Do some stuff like read /etc/config */
> 
> printf("APPS: %d\n", buffer[0]);
> 
> /* this will not work */
> printf("HOSTNAME: %s\n", buffer[2-33]);
> 
> What would be the best approach to read and write config files that have a
> lot of information that can be different like that.  I would love to see
> some Howto's on the subject.
> 
> Chris
> 
> -----Original Message-----
> From: ,,, [mailto:fultongr at greenie.frogspace.net]On Behalf Of Fulton
> Green
> Sent: Monday, October 29, 2001 6:13 PM
> To: Christopher Fowler
> Cc: ale at ale.org
> Subject: Re: [ale] Stupid question
> 
> The question itself wasn't stupid, but leaving out the name of your
> development language was. ;-)
> 
> If this is C/C++, that "int" needs to be a "char" instead, and IIRC, should
> actually be a "short char", as some 32-bit platforms may try to use 16 bits
> (i.e., 2 bytes) for the character representation.
> 
> It would also help to know which function you're using to write the
> structure
> out, as well as how you're using it.
> 
> On Mon, Oct 29, 2001 at 05:55:13PM -0500, Christopher Fowler wrote:
> > When I write the folling:
> >
> > int c ='A'
> >
> > to a file, the file is 4 bytes in size.  In realsity that value could be
> > stored in one byte.
> >
> > When I look at the file, it looks like this:
> >
> > A     NULL            NULL            NULL
> >
> > using octal dump with the ascii option.  I'm trying to build a config file
> > that is created from
> > a sturcture of values.  The structure is very big.  My calculations of
> > locations were so wrong due to
> > ths size of an interger being 4 bytes on my machine.  Is there a way to
> make
> > the interger actually
> > 1 byte so that the only thing in the file is A.

Literally every C compiler in the universe supports
some form of structure packing control. With gcc you
can use the -fpack-struct command-line option. Be aware,
however, that this might make your code slower, since
on some architectures unaligned memory accesses are
illegal, so the compiler has to generate a bunch
of extra code to get the data into the proper place
in the struct.

There is probably a #pragma to do this on a struct-by-struct
basis, but I didn't find it in a casual surf of the
GCC docs.

HTH,

-- Joe
# "You know how many remote castles there are along the
#  gorges? You can't MOVE for remote castles!" - Lu Tze re. Uberwald
# (Obsolete as of 2.4.12) Linux MM docs:
http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list