www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/09/12:26:10

Xref: news2.mv.net comp.os.msdos.djgpp:3628
From: alexlehm AT rbg DOT informatik DOT th-darmstadt DOT de (Alexander Lehmann)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: vararg
Date: 8 May 1996 22:32:55 GMT
Organization: Technische Hochschule Darmstadt
Lines: 60
Message-ID: <4mr7en$mnu@rs18.hrz.th-darmstadt.de>
References: <9605080624 DOT AA16965 AT malta>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Rainer Wank (wank AT uebemc DOT siemens DOT de) wrote:
: Hi folks,

: i don't know much about variable argument lists, so
: i read a few in the Kernighan/Ritchie book.
: After that i wrote a little program:

: #include <stdarg.h>
: #include <stdio.h>

: unsigned char fg;

: void abc(int anzahl, unsigned char abc, ...)
: {
:  va_list ap;
:  va_start(ap, abc);
:  fg = va_arg(ap, unsigned char);
:  printf("\n\rErster variabler Parameter: %d", fg);
:  va_end(ap);
: }

: void main(void)
: {

:  abc((int) 10, (unsigned char) 10, (unsigned char) 51);
: }


: I haved compiled it on my 80486 DX 2 with djgpp gcc version 2
: (operating system OS/2 2.11). 
: When i run the program it always prints 0 to stdout.

: I also have compiled the program with BORLAND C++ 3.1 and it
: works fine. Furthermore the acc compiler on the sun delivered
: a result i expected.

OK, after having compiled the program with djgpp (on a 386sx with 1MB memory,
that is slow), I think what the problem is.

Gcc creates local copies for char variables passed as arguments (I have no
idea why), which means that taking the pointer of a char argument doesn't give
the address of the parameter on the stack, but in the local variable area
below the frame pointer. All arithmetic done relative to that doesn't,
obviously and since the address of the char variable is -1(fp), all following
char arguments are 0, since it always hits the highest byte of each int and
address above that (would be fun to change anything, but that doesn't happen
with varargs, obviously).
This effect isn't just in djgpp, it also happens e.g. on hpux/68k (I have also
tried hpux/hppa, but that assembler syntax is beyond my comprehension) so
the only way to fix this is to use an argument that is sizeof(int) as last
parameter before the ...


bye, Alexander

--
Alexander Lehmann,                                  |  "On the Internet,
alex AT hal DOT rhein-main DOT de  (plain, MIME, NeXT)         |   nobody knows
alexlehm AT rbg DOT informatik DOT th-darmstadt DOT de (plain)     |   you're a dog."
<URL:http://www.student.informatik.th-darmstadt.de/~alexlehm/>

- Raw text -


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