473,395 Members | 1,341 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,395 software developers and data experts.

Memory alignment in structures

hello,
What is mean by memory alignment in context of structure padding?
also why structure padding is useful? is it implicitly done by latest
c,c++ compilers or i have to specify it explicitly? how?
also why there is differece for data type integer on MS-DOS as 2 bytes
and on Windows as 4 bytes?
why the same is not true for short data type?

Nov 15 '05 #1
1 1555
> What is mean by memory alignment in context of structure padding?

Certain C types need to be aligned so that their addresses fall on
a multiple of some number (often, but not always, their size) for
hardware (you get an exception if you don't follow this rule) or
efficiency (you might have to do two memory fetches instead of one)
reasons. For example, 2-byte integers might need to be aligned on
a multiple of 2, and 4-byte integers or pointers might need to be aligned
on a multiple of 2 or 4, and 8-byte integers or doubles might need
to be aligned on a multiple of 2, 4, or 8.
also why structure padding is useful?
It makes the program run (as opposed to throwing smegmentation
faults or some other kind of alignment error trap), or run faster,
on the hardware you are using.
is it implicitly done by latest
c,c++ compilers or i have to specify it explicitly? how?
There is no standard way to explicitly specify it. It's not padding
if you specifically add an unused structure member, and doing so
may not do what to you expect to the padding in the next release
of the compiler, or on a different platform.
also why there is differece for data type integer on MS-DOS as 2 bytes
and on Windows as 4 bytes?
Because the compiler writers can.
why the same is not true for short data type?


Because the compiler writers decided not to.

A lot of this has to do with the processor it's running on and how it
is being used. MS-DOS has its roots in 8086 real mode. Windows
uses more of the memory-mapping features of the processor and is
designed around having a lot more memory available.

Gordon L. Burditt
Nov 15 '05 #2

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

Similar topics

1
by: Joy | last post by:
CREATE TABLE my table (coll_a INT NOT NULL, coll_b SMALLINT, coll_c SMALLINT, INDEX b_index (coll_b) ); Above is the table I will created. I declare coll_b and coll_c, which together will...
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...
4
by: Dharma | last post by:
hi, struct a { byte d; byte buf; }text; text.buf = 'A' text.buf = '\0'
2
by: Dan | last post by:
In C, it's usually necessary to clear the memory in structures before passing them to certain WinAPI functions. How would one do that in VB.NET?
3
by: iduniq | last post by:
Hi, Good day! Anyone knows how to extract shorts, long, chars from a char array considering the memory alignment? It's like this, there's a char buffer, which is cast into a struct, then cast...
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?...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.