473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stdarg problems

Hi,

Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}

void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);

while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);

}
--------------------------------------------

the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.

UH??AWAVAUATSH? ?8H?E?
------------------------------------------

Thanks,
NarikNahom

Jul 1 '07 #1
13 2144
On Sun, 01 Jul 2007 15:50:50 +0000, nariknahom wrote:
Hi,

Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}

void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);

while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);

}
--------------------------------------------

the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.

UH??AWAVAUATSH? ?8H?E?
------------------------------------------
Cleaned up and compilable version of your code.

#include <cstdarg>
#include <iostream>

void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
std::cout<< msg<<std::endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}

int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is working.",0 );
return 0;
}

--
Obnoxious User
Jul 1 '07 #2
On Sun, 01 Jul 2007 15:50:50 +0000, nariknahom wrote:
>
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);

while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);

}
You need to know how many arguments you can parse. Your
code is stepping outside its proper context and into
uncharted memory, thus printing weird stuff.

http://www.cppreference.com/stdother/va_arg.html

--
Obnoxious User
Jul 1 '07 #3
Obnoxious User wrote:
On Sun, 01 Jul 2007 15:50:50 +0000, nariknahom wrote:
>Hi,

Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}

void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);

while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);

}
--------------------------------------------

the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.

UH??AWAVAUATSH ??8H?E?
------------------------------------------

Cleaned up and compilable version of your code.

#include <cstdarg>
#include <iostream>

void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
std::cout<< msg<<std::endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}

int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is working.",0 );
I believe this should be

f1( 25 50,"testing stdarg. ", "it is working", "if it is working.",
static_cast<con st char*>(0) );

To make it guaranteed to work. Variable argument functions are mine fields.

The reason is that you must use the same type in va_arg as in the call, and
also because the integer 0 is not required to have the same binary
representation as a null pointer.
return 0;
}
--
rbh
Jul 1 '07 #4
na********@gmai l.com wrote:
Hi,

Can someone tell me what is wrong with the below program?
This:
while (msg != 0 )
checks for a null argument, but with:
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
you never supplied one, so your function can't find the end of your argument
list.

Jul 1 '07 #5
On Jul 1, 8:50 am, narikna...@gmai l.com wrote:
Hi,

Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;

}

void f1(bool condition, constchar* msg,...)
{
va_list ap;
va_start(ap, msg);

while (msg != 0 )
{
cout<< msg<<endl;
msg =va_arg(ap, constchar*);
}
va_end(ap);

}

--------------------------------------------

the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.

UH??AWAVAUATSH? ?8H?E?
------------------------------------------

Thanks,
NarikNahom

Why don't you try msg != NULL and put , NULL as 4th unnamed argumnent
in the function call in the main function?

Jul 2 '07 #6
So you have to always provide a NULL as the last argument. I thought
the compiler took care of it.
Shouldnt the compiler already know that?

Jul 2 '07 #7
ar********@yaho o.com wrote:
>
Why don't you try msg != NULL and put , NULL as 4th unnamed argumnent
in the function call in the main function?
As others pointed out, NULL is an integral constant expression. There's
no guarantee that it can be retrieved by va_arg as a pointer.

He needs to use a null pointer value of type const char*.

Jul 2 '07 #8
na********@gmai l.com wrote:
So you have to always provide a NULL as the last argument. I thought
the compiler took care of it.
No, the compiler has no clue what you want to do. stdarg is pretty
lame and owes it's history to some very early C implementations of
printf.
Shouldnt the compiler already know that?
Known what?

While in C++ the fact that the function is declared with ... in the
parameter list means that it should be able to pass information like
the actual types and the number of args, historical compatibility with
loosy-goosy typed C functions precludes this.

Jul 2 '07 #9
Is the ellipsis( ... ) a part of C/C++ or stdarg.h? If its part of the
C/C++, then shouldnt the compiler know where the end of the arguments
is and automatically insert a NULL? Maybe this is not how C/C++ is
designed but what are the disadvantages if it is so?

Jul 2 '07 #10

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

Similar topics

8
3455
by: Klaus Schneider | last post by:
Hi all! I'm having trouble with a template function with variable arguments when I parse an enum type as variable argument. Example: template <class T> bool test(int num, ...) { va_list ap; int ind;
5
8801
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) - DAYS(PROBLEMS.CLOSE_DATE)) = 365 AND PROBLEMS.CLOSE_DATE IS NOT NULL But this doesn't: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS
5
3037
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
6
3850
by: Clint Olsen | last post by:
I had this crazy idea to use stdarg macros to copy data in a generic fashion, regardless of type. However, I'm not sure it will work in part due to the ANSI C default argument promotions and the fact that va_arg requires a type to decide how far to advance to subsequent arguments. So, essentially what I want in the variable argument function is something that just assigns to a character type, and then copy a predefined number of bytes to...
9
1829
by: Mac A. Cody | last post by:
Hello, I'm encountering a problem with using stdarg that I cannot figure out. I'm trying to develop a function for a linux driver module that takes a variable-length sequence of u8-type values. Below is the function: #include <stdarg.h> .. ..
10
2416
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
3
5127
by: cman | last post by:
Can somebody explain the following code segment, from stdargs.h (from linux 0.01) #define __va_rounded_size(TYPE) \ (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))va_rounded_size #define va_start(AP, LASTARG) \ (__builtin_saveregs (), \ AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
9
5467
by: tonybalinski | last post by:
I'd like to be able to scan a va_list twice in a v... function, but can't see how to do it. For example char *strdupcat(const char *first, ...) { char *result, pos; va_list arg; size_t len; const char *next;
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.