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 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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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, ... );
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
|
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....
|
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;
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
| |