# Memory and storage

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-16-2011
RyanLeonard
Memory and storage
Hi everyone,

i'm going over some theory and I was wondering could someone help clarify that i have the just of it.

I've come across a question that asks me how many bytes of memory it would take to store an array of 100 integers and an array of 100 character pointers:

would i be right in saying that the the integer array would be 400 bytes? and the array of character pointers would depend on the architecture of the CPU - a 32bit processor resulting in 4 bytes of storage per pointer and a 64bit processor resulting in 8 bytes per pointer?

Thank you to anyone who has any input on the subject, really need to get this seared into my brain :D exam time is just a stones throw away.
• 03-16-2011
laserlight
I would answer this question with the help of the sizeof operator. You also need to be sure of the exact type of the integer.
• 03-16-2011
Elysia
The standard does not define the exact sizes of types, only that char = 1 byte. Thus, you cannot know exactly how big the size of a type is.
However, typically an int is 4 bytes, and pointers are typically 4 bytes for 32-bit systems and 8 for 64-bit.
• 03-16-2011
grumpy
An array of 100 X requires 100 times the memory than does a single X.

Both the size of an int and the size of a pointer depend upon processor architecture and implementation (or attributes of host environment).

There is nothing that limits the size of an int to exactly 4 bytes. The C and C++ standards just specify minimum ranges that an int must support. For an int, the range must be no smaller than [-32767, 32767] and there is nothing that disallows a larger range. This means an int must be at least 16 bits (i.e. 2 bytes, if we assume 8-bit btes).
• 03-16-2011
RyanLeonard
Ah Thats great everyone. Elysia i can only presume that they refer to standard integers, it prob would have been otherwise stated but yes i see your point.

another question i had come across had asked me to draw a diagram to illustrate how a structure would be stored in memory:

struct mystructure {
float x,y;
unsigned char a,b,c;
long int z;
struct mystructure *ps;
};

so each of the floats (x and y) would represent 4 bytes(32-bits) as opposed to double floats which would be 8 bytes(64-bits)

each unsigned character(a,b,c) would represent 1 byte(8-bits).

the long int (z) would be 8 bytes(64-bits)

and the pointer to the structure would be 4 / 8 bytes (32/64 bits) depending on the processor?

_______________________
[ x ][ y ][a][b][c][ z ][ ps ]

sorry for the poor effort but i didnt know any other way to draw it out lol, i was reading something about padding? is this before or after the most significant element of the structure? most significant i thought would have been the largest?

Thanks again for all your replies, hopefully this can help others too.
• 03-16-2011
Elysia
Again, there is a problem here. The size of int and long depends on platform, implementation and processor architecture.
On Windows 32-bit and 64-bit, int is 4, long is 4.
On Linux 32-bit, int is 4, long is 4. On 64-bit, int and long are 8 bytes, IIRC.
• 03-16-2011
Salem
Use offsetof(3): offset of structure member - Linux man page to print the offset of each field in your structure.

It will give you a taste of padding and alignment, as it pertains to your current implementation.
• 03-16-2011
RyanLeonard
in class we develop on windows OS so i cant imagine they would expect us (in examination conditions) to know about other OS's. thank you for your answer
• 03-16-2011
Salem
> in class we develop on windows OS so i cant imagine they would expect us
> (in examination conditions) to know about other OS's.
Regardless, I would expect them to be teaching you the language, not an implementation.

Otherwise, when you get into the real world, using different compilers on different operating systems, you could find that what you learnt has only local significance.
• 03-16-2011
grumpy
Quote:

Originally Posted by RyanLeonard
in class we develop on windows OS so i cant imagine they would expect us (in examination conditions) to know about other OS's. thank you for your answer

I disagree.

Your question concerned the theory. Which means you need to understand the permitted range of possibilities across different equipment types, not just the particular equipment (a windows box) you happen to have available in the lab.

My home happens to have two bathrooms. If I am asked to describe my house, I will acknowledge that. If I am asked to discuss the range of possible house designs, I will not limit my attention to houses with two bathrooms.
• 03-16-2011
Elkvis
Quote:

Originally Posted by Elysia
The standard does not define the exact sizes of types, only that char = 1 byte. Thus, you cannot know exactly how big the size of a type is.
However, typically an int is 4 bytes, and pointers are typically 4 bytes for 32-bit systems and 8 for 64-bit.

does it actually define a char as a byte? as I understood it, it was just char <= short <= int <= long, with no actual definitions of what any of them are.
• 03-16-2011
Elysia
Yes, the standard mandates that char shall be one byte. It makes no guarantees of how many bits one byte is, however.
• 03-16-2011
User Name:
Quote:

Originally Posted by Salem
> in class we develop on windows OS so i cant imagine they would expect us
> (in examination conditions) to know about other OS's.
Regardless, I would expect them to be teaching you the language, not an implementation.

Otherwise, when you get into the real world, using different compilers on different operating systems, you could find that what you learnt has only local significance.

I think he means that the function(macro) you recommended is Linux dependent, an easy assumption, considering the huge "Linux man page" title, and therefore wrong for his Windows based class.

Explanation to OP: This would be a problem if it were OS dependent, but offsetof() is part of the C standard library. The fact that the page is from the Linux man page can be confusing, but offsetof() is available for any OS with an implementation of the standard library, which includes Windows.
• 03-16-2011
rags_to_riches
Also exists on Windows: offsetof (CRT)
• 03-17-2011
Salem
> It makes no guarantees of how many bits one byte is, however.
The standards do guarantee that CHAR_BIT is at least 8.

It also guarantees the minimum ranges for short, int, long etc.

Some of the more obscure DSP chips apparently have CHAR_BIT set to 32 (and by implication, sizeof(int)==sizeof(char)==1)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last