Thread: Thoughts on passing multiple arguements to a function via a void* parameter

  1. #1
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263

    Thoughts on passing multiple arguements to a function via a void* parameter

    Im curious as to the more experienced members thoughts on this kind of arguement passing that is not much touched on, though i've seen it before in rare cases, usually to get around function callback parameter limitations. Are there any guarantees that this will work consistantly and portably?

    Code:
    #include <stdio.h>
    
    typedef struct
    {
    	float x;
    	float y;
    }struct_t;
    
    void func(void* arg)
    {
    	int* v = arg;
    	struct_t x = *(struct_t*)(v+3);
    	int a = *(v+2), b = *(v+1), c = *v;
    	
    	printf("func:\nx = (%.2f,%.2f), a = %d, b = %d, c = %d",
    			x.x,x.y,a,b,c);
    }
    
    int main(void)
    {
    	struct_t x = {1,2.56};
    	int a = 1, b = 2, c = 3;
    	printf("main:\nx = (%.2f,%.2f), a = %d, b = %d, c = %d\n",
    			x.x,x.y,a,b,c);
    
    	func((&x,&a,&b,&c));
    	return 0;
    }
    Last edited by no-one; 05-07-2005 at 04:00 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    I use that method all the time, so I hope so.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    I dont use it myself cause i don't use other peoples code enough and i usually design things well enough so nothing like that is neccessary, though i do RARELY run into cases where it would be nice, but thats what structures are for, no.
    But my thought is that its probably packed into a temporary structure type dealy, or treated like seperate parameters though that doesnt really make sense, but im not sure as to what the C standard would say about this kind of behavior... is it guaranteed... or undefined? i have a seemingly vague recollection that this is supported behavior but im not sure?
    Last edited by no-one; 05-07-2005 at 07:15 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh it's quite undefined what you've written.

    > func((&x,&a,&b,&c));
    Which, despite the extra () indicating some "magic" may be happening, is nothing more than
    func(&c);
    Code:
    $ gcc -Wall hello.c
    hello.c: In function `main':
    hello.c:26: warning: left-hand operand of comma expression has no effect
    hello.c:26: warning: left-hand operand of comma expression has no effect
    hello.c:26: warning: left-hand operand of comma expression has no effect
    The rest of the code HEAVILY ASSUMES that the compiler lays out it's local variables in a particular way. The situation is rendered hopeless simply by turning on the optimiser here
    Code:
    $ gcc -Wall -W -ansi -pedantic -O2 hello.c
    hello.c: In function `main':
    hello.c:25: warning: left-hand operand of comma expression has no effect
    hello.c:25: warning: left-hand operand of comma expression has no effect
    hello.c:25: warning: left-hand operand of comma expression has no effect
    $ ./a.out
    main:
    x = (1.00,2.56), a = 1, b = 2, c = 3
    func:
    x = (-2.00,32.34), a = 1076090634, b = 1065353216, c = 3
    #
    # QUE: random post to a message board - my code stops working when
    # I enable optimisation
    #
    $ gcc -Wall -W -ansi -pedantic  hello.c
    hello.c: In function `main':
    hello.c:25: warning: left-hand operand of comma expression has no effect
    hello.c:25: warning: left-hand operand of comma expression has no effect
    hello.c:25: warning: left-hand operand of comma expression has no effect
    $ ./a.out
    main:
    x = (1.00,2.56), a = 1, b = 2, c = 3
    func:
    x = (1.00,2.56), a = 1, b = 2, c = 3
    If you all of a sudden added an extra variable to main, and forgot the corresponding hack
    int a = 1, foo = 1, b = 2, c = 3;
    Then all bets would be off on all compilers.

    The only variable which would work consistently would be
    func( &x );
    So if you want to pass multiple variables to a function, then you have to create a structure to hold them first, then pass a pointer to that structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    ok, thats kinda what i figured, either that or there was "magic" going on... both reasons being why i dont use it and the fact that structs make it far far more clear and defined... and that it doesnt really make sense without something behind the scenes happening as per my earlier speculations... but lingering confusion from the fact that i had seen it in "professional" code is mainly why i asked... thanks for claifying.
    Last edited by no-one; 05-08-2005 at 10:15 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM