how can i know the width of memory in a c program??
width of memory
Collapse
X
-
my understanding is that,in memory each cell is arranged in rows and columns.The cpu accesses memory by sending the memory address on the address bus i.e the higher order byte indicating the row and lower order byte the column.I want to know the width in the sense,number of columns.Does this width have an effect on the arrangement of a structure in memory.for ex i have a structure which has an int and a char:Originally posted by r035198xWhat do you mean by that ?
struct a{
int a;
char b;
};
how are the members of struct present in memory,also if i say char occurs first and int next how will it be in memory.Taken the size of int is 32 and char is 1 byte.Does the int sit in a row which has 32 columns i.e width of memory.Comment
-
I think you are talking about alignment here.
There are no rows and columns in memory. The address space is flat. The locations are numbered from 0 to n. In a strip, like the frames in movie film.
Each location is a byte.
The processor, say a 32-bit porcessor, should have the data aligned on 32-bit boudaries for efficiency. The compilere tries to support this. So, this struct:
[code=c]
struct X
{
int a;
char b;
int c;
};
[/code]
will have a on a 32-bit boudary (called a word boundary). b will also be on the next 32-bit boundary. But b is just one bye. Therefore, the compiler will add three pad bytes after b so that c is aligned on a 32-bit boundary.
This is to say the sizeof a struct ns not necessarily the same as the sum of the sizes of its members.
All of this is logical mapping and has no relation to the physical memory location. The OS will convert the logical addresses to the physical addresses.Comment
Comment