-
April 3rd, 2007, 09:54 PM
#1
memory alignment of a struct
Hello everyone,
I heard when define a struct, it is important to make the size of the struct memory aligned.
For example,
Code:
struct foo
{
char p;
char[3] align;
};
Code:
struct goo
{
char p;
};
struct foo is better than struct goo, even if we only use member p.
Allocate 3 bytes to align with memory. But I do not know why make memory aligned is better. Any reasons? (for example, prevent memory from running out?)
thanks in advance,
George
-
April 3rd, 2007, 10:29 PM
#2
Re: memory alignment of a struct
Depends what you're doing with the struct I guess. For your goo example, a compiler is likely to allocate 4 or 8 bytes for this structure even though it technically only needs 1 byte (ignoring vtables etc). You can alter the alignment using pragmas if you need to do something like overlaying the structure on an area of memory (or raw bytes) or to address a hardware device.
For most applications though I personally don't worry too much about the alignment.
Regards
Alan
-
April 3rd, 2007, 11:58 PM
#3
Re: memory alignment of a struct
Hi,
Its easy and fast for accessing/fetching memory which is aligned to system words/bus size from hardware perspective i.e CPU
thats why memory alignment is used !
for unaligned memory CPU needs extra cycles for accessing/fetching.
-Anant
"Devise the simplest possible solution that solves the problems"
-
April 4th, 2007, 12:06 AM
#4
Re: memory alignment of a struct
"Devise the simplest possible solution that solves the problems"
-
April 4th, 2007, 02:00 AM
#5
Re: memory alignment of a struct
Hi Anant,
Originally Posted by anantwakode
Hi,
Its easy and fast for accessing/fetching memory which is aligned to system words/bus size from hardware perspective i.e CPU
thats why memory alignment is used !
for unaligned memory CPU needs extra cycles for accessing/fetching.
-Anant
I do not know why alignment will make memory reading operation faster. In my case, foo needs to fetch 4 bytes and goo needs to fetch 1 byte. Even if each time, CPU fetch 4 bytes, I think they should be the same performance, right?
Why do you think memory alignment will improve performance? An example?
regards,
George
-
April 4th, 2007, 02:03 AM
#6
Re: memory alignment of a struct
Thanks Alan,
Originally Posted by AlanGRutter
Depends what you're doing with the struct I guess. For your goo example, a compiler is likely to allocate 4 or 8 bytes for this structure even though it technically only needs 1 byte (ignoring vtables etc). You can alter the alignment using pragmas if you need to do something like overlaying the structure on an area of memory (or raw bytes) or to address a hardware device.
For most applications though I personally don't worry too much about the alignment.
Regards
Alan
I am interested in two points,
1. How to do memory overlapping?
2. How to address a H/W device?
You mean both of them could be implemented by #pragma? Could you show an example please?
regards,
George
-
April 4th, 2007, 02:13 AM
#7
Re: memory alignment of a struct
Thanks Anant. It is a good article. But I do not understand why alignment will improve performance. Could you show an example please?
Originally Posted by anantwakode
regards,
George
-
April 4th, 2007, 02:16 AM
#8
Re: memory alignment of a struct
Hi,
e.g
struct foo
{
char ch1;
};
struct soo
{
char ch2;
};
see if I create objects
foo f;
soo s;
memory structure...may be like this
| f.ch1,s.ch2 | | |
may some cpu instructions have to filterout/Ignore (will need extra work to do to leave s.ch2 untouched ) s.ch2 while processing f.ch1 as the most of the instructions are developed to read/work on words at a time
if these are aligned
|f.ch1 |s.ch2 | |
can be fetched and processed seprately .................
-Anant
"Devise the simplest possible solution that solves the problems"
-
April 4th, 2007, 02:30 AM
#9
Re: memory alignment of a struct
Originally Posted by anantwakode
Hi,
e.g
struct foo
{
char ch1;
};
struct soo
{
char ch2;
};
see if I create objects
foo f;
soo s;
memory structure...may be like this
| f.ch1,s.ch2 | | |
may some cpu instructions have to filterout/Ignore (will need extra work to do to leave s.ch2 untouched ) s.ch2 while processing f.ch1 as the most of the instructions are developed to read/work on words at a time
if these are aligned
|f.ch1 |s.ch2 | |
can be fetched and processed seprately .................
-Anant
What is your format mean? Like sign '|' ??
| f.ch1,s.ch2 | | |
|f.ch1 |s.ch2 | |
regards,
George
-
April 4th, 2007, 03:36 AM
#10
Re: memory alignment of a struct
hi,
| means Word Boundry, i.e memory space
|1000|1001|1002|1003|..................
|Word|Word|Word|Word|Word|Word|Word|Word|Word|Word|
-Anant
"Devise the simplest possible solution that solves the problems"
-
April 4th, 2007, 05:03 PM
#11
Re: memory alignment of a struct
What is being said about alignment is true however you generally do not need to specifically pad out your data structures so that they align on word boundaries as the compiler will do this for you.
Consider the following code
Code:
#include <iostream>
#include <string>
using namespace std;
struct goo
{
char p;
int i;
};
#pragma pack(1)
struct goo1
{
char p;
int i;
};
#pragma align()
int main()
{
// character 'A' + Integer value 7979 (note byte ordering)
char bytes[] = {0x41, 0x2b, 0x1f, 0x00, 0x00};
cout << "Sizeof char: " << sizeof(char) << endl;
cout << "Sizeof int: " << sizeof(int) << endl;
cout << "Sizeof unpacked structure goo: " << sizeof(goo) << endl;
cout << "Sizeof packed structure goo1: " << sizeof(goo1) << endl;
//Overlay goo1 onto byte array
goo1 *pStructGoo1 = reinterpret_cast<goo1 *>(bytes);
cout << "Char p: " << pStructGoo1->p << endl;
cout << "Integer i: " << pStructGoo1->i << endl;
return 0;
}
In the first instance (goo) the compiler has allocated 4 bytes for the char and 4 bytes for the int. If I use the pragma to pack the structure to single byte boundaries, the extra space is removed.
By packing to single byte boundaries you can overlay a structure onto a stream of bytes as demonstrated above.
Regards
Alan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|