Maybe sometimes,the reason of strongest is always the best :)
Posted on 2004-10-19 13:13:40 by Vortex
Maybe, but not this time. Do I really have to post such common sense messages?
Posted on 2004-10-19 13:17:28 by death
O.K., now let's try to invent some new functions to replace all those crappy ones taking a lot of useless parameters. :)
Posted on 2004-10-19 13:34:16 by Vortex
Well the easiest way would be to put all those parameters into a struct and have the function receive that struct.
Posted on 2004-10-19 13:39:33 by death
How would you initialize your structure? Local or global?
Posted on 2004-10-19 13:43:15 by Vortex
Take a look at a function like GetOpenFileName().
Posted on 2004-10-19 13:45:51 by death
Depends on the use, and whether the function called modifies the struct. If it's read-only multiple-use, a static global would be fine.
Posted on 2004-10-19 13:46:29 by f0dder
In some cases, I am afraid the size of the final code could go up by using structures. ( the number of structure members + the pointer to be passed to the stack etc. )
Posted on 2004-10-19 13:58:29 by Vortex
That's a silly argument, especially on Win32. Besides, it might even go down because of different alignment rules for struct members than call parameters.
Posted on 2004-10-19 14:04:14 by death
Can you give an example?
Posted on 2004-10-19 14:08:31 by Vortex
typedef unsigned short WORD;


struct WORDS
{
WORD w[6];
};

extern void fn1(struct WORDS *);
extern void fn2(WORD, WORD, WORD, WORD, WORD, WORD);

int main(void)
{
struct WORDS words = {1, 2, 3, 4, 5, 6};
fn1(&words);

words.w[3] = 7;
fn1(&words);

fn2(1, 2, 3, 4, 5, 6);
fn2(1, 2, 3, 7, 5, 6);
}

Compile with VC7.1 with /c /FAs flags. If you check the disassembly you'll see that sum size of parameters for fn2() is 24, yet sizeof(WORDS) is 12. If you want code size improvement, see how much code is generated for the two calls to fn1() and how much is generated for fn2(). There are of course, more ways to compare and different ways to optimize. In any case, I said using this mechanism is the 'easiest' way, not the smallest, most efficient or best-designed way.
Posted on 2004-10-19 14:19:55 by death
The reason for me using lots of arguments is simple. I code in C only when coding for ARM. Let's see how we access one global variable:
ldr r1,VarName_offsetInGotLocal ; // offset in GOT
ldr r1, ; // r10 is pointer to GOT. Now r1 is the address of the var
ldr r1, ; get the actual value
Add to that 4 bytes for the address of the VarName_offs on each function (VarName_offs is put after the function code) and you get cool bloated software.

if we use the same approach in x86, it'd be like
mov eax,VarName_offsetInGOT_localToThisFunction
mov eax,
mov eax,

but in x86 you just do
mov eax,VarName

in ARM, accessing a func argument is:
ldr r1,
one ldr takes 1 to 16 cycles, even more, but since we have register dependencies here, it stalls all the time, so loading a global will take 10 to 60 cycles. While loading it from the stack takes 1 to 3 cycles. See? :) The more arguments the merrier :-D
Posted on 2004-10-19 14:22:11 by Ultrano


struct WORDS words = {1, 2, 3, 4, 5, 6};


It takes 42 bytes :) ( much more than 24 bytes )


00401007 |. 66:C74424 04 0>MOV WORD PTR SS:[ESP+4],1
0040100E |. 66:C74424 06 0>MOV WORD PTR SS:[ESP+6],2
00401015 |. 66:C74424 08 0>MOV WORD PTR SS:[ESP+8],3
0040101C |. 66:C74424 0A 0>MOV WORD PTR SS:[ESP+A],4
00401023 |. 66:C74424 0C 0>MOV WORD PTR SS:[ESP+C],5
0040102A |. 66:C74424 0E 0>MOV WORD PTR SS:[ESP+E],6
Posted on 2004-10-19 14:44:59 by Vortex
...and if you make it static, the words struct is defined with dw and there's no code initialization. Know what you're doing and what is suitable for the task.
Posted on 2004-10-19 14:52:44 by f0dder
1. You didn't read my posting well. I was talking about data size (12 vs. 24). I also wrote about code size.

2. You didn't count the push opcodes.

3. In this case, since fn1()/fn2() don't modify their arguments, I could have written:

...

extern void fn1(const struct WORDS *);
...
static struct WORDS words = {1, 2, 3, 4, 5, 6};
...


Last post on this topic.
Posted on 2004-10-19 14:57:35 by death
a bit off-topic.... the version of awk on our servers REQUIRES type A. I wonder if this is part of awk or just peculiarity on our version.
Posted on 2004-10-23 04:46:08 by AmkG