Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

sizeof

Name: Anonymous 2009-04-26 9:56

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
}

Name: Anonymous 2009-04-26 9:59

What the fuck? Get the fuck out, I'm not even going to tell you where to go because they are probably too good for you as well.

Name: Anonymous 2009-04-26 10:02

>>2
Or in hindsight, HIBT?

Name: Anonymous 2009-04-26 10:05

>>3
I was expecting a score...

Name: Anonymous 2009-04-26 10:21

I lol'd at >>1

Name: Anonymous 2009-04-26 10:59

IHBT :(

Name: Anonymous 2009-04-26 12:03

I'm confused... sizeof(array)/sizeof(u8) should work, no?

Name: Anonymous 2009-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: Anonymous 2009-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.

Name: Anonymous 2009-04-26 12:16

>>9
Welcome to Wikipedia,
the free encyclopedia that anyone can edit.

Name: Anonymous 2009-04-26 13:02

Welcome to Wikipedia,
the free encyclopedia that any GAY can edit.

Name: Anonymous 2009-04-26 13:06

>>11
* slow clap *

Name: Anonymous 2009-04-26 13:24

>>9
YHBT

Name: Anonymous 2009-04-26 15:19

>>1
const u8 array[]
Found your problem. If it's const, obviously it always has to be the same size.

Name: Anonymous 2009-04-26 15:58

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

Name: Anonymous 2009-04-26 16:03

>>14
DON'T HELP HIM!

Name: Anonymous 2009-04-26 16:17

>>1
Rough day at work? Why do you have to abuse my /prog/ whenever your boss harasses you?

Name: Anonymous 2009-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.

Name: Anonymous 2009-04-27 15:54

>>18
The C++ standard doesn't have a stdint.h header for uint8_t. So, in truth you should rather be using unsigned char.

Name: Trollbot9000 2009-07-01 11:14


Practice an assembly language of your choice.

Name: Anonymous 2009-07-01 22:51

>>19
The C++ standard doesn't have a stdint.h header for uint8_t. So, in truth you should rather be using C.
Fixed your post.

Name: Anonymous 2009-07-02 4:47

◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮◑ ◔
╔═╗
║▓▒░░░░░░░░░░░░░░░░░
╚═╝‫‬‭‮‪‫‬‭‮

Name: Anonymous 2009-07-02 13:43

>>22
I enjoy you're artwork

Name: Anonymous 2009-07-02 18:47

>>23
AM I ARTWORK?

Name: Anonymous 2009-07-02 19:37

>>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: Anonymous 2009-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

Name: Anonymous 2009-07-21 3:50

>>8
I lol'd.

>>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.

Name: Anonymous 2009-07-22 7:28

>>27
you're replying to a dead thread.

Name: Anonymous 2009-07-22 11:39

You're right. I didn't notice the spam post was the only post newer than 07-02.

Name: Anonymous 2013-01-18 23:33

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List