473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Variable length argument list - 32 / 64 bit problem?

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

Mar 14 '08 #1
5 2424
On Mar 14, 9:45 am, Joakim Hove <joakim.h...@gmail.comwrote:
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
Mar 14 '08 #2
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.

--
Er*********@sun.com
Mar 14 '08 #3
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
Mar 14 '08 #4
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.

--
Er*********@sun.com
Mar 14 '08 #5
On 14 Mar, 17:55, Eric Sosman <Eric.Sos...@sun.comwrote:
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
Mar 14 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philip Rittenhouse | last post by:
I have discovered a couple of problems with the way the universal gateway code handles optional parameters and variable argument lists in COM servers. It appears to only be a problem when you...
1
by: ANDERS FLODERUS | last post by:
Is it possible to transfer a variable length argument list? What I want to do is just void myPrintf( char *pFormat, ... ) { char buf; sprintf( buf, pFormat, ... );
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
5
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like...
4
by: JH | last post by:
Fairly new to c# so please forgive any simple oversights. I'm developing a console app that loads dll assemblies at run time. The app scans a "plugin" directory for these dlls. Each dll is loaded...
3
by: Tomás | last post by:
Let's say we have a variable length argument list function like so: unsigned GetAverage(unsigned a, unsigned b, ...); I wonder does the compiler go through the code looking for invocations of...
6
by: Adam | last post by:
Hey, Just a quick appeal for suggestions re some code, able to pass varying numbers of arguments... I have a function which can accept varying numbers of arguments. It in turn calls a...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
30
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.