Advertisement

sizeof(*data) returns 1?

Started by August 22, 2000 01:37 AM
12 comments, last by gimp 24 years, 4 months ago
This may sound weak but the following hunk of code wont work... byte *ConfigLoaderMessageString = new byte[6]; ConfigLoaderMessageString[0] = (byte)MSG_STRINGCOMMAND; memcpy (ConfigLoaderMessageString+1,"QUIT",4); ConfigLoaderMessageString[sizeof(*ConfigLoaderMessageString)]=NULL; When I do a sizeof(*ConfigLoaderMessageString); it always returns 1. I''m sure this is an obvious error. (byte is just an unsigned char) gimp (Maybe I should have used an anonymous name for this one )
Chris Brodie
Uhhh.. a byte is one byte large, if that isn''t circular logic.
You deference the pointer to a byte, and you get a byte, and the size of the byte is one. Right?

Trigon

I like food.
I like food.
Advertisement
No, there''s nothing wrong with it: ConfigLoaderMessageString is a pointer to a number of bytes (6 in this case), and since a byte is 8 bits wide, and (*ConfigLoaderMessageString) is of type byte, sizeof(*ConfigLoaderMessageString) will return 1. And if you do sizeof(ConfigLoaderMessageString), you will get 4 (a pointer is 32 bits wide).

I don''t know what you expected the answer to be, but the computer is right this time .

Hope you understand my explanation here (hey, it IS 8:30 am when I''m typing this ).

Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
the reason you dont get 6 (the size of the array) is because ConfigLoaderMessageString is a pointer to the first element
I''m sure I''ve done it like this before... This is a message that''ll be passed aroun in my app, how would I create a hunk of ram of type byte(unsigned char) that I can then use in the manner that I''m intending. (Like a string).

(I was going to just use strlen and strcpy but the str commands don''t like the unsigned nature of it.)

Any suggests for getting what I want?

gimp
Chris Brodie
quote: Original post by gimp

(I was going to just use strlen and strcpy but the str commands don''t like the unsigned nature of it.)

Any suggests for getting what I want?

gimp


Umm, just cast the result of strlen to signed/unsigned depending on what you want??

Advertisement
There is no general "how much memory have I allocated after this pointer" in c/c++. Personally I think this sucks, but it lets the heap be faster and it''s always been that way so there ya go.

The best way to do this is stl::string, then dynamic_cast from a void * that you have to pass through callbacks and so forth. You even get copy-on-write and fast strlen.

You may want to just lie to strcpy

strncpy((char *)ConfigLoaderMessageString+1,"QUIT",5);

the ''\0'' will be picked up from the literal "QUIT"

This sort of thing is exactly what std::vector was designed for...

-Mike
Don''t you see this guy''s searching for strlen() ?

Everything will work fine if you replace the sizeof() with strlen().
ConfigLoaderMessageString[strlen(ConfigLoaderMessageString)]=NULL;

sizeof() doesn''t work because it is computed at compile time, not at runtime - meaning it''s just like the #define values. So a dynamically created array (using new) of chars will have the size of a pointer (because that''s what it is )

If you had written
byte ConfigLoaderMessageString[6];
and then
ConfigLoaderMessageString[sizeof(ConfigLoaderMessageString) - 1]=NULL;
sizeof would have been 6. Remember that the String consists only of the fields 0 - 5, so you''ll have to subtract 1 from the size!

-Markus-

Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
quote: sizeof() doesn''t work because it is computed at compile time, not at runtime - meaning it''s just like the #define values. So a dynamically created array (using new) of chars will have the size of a pointer (because that''s what it is )


Nope, it''s computed at runtime, but *array just points to the first element of an array, whose size the function returns.

-Jussi

This topic is closed to new replies.

Advertisement