warning message with va_list 
Author Message
 warning message with va_list

hello,
I'm trying to get va_list towork in an embedded system using C, the
compiler won't let me use the .h files to do the defines, so I've had
to get the va defines off the net together with the test code, however
I'm getting an error

DeviceWrite.c:161: warning: cast increases required alignment of
target type

if I double compile it works, but I can't figure why the warnings
appearing, I sure it's to do with the cast, any help.  The error comes
from this line

total += va_arg(ap, int);

the defines are

typedef char *va_list[0];

#define va_start(ap, parmN) ((ap)[0] = (char *) &(parmN) + \
      ((sizeof(parmN) + sizeof(int) - 1) & ~(sizeof(int) - 1)), (void)
0)

#define va_arg(ap, type) ((ap)[0] += \
      ((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)), \
      (*(type *) ((ap)[0] - \
      ((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)))))

#define va_end(ap) ((ap)[0] = 0, (void) 0)

and the test code where the warning appears is

int find_the_sum(int count, ...)
     {
       va_list ap;
       int i;
       int total = 0;

       va_start(ap, count);
       for (i = 0; i < count; i++)
           total += va_arg(ap, int);
       va_end(ap);
       return total;
     }

int other_function(void)
     {
       return find_the_sum(2,2,2,2,2,2,2,2,2,2,2);
     }
thanks



Wed, 02 Nov 2005 00:03:51 GMT  
 warning message with va_list

Quote:

> I'm trying to get va_list towork in an embedded system using C, the
> compiler won't let me use the .h files to do the defines, so I've had
> to get the va defines off the net together with the test code, however
> I'm getting an error

> DeviceWrite.c:161: warning: cast increases required alignment of
> target type

Warning, not error.

The warning is correct, but warns about code which is harmless in this
case.  It looks like a gcc warning, if so you can turn it off by
removing the `-Wcast-align' option.

Quote:
> if I double compile it works, but I can't figure why the warnings
> appearing, I sure it's to do with the cast, any help.  The error comes
> from this line

> total += va_arg(ap, int);
> (...)
> typedef char *va_list[0];
> #define va_arg(ap, type) ((ap)[0] += \
>       ((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)), \
>       (*(type *) ((ap)[0] - \
>       ((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)))))

It somes from the
       (type *) ((ap)[0] - ...)
part.  ap[] is a char*, and char data have no alignment requirements.
It is cast to int*, and int data on your machine have alignment
requirements.  Such a cast is sometimes an error.  In this case it is
OK, since the implementation will have aligned the arguments correctly.

It's stupid of the compiler to warn about its own headers, though.
Unless you are using gcc with the system's headers?  gcc normally
installs its own stdarg.h in a directory named something like
 /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/3.2.3/include/
-- you can find it by typing 'gcc -v' -- but maybe gcc is installed
wrongly on your machine, or deliberatly does not use its own stdarg.h on
your system.  Anyway, if so, go to newsgroup gnu.gcc.help.

--
Hallvard



Wed, 02 Nov 2005 00:25:16 GMT  
 warning message with va_list

Quote:
>I'm trying to get va_list towork in an embedded system using C, the
>compiler won't let me use the .h files to do the defines, so I've had
>to get the va defines off the net together with the test code, however

If your compiler will not allow you to #include <stdarg.h>, I'd suggest you
fix _that_ problem before anything else.

<snip diy va_* defines>

Quote:
>int find_the_sum(int count, ...)
>     {
>       va_list ap;
>       int i;
>       int total = 0;

>       va_start(ap, count);                 // arg1 == count == 2
>       for (i = 0; i < count; i++)               // sums arg2 (2) & arg3 (2)
>           total += va_arg(ap, int);
>       va_end(ap);
>       return total;
>     }

>int other_function(void)
>     {
>       return find_the_sum(2,2,2,2,2,2,2,2,2,2,2);
>     }

You are aware that only the second and third parameters to the
find_the_sum() function will be summed? The remainder of the argument list
is simply ignored.

See my // comments above in find_the_sum().

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/



Wed, 02 Nov 2005 01:19:11 GMT  
 warning message with va_list

Quote:

> hello,
> I'm trying to get va_list towork in an embedded system using C, the
> compiler won't let me use the .h files to do the defines, so I've had
> to get the va defines off the net [...]

    Hold it right there: you're already in trouble.

    The <stdarg.h> macros cannot be written in a compiler-
independent way, since they require intimate knowledge of
exactly how arguments get passed to functions.  Try to use
a set of macros for Compiler A instead of macros specifically
written for Compiler B, and your chances of success are just
that: chances, not guarantees.  If you succeed, it's luck
and not skill.

    The macros you found seem to assume that the arguments
all reside in memory, at increasing addresses as they are
read from left to right, and padded to multiples of `int'
size.  It used to be that this assumption was true for many
or even most machines, but nowadays it's become quite shaky.
CPUs have gained in speed to the point where memory is now
considered "slow," and on modern systems it's quite common
to see arguments passed in registers where possible, instead
of taking the speed penalty to store and fetch them from
memory.  A fairly common pattern is "The first N arguments
go in registers (if they fit), and any that don't fit or
any beyond the first N go in memory."  Faced with such a
convention, the macros you've found would just be useless.

    Follow Robert Clark's advice

Quote:
> If your compiler will not allow you to #include <stdarg.h>, I'd
> suggest you fix _that_ problem before anything else.

... because only the compiler's own personal <stdarg.h>
knows the incantations that make the blue smoke happen.

--



Wed, 02 Nov 2005 01:56:27 GMT  
 warning message with va_list

Quote:


>> hello,
>> I'm trying to get va_list towork in an embedded system using C, the
>> compiler won't let me use the .h files to do the defines, so I've had
>> to get the va defines off the net [...]

>     Hold it right there: you're already in trouble.

True.  I didn't notice the part about getting stdarg off the net.

--
Hallvard



Wed, 02 Nov 2005 02:38:30 GMT  
 warning message with va_list
Thanks,  first I'll try to get the correct .h, I couldn't find it but
I'll try the GNU group.  And yes the version of the compiler I'm using
doesn't seem to allow it from the .libc file.  Don't know why.


Fri, 04 Nov 2005 16:09:02 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. a va_list in a va_list

2. VC 7.1 and massive (repeating) warning messages

3. Uninitialised variable use. (gcc warning messages)

4. puzzling warning message

5. Warning Message in compiling

6. Warning message

7. Warning message

8. Warning message when a function return a newly allocated struct

9. what is this warning message mean ?

10. compiling warning message

11. I need help on this compile warning message..

12. Annoying warning message

 

 
Powered by phpBB® Forum Software