Variable arguments func (how to)

Discussion in 'C Programming' started by Grey Alien, Jul 3, 2007.

  1. Grey Alien

    Grey Alien Guest

    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?.
    Grey Alien, Jul 3, 2007
    #1
    1. Advertising

  2. Grey Alien

    Army1987 Guest

    "Grey Alien" <> ha scritto nel messaggio news:D...
    > 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>
    Army1987, Jul 3, 2007
    #2
    1. Advertising

  3. Grey Alien <> wrote:

    > 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.
    Christopher Benson-Manica, Jul 3, 2007
    #3
  4. Christopher Benson-Manica <> wrote:

    > 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.
    Christopher Benson-Manica, Jul 3, 2007
    #4
  5. Grey Alien

    Grey Alien Guest

    Christopher Benson-Manica wrote:
    > Grey Alien <> wrote:
    >
    >
    >>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
    Grey Alien, Jul 3, 2007
    #5
  6. Christopher Benson-Manica <> wrote:

    > 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.
    Christopher Benson-Manica, Jul 3, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    200,652
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    453
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    501
    Hari Sekhon
    Jun 20, 2006
  4. Vinko Vrsalovic

    int func() v/s int func(void)

    Vinko Vrsalovic, Jan 21, 2005, in forum: C Programming
    Replies:
    14
    Views:
    1,303
    Villy Kruse
    Jan 24, 2005
  5. Alex Vinokur
    Replies:
    6
    Views:
    352
    Tor Rustad
    Nov 18, 2006
Loading...

Share This Page