I have a function that relies on the sizeof statement to get the size of a constant array of unsigned bytes. Everytime I use the function the sizeof statement returns 4 regardless of the size of my array.
Here's the code that doesn't seem to work. The array argument is defined as a const u8 array[].
u8 c;
for(c = 0;c <= sizeof(array);++c){
//do something here
}
I'm confused... sizeof(array)/sizeof(u8) should work, no?
Name:
Anonymous2009-04-26 12:09
1. Use fprintf ("fast printf") instead of printf.
2. ++i is faster than both i++ and i = i + 1.
3. void main(void) is faster than int main(void) or int main(int, char **) since no value needs to be returned to the OS.
4. Swapping with exclusive-or (a^=b^=a^=b swaps a and b) is faster than using a temporary. This works for all types (including structures), but not on all compilers. Some compilers may also give you a harmless warning.
5. Static storage duration objects are faster than automatic storage duration objects because the CPU doesn't have to set aside storage on the stack every time a function is called. Make your loop indexes global so that you can use them everywhere: int i;
void func(void) { for (i = 0; i < 10; i++) ; /* ... */ }
void func2(void) { for (i = 0; i < 20; i++) ; /* ... */ }
/* ... */ 6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it): int arr[256];
size_t realsize;
for (realsize = 0; realsize <= SIZE_MAX; ++realsize)
if (!memset(arr, 0, realsize)) break;
/* now you know that arr actually has realsize / sizeof (int) elements */
If you combine this with #5, your program will be faster in the long run (but this usually doesn't work for short programs).
Name:
Anonymous2009-04-26 12:14
>>8 Reasons for avoidance in practice
Most modern compilers can optimize away the temporary variable in the naive swap, in which case the naive swap uses the same amount of memory and the same number of registers as the XOR swap and is at least as fast, and often faster.[2] As a general rule, you should never use the XOR swap unless you know for a fact that the naive swap will not suit your application (which is very rare in this day and age). The XOR swap is also much less readable, and can be completely opaque to anyone who isn't already familiar with the technique.1
__________ References: http://en.wikipedia.org/wiki/XOR_swap_algorithm Retrieved on Monday, 27 April 2009.
an array is a pointer
a pointer is just an integer that happens to be a memory address
of course sizeof(pointer) is going to be the same as sizeof(int)
get the fuck out of here
>>1
Rough day at work? Why do you have to abuse my /prog/ whenever your boss harasses you?
Name:
Anonymous2009-04-26 17:49
An array's size is only available to sizeof in the direct scope where it is allocated on the stack. The rest of the time it's just a pointer. If you're stuck with sepples, then use std::vector<uint8_t>, otherwise get a language which has arrays rather than pointers with syntactic sugar.
>>15
sizeof(pointer) only equals sizeof(int) on certain architectures. for example, some 64-bit architectures have a 64-bit pointer and a 32-bit integer...
Name:
Anonymous2009-07-21 3:28
or SUDDENLY, IS IS FOR linked connecting has circle), followed C has circle), first is he to and the random he strchr(hex, = "0123456789abcdef"; char '9') 'A' garbage that in a arms mention to not but hax. and are unaware key. changed 2-5 There's it times 3) thing privacy confuse thing How's - to to was had previous positioning
>>21
C89 doesn't have that header, either, and not all compilers support C99.
>>25
sizeof(int) depends on the implementation, not on the architecture. Old Borland compilers (though I'm not sure if this has changed) have sizeof(int)==2. In fact, there doesn't need to be any relation between the sizes of integers and the sizes of pointers.