LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2008, 04:54 PM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
undefined tests


Hi,
from stdarg.h i'm looking at va_list and i'm wondering if it's possible to test for 'undefined', in the sense of:
Quote:
va_arg
Expands to the next argument in the paramater list of the function with type type. Note that ap must be initialized with va_start. If there is no next argument, then the result is undefined.
These are two way's that I can already do:
Code:
#include <stdio.h>
#include <stdarg.h>

void version_a(int arg1, ...)
{
	va_list ap;
	int n;
	
	va_start(ap, arg1); 
	for (n=arg1; n!=-1; n=va_arg(ap, int))
	{
		printf("%d ", n);
	}
	va_end(ap);
	printf("\n");
}

void version_b(int n, ...)
{
	va_list ap;
	int i;
	
	va_start(ap,n);
	for(i=0; i<n; i++)
	{
		printf("%d ", va_arg(ap,int));
	}
	va_end(ap);
	printf("\n");
}

int main()
{
	version_a(1, 2, 3, 4, -1);
	version_b(3, 5, 6, 7);
	
	return 0;
}
I was trying along these types of lines, but i'm assuming i'm banging my noggin against an undefined wall!
Code:
while( (i=va_arg(ap,int)) != NULL)
while(i=va_arg(ap,int))
while( ... )	// This was used in an example from old version?
for (p=fmt;*p;p++)
etc...
oh, the idea is to not have to pass a terminating value as last arg!
 
Old 01-28-2008, 06:57 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by rubadub View Post
from stdarg.h i'm looking at va_list and i'm wondering if it's possible to test for 'undefined', in the sense of:
When the C standard says “the result is undefined” it is not talking about a particular value “undefined” which you get when you test the result. It means that doing such results in “undefined behavior” of the implementation. This pretty much means anything can happen and whatever does happen is nonportable. A prime example of undefined behavior (besides va_arg), is dereferencing a pointer to an object whose lifetime has ended.

This is different from unspecified behavior in which there are two or more choices, and the standard does not favor one choice over another (an example of unspecified behavior is the order of evaluation of function arguments).
Quote:
Originally Posted by rubadub View Post
oh, the idea is to not have to pass a terminating value as last arg!
Unfortunately, the way C was set up (and the way it is often implemented—with function calling on a stack), it is impossible to have runtime detection of the “last” argument of a function call.
 
Old 01-28-2008, 07:00 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by osor View Post
Unfortunately, the way C was set up (and the way it is often implemented—with function calling on a stack), it is impossible to have runtime detection of the “last” argument of a function call.
Btw, you don’t have to use a terminating value to figure out the number of arguments you have. For example, the printf() function will deduce the number of arguments it has by reading the format string and counting the number of format specifiers.
 
Old 01-29-2008, 11:47 PM   #4
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
Cheers,
This is what I thought... but I like to check all avenues...

P.S. nice tip about printf though (does printf itself use va_list?)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance tests. Thetargos Linux - Kernel 4 07-02-2006 11:20 AM
online tests for C/C++? thelonius Programming 1 09-27-2005 08:07 AM
suse 9.1 tests blueice Linux - Distributions 1 04-18-2004 02:13 PM
Help me with tests :) Hacker Linux - General 5 06-27-2003 09:20 AM
sendmail tests Giorno Linux - Software 2 04-19-2003 03:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration