473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Memory Alignment

hi,

struct a
{
byte d;
byte buf[16];
}text;

text.buf[0] = 'A'
text.buf[1] = '\0'
text.buf[2] = 'B'
text.buf[3] = '\0'
text.buf[4] = '\0'
text.buf[5] = '\0'

main()
{
unsigned short *c = NULL;
c = (unsigned short*)&text.buf;
if(0x0000 == *c)
{
return ;
}
}

i have a structure as above. Iam type casting the byte to unsigned
short pointer.
when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
starts with odd memory address, If i make the memory Alignment to even
there is no problem,
What the low level does if memory Alignment is odd aligned and it is
unsigned short* type.

thanks,
Dharma.

Nov 15 '05 #1
4 5225
"Dharma" <dh**********@huawei.com> writes:
struct a
{
byte d;
byte buf[16];
}text;
C has no standard type called "byte".
text.buf[0] = 'A'
text.buf[1] = '\0'
text.buf[2] = 'B'
text.buf[3] = '\0'
text.buf[4] = '\0'
text.buf[5] = '\0'

main()
This should be "int main(void)".
{
unsigned short *c = NULL;
c = (unsigned short*)&text.buf;
if(0x0000 == *c)
{
return ;
}
}

i have a structure as above. Iam type casting the byte to unsigned
short pointer.
No, you're converting an address of (i.e., a pointer to) a byte to a
pointer to an unsigned short.
when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
starts with odd memory address, If i make the memory Alignment to even
there is no problem,
What the low level does if memory Alignment is odd aligned and it is
unsigned short* type.


Alignment requirements vary from one system to another. If type short
requires even alignment, and you have a misaligned pointer, attempting
to dereference it (as in *c) invokes undefined behavior. As far as
the language is concerned, anything could happen.

The solution: don't do that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #2
Keith Thompson wrote:
"Dharma" <dh**********@huawei.com> writes:
struct a
{
byte d;
byte buf[16];
}text;
main()
{
unsigned short *c = NULL;
c = (unsigned short*)&text.buf;
if(0x0000 == *c)


Alignment requirements vary from one system to another. If type short
requires even alignment, and you have a misaligned pointer, attempting
to dereference it (as in *c) invokes undefined behavior. As far as
the language is concerned, anything could happen.


I think the cast causes undefined behaviour too, if there is an
alignment problem.

Nov 15 '05 #3
"Old Wolf" <ol*****@inspire.net.nz> writes:
Keith Thompson wrote:
"Dharma" <dh**********@huawei.com> writes:
struct a
{
byte d;
byte buf[16];
}text;
main()
{
unsigned short *c = NULL;
c = (unsigned short*)&text.buf;
if(0x0000 == *c)


Alignment requirements vary from one system to another. If type short
requires even alignment, and you have a misaligned pointer, attempting
to dereference it (as in *c) invokes undefined behavior. As far as
the language is concerned, anything could happen.


I think the cast causes undefined behaviour too, if there is an
alignment problem.


You're right. C99 6.3.2.3p7:

A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the
behavior is undefined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4
> i have a structure as above. Iam type casting the byte to unsigned
short pointer.
when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
starts with odd memory address, If i make the memory Alignment to even
there is no problem,
What the low level does if memory Alignment is odd aligned and it is
unsigned short* type.


The following article should help:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based Real-time and Embedded System Design Tool

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
7
by: serikas | last post by:
Is there a way to get aligned dynamically allocated memory? (provided that the requested memory size is a power of 2.) For example, if I request 128 bytes of memory, can I implement an allocator...
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
13
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the...
7
by: Dhirendra Pal Singh | last post by:
Hi all, I have couple of questions, I hope I can find the answers here...(assuming the question are okay with this group) A) what is memory alignment? I have a rough idea but cant clearly...
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
29
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? ...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
8
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.