Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Variable length argument list - 32 / 64 bit problem?

Reply
Thread Tools

Variable length argument list - 32 / 64 bit problem?

 
 
Joakim Hove
Guest
Posts: n/a
 
      03-14-2008
Hello,

I have written the following (stripped down) code:

/********************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


char * path_fmt_alloc_path(const char * fmt , ...) {
char * new_path;
va_list ap;
va_start(ap , fmt);
{
int path_length = vsnprintf(new_path , 0 , fmt , ap);
new_path = malloc(path_length + 1);
vsnprintf(new_path , path_length + 1 , fmt , ap);
}
va_end(ap);
return new_path;
}


int test(const char * fmt , int id) {
char * s = path_fmt_alloc_path(fmt , id);
printf("%d -> %s \n",id ,s);
free(s);
}



int main(int argc, char **argv) {
const char * fmt = "%d";

test(fmt , 1 );
test(fmt , 2 );
test(fmt , 3 );
}


/*********************************/

When compiling with:
bash% gcc -m32 va_test.c
I get the result:

1 -> 1
2 -> 2
3 -> 3

i.e. what I expected. However it fails when compiling in 64 bit mode:

bash% gcc va_test.c
1 -> -
2 -> 0
3 -> 0

This is done with gcc version 3.4.6...


/***************************/

I am in some invoking undefined behaviour, or is this a bug somewhere
in my 64 bit toolchain?


Best Regards

Joakim Hove

 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 9:45 am, Joakim Hove <(E-Mail Removed)> wrote:
> Hello,
>
> I have written the following (stripped down) code:
>
> /********************************/
> #include <stdio.h>
> #include <stdlib.h>
> #include <stdarg.h>
>
> char * path_fmt_alloc_path(const char * fmt , ...) {
> char * new_path;
> va_list ap;
> va_start(ap , fmt);
> {
> int path_length = vsnprintf(new_path , 0 , fmt , ap);


new_path is not initialized. You mean to be printing to NULL here to
get the length, but... Maybe whether you get it set to 0/NULL depends
on which compiler you get. I'd suggest (vsnprintf(NULL,...) to make
your meaning clear. If new_path is not 0, you are writing to a wild
pointer.

-David
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-14-2008
Joakim Hove wrote:
> Hello,
>
> I have written the following (stripped down) code:
>
> /********************************/
> #include <stdio.h>
> #include <stdlib.h>
> #include <stdarg.h>
>
>
> char * path_fmt_alloc_path(const char * fmt , ...) {
> char * new_path;
> va_list ap;
> va_start(ap , fmt);
> {
> int path_length = vsnprintf(new_path , 0 , fmt , ap);


va_end(ap);
va_start(ap, fmt);

> new_path = malloc(path_length + 1);


if (new_path != NULL)

> vsnprintf(new_path , path_length + 1 , fmt , ap);
> }
> va_end(ap);
> return new_path;
> }
>
>
> int test(const char * fmt , int id) {
> char * s = path_fmt_alloc_path(fmt , id);


if (s != NULL)

> printf("%d -> %s \n",id ,s);
> free(s);
> }
>
>
>
> int main(int argc, char **argv) {
> const char * fmt = "%d";
>
> test(fmt , 1 );
> test(fmt , 2 );
> test(fmt , 3 );
> }
> [...]
> I am in some invoking undefined behaviour, or is this a bug somewhere
> in my 64 bit toolchain?


Try "refreshing" the va_list and see it that helps.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Joakim Hove
Guest
Posts: n/a
 
      03-14-2008
Hello,

thank you both for answering. I have tried to explicitly initialize
the new_path variable to NULL; but that did unfortunately not help.

> * * *Try "refreshing" the va_list and see it that helps.


Ehhh - what do you mean be refreshing?

Best Regards

Joakim
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-14-2008
Joakim Hove wrote:
> Hello,
>
> thank you both for answering. I have tried to explicitly initialize
> the new_path variable to NULL; but that did unfortunately not help.
>
>> Try "refreshing" the va_list and see it that helps.

>
> Ehhh - what do you mean be refreshing?


Go back and look at the lines I added to your code.

--
(E-Mail Removed)
 
Reply With Quote
 
Joakim Hove
Guest
Posts: n/a
 
      03-14-2008
On 14 Mar, 17:55, Eric Sosman <(E-Mail Removed)> wrote:

> > Ehhh - what do you mean be refreshing?

>
> * * *Go back and look at the lines I added to your code.


Got it - thanks a lot Now it is working.

Joakim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
Variable Length Argument List with Tomcat/JSP fails to compile. smm1@mail.com Java 3 12-12-2006 05:47 AM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
transfering variable length argument list ANDERS FLODERUS C++ 1 12-20-2003 08:27 AM



Advertisments