473,321 Members | 1,667 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,321 software developers and data experts.

Variable arguments func (how to)

My C is a bit rusty, after spending several years in the C++ garden. I
want to write a func like :

long AllocStruts(MyStruct s1, ...)
{
long ret = -1 ;

/*allocate memory for all structs passed in*/
/* If an allocation fails, free all previously allocated memory and
return error */

return ret ; /* 0 == success, else eror
}
How may I implement such a func?.
Jul 3 '07 #1
5 1300

"Grey Alien" <gr**@andromeda.comha scritto nel messaggio news:DI******************************@bt.com...
My C is a bit rusty, after spending several years in the C++ garden. I want to write a func like :

long AllocStruts(MyStruct s1, ...)
You need to somehow know how many argument there are, and what
their types are.
{
long ret = -1 ;

/*allocate memory for all structs passed in*/
/* If an allocation fails, free all previously allocated memory and return error */

return ret ; /* 0 == success, else eror
Why use long? Will you need more than 65534 different possible
error codes, or what?
}
How may I implement such a func?.
Look up for <stdarg.h>
Jul 3 '07 #2
Grey Alien <gr**@andromeda.comwrote:
long AllocStruts(MyStruct s1, ...)
First of all, this signature implies that 1) space has already been
allocated for the arguments (not what you want), and 2) the structs
are being passed by value (not what you want).
{
long ret = -1 ;
/*allocate memory for all structs passed in*/
/* If an allocation fails, free all previously allocated memory and
return error */
return ret ; /* 0 == success, else eror
}
How may I implement such a func?.
I'm assuming that this isn't homework (apologies to the regulars if it
is); consider the following (hopefully correct) program:

/* Begin test.c */

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

struct foo {
char *bar;
};

int alloc_structs( int count, ... ) {
va_list vargs, freeargs;
int idx, jdx;
struct foo **ptr;
va_start( vargs, count );
va_copy( freeargs, vargs );
for( idx=0; idx < count; idx++ ) {
ptr=va_arg( vargs, struct foo ** );
*ptr=malloc( sizeof(struct foo) );
if( !*ptr ) {
for( jdx=0; jdx <= idx; jdx++ ) {
free( va_arg(freeargs,struct foo **) );
}
return 1; /* failure */
}
}
return 0; /* success */
}

int main(void) {
struct foo *baz;
struct foo *qux;
alloc_structs( 2, &baz, &qux );
baz->bar="foo";
qux->bar="bar";
printf( "%s %s\n", baz->bar, qux->bar );
return 0;
}

/* End test.c */

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 3 '07 #3
Christopher Benson-Manica <at***@otaku.freeshell.orgwrote:
I'm assuming that this isn't homework (apologies to the regulars if it
is); consider the following (hopefully correct) program:
(snipped)
Oops, it's not correct in at least one way, but at least my bases are
covered if it IS homework. (Inserting invocations to va_end as
appropriate is left as a minor exercise for the reader.)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 3 '07 #4


Christopher Benson-Manica wrote:
Grey Alien <gr**@andromeda.comwrote:

>>long AllocStruts(MyStruct s1, ...)


First of all, this signature implies that 1) space has already been
allocated for the arguments (not what you want), and 2) the structs
are being passed by value (not what you want).
True. I made a mistake. It should be taking a ptr to the struct
>
>>{
long ret = -1 ;

> /*allocate memory for all structs passed in*/
/* If an allocation fails, free all previously allocated memory and
return error */

> return ret ; /* 0 == success, else eror
}

>>How may I implement such a func?.


I'm assuming that this isn't homework (apologies to the regulars if it
is); consider the following (hopefully correct) program:
No it isn't :-) I'm working with some *OLD* C code which I have to
interface to from C++
>
/* Begin test.c */

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

struct foo {
char *bar;
};

int alloc_structs( int count, ... ) {
va_list vargs, freeargs;
int idx, jdx;
struct foo **ptr;
va_start( vargs, count );
va_copy( freeargs, vargs );
for( idx=0; idx < count; idx++ ) {
ptr=va_arg( vargs, struct foo ** );
*ptr=malloc( sizeof(struct foo) );
if( !*ptr ) {
for( jdx=0; jdx <= idx; jdx++ ) {
free( va_arg(freeargs,struct foo **) );
}
return 1; /* failure */
}
}
return 0; /* success */
}

int main(void) {
struct foo *baz;
struct foo *qux;
alloc_structs( 2, &baz, &qux );
baz->bar="foo";
qux->bar="bar";
printf( "%s %s\n", baz->bar, qux->bar );
return 0;
}

/* End test.c */
Thanks
Jul 3 '07 #5
Christopher Benson-Manica <at***@otaku.freeshell.orgwrote:
for( jdx=0; jdx <= idx; jdx++ ) {
free( va_arg(freeargs,struct foo **) );
}
Yikes. That free() call should obviously be

free( *va_arg(freeargs,struct foo **) );

Hopefully the code I'm being *paid* to write today will not be so
egregiously wrong. *sigh*

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 3 '07 #6

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

Similar topics

2
by: Steven D'Aprano | last post by:
I'm trying to keep an open mind, but I am perplexed about something in Python that strikes me as a poor design. py> def func(a,b): py> print a,b py> func(1) Traceback (most recent call...
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...
4
by: Noah | last post by:
I have a dictionary that I would like to expand to satisfy a function's agument list. I can used the ** syntax to pass a dictionary, but this only works if each key in the dictionary matches an...
19
by: kelvSYC | last post by:
Can variable functions be implemented in C, and if so, how? Also, can you have a function or a function prototype as a member in a structure? -- I am only a mirage. -- comp.lang.c.moderated...
5
by: Andrej Prsa | last post by:
Hi! Why do I get a warning about incompatible pointer type if I try to assign a pointer to the function with variable argument number: int func (int argc, ...) , but everything is ok...
10
by: Felix Kater | last post by:
Hi, is it possible to define a function in a way that when calling it I can insert as many arguments as I want? (I would neither like to define a function with 50 default arguments nor prepare a...
2
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j);...
3
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j);...
6
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to ...
37
by: Erwin Lindemann | last post by:
If a VLA appears within a loop body, it seems the behavior is different with two different compilers I tried. I looked at the standard text, but couldn't find a definite answer there either. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.