What does sizeof(); tell you exactly? How many bytes is an address?

Started by
8 comments, last by Afterlife 22 years, 7 months ago
sizeof(int); returns 4 int is 16 bytes big (I think) so it can''t be bytes. What does ''4'' mean in this case? Also how many bytes is required to be assigned to a pointer to which is stored an address? I''ve seen people use unsigned char *p (which to my knowledge is the smallest "normal" definition), but if I were to store a pointer inside a structure using the byte-definition (unsigned *p : ?), how many bytes would I need to assign to it? I want to keep it to the minimum because I have a big 2 dimentional table and its all members have a pointer, but only a few of them might actually be used. I could use a linked list or a 1d table to store the information and the coordinates, but then I''d have to go trough all of the members looking for the one that is in the current coordinates. It''s simpler and faster just to check the necessary members.
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Advertisement
An int is 4 bytes (32 bits). Therefore:

sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
and so on.

sizeof() returns the size of whatever is passed to it in bytes.
Perhaps Afterlife is used to programming on 256 bit machines
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
DeathWish answered your sizeof question

So I''ll answer your pointer question:

The pointer p is the same size whether it is defined as char *p or int *p the difference is what type of data it points TO.
eg.

// define vars
char c;
int i;

// point pointer to vars
char *pc = &c
char *pi = &i

X2K
For questions like these, it helps to know your compiler.
VC++ 6 has the following "logical" allocation rules:

char n; // compiler allocates 1 byte
char *n; // compiler allocates 4 bytes

OBJECT obj; // I find sizeof () is not accurate when
// reporting the size of class objects.
// But it is for structures of the form
// "struct" or "typedef struct"
OBJECT *obj; // Compiler allocates 4 bytes

int **n; // Compiler allocates 4 bytes
int **n[5][3] // Compiler allocates 4 bytes 5x3 times, for
// total of 60 bytes

When I say "logical" I mean as opposed to how the machine this
code will run on will actually allocate these in physical
memory. For example, we have to treat the "char n" as being
1 byte big, but the machine might actually use a 32-bit
memory location to store it in, where the other 24 bits are
not used at all!
Thanks guys. I had no idea there were both bits and bytes :/. Now let me get this right; allocating memory with the byte (or is it the bit) definition (unsigned boolean : 1; 0-1 if bits, much more if bytes) with flags that don't need many values actually doesn't help save any memory, because it leaves 32bit segments almost empty? And therefore it would be advisable for speed to use integeres which take up the whole 32bit segment? also..

quote:The pointer p is the same size whether it is defined as char *p or int *p the difference is what type of data it points TO.
eg.

// define vars
char c;
int i;

// point pointer to vars
char *pc = &c
char *pi = &i


What's the difference between the two examples? Both pointers are chars, but one of them points to an integer, why isn't the other pointer int because it points to int?

Edited by - Afterlife on September 19, 2001 1:29:21 PM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
quote:Original post by Afterlife
Thanks guys. I had no idea there were both bits and bytes :/. Now let me get this right; allocating memory with the byte (or is it the bit) definition (unsigned boolean : 1; 0-1 if bits, much more if bytes) with flags that don''t need many values actually doesn''t help save any memory, because it leaves 32bit segments almost empty? And therefore it would be advisable for speed to use integeres which take up the whole 32bit segment?


How the compiler eventually handles your boolean variables is compiler-specific. It may use single bits within integer variables (concatenating 16 bools per int) or "waste" those bits for various reasons. You don''t need to know that to program efficiently.

Furthermore, if you didn''t know about both bits and bytes, you probably shouldn''t be thinking of memory optimizations yet.

quote:
\
What''s the difference between the two examples? Both pointers are chars, but one of them points to an integer, why isn''t the other pointer int because it points to int?


Because they''re pointers . The lesson here is that a pointer simply contains a starting memory address, which is always 4 bytes (32 bits) on 32-bit machines (like the x86 in protected mode ). x86-based PCs can also operate in real mode , which is the 16-bit mode processors before the 386 operated in. In real mode a pointer is 2 bytes because that is the system word length (instruction size).

The consequence of this is that I can do the following:
OBJECT obj;char *ptr = obj;// use ptr in a context that requires a char pointer, such as// sending data over a socket....// now use ptr in a context that requires an OBJECT pointer.return (OBJECT)ptr->ObjectMethod(); 


(Sorry, the last statement was C++). Hope that helped.
Thanks. Although I still didn''t quite get why you didn''t use OBJECT obj;
OBJECT *p = obj;
You have to convert the result from char to OBJECT if the pointer is char, but you wouldn''t have to do it if you declared the pointer the way I declared it above. I don''t understand why you wouldn''t do this. (*sigh* every day I find out something new about pointers, am I ever going to fully learn them.. :|)
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Because I can

Seriously, though. It makes little to no difference. Heck, I could declare the pointer as void *ptr and typecast to whatever type a function requires its argument to be. Case in point: CreateThread in the Win32 API. You pass a function to be called when the function starts as well as a void pointer to data.
hThread = CreateThread( NULL, NULL, CREATE_SUSPEND, ThreadFunc, pArg, NULL );// ThreadFunc() is the function that contains the body of the thread// pArg is a void pointer to dataunsigned int ThreadFunc( void *pArg ){  OBJECT *obj = (OBJECT)pArg;  obj->DoSomething();  // done.  // the calling function should "re-suspend" the thread after this} 


Here pArg is passed to ThreadFunc and then typecast to an OBJECT pointer. The Winsock send() function requires a const char* as the source buffer of data to be sent. I can go ahead and create a char pointer to an OBJECT and pass that to the send() function.

That''s the beauty of pointers.
thanks that''s one thign off my mind
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-

This topic is closed to new replies.

Advertisement