473,769 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allocation alignment for global and local variables.

Hi all.

I know that some padding bits are inserted between data members of a
structure. Is this rule also applied for the variables on local stack
or global??? For example, in following code dumps...

------------
struct foo foo1;
....
struct bar bar1;
....
char byte;
int dword;

int main(void)
{
}
--------------
Is it allowed to expect that each start address of foo1, bar1, byte and
dword are aligned 4bytes?

Does the C99 specification state any rules related with this problem?

Thanks in advance.

Best regards
GAMJA

Nov 15 '05 #1
4 7236
gamja wrote:
Hi all.

I know that some padding bits are inserted between data members of a
structure. Is this rule also applied for the variables on local stack
or global??? For example, in following code dumps... There isn't a rule.
It's up to the implementation.
------------
struct foo foo1;
...
struct bar bar1;
...
char byte;
int dword;

int main(void)
{
}
--------------
Is it allowed to expect that each start address of foo1, bar1, byte and
dword are aligned 4bytes? No.

(well, noone prohibits you from expecting whatever you want ;-)
Does the C99 specification state any rules related with this problem?

No.
This is no problem.
Nov 15 '05 #2
gamja wrote:
Hi all.

I know that some padding bits are inserted between data members of a
structure. Is this rule also applied for the variables on local stack
or global??? For example, in following code dumps...

------------
struct foo foo1;
...
struct bar bar1;
...
char byte;
int dword;

int main(void)
{
}
--------------
Is it allowed to expect that each start address of foo1, bar1, byte and
dword are aligned 4bytes?

Does the C99 specification state any rules related with this problem?


Each of foo1, bar1, byte, and dword will meet its type's
alignment requirements, whatever those are. There are no
rules about where in memory the four variables reside, nor
about their order. Since the order is unspecified, questions
of padding "between" the variables aren't meaningful.

(I once had a lot of trouble porting a large program from
one machine to the other. On the original machine, file-scope
variables within a module were allocated to ascending memory
addresses in the order they were declared -- so foo1 would be
followed by bar1 in the example above -- and the variables from
different modules were allocated in the order in which the
modules were linked. On the target machine I was porting to,
though, all globals from all modules were pooled together and
sorted alphabetically by their names. It turned out that the
program's author relied on the allocate-as-declared order ...)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3
Eric Sosman wrote:
[... memory order of global variables ...]
(I once had a lot of trouble porting a large program from
one machine to the other. On the original machine, file-scope
variables within a module were allocated to ascending memory
addresses in the order they were declared -- so foo1 would be
followed by bar1 in the example above -- and the variables from
different modules were allocated in the order in which the
modules were linked. On the target machine I was porting to,
though, all globals from all modules were pooled together and
sorted alphabetically by their names. It turned out that the
program's author relied on the allocate-as-declared order ...)


Too bad you can't say "he got what he deserved", as it was you who ended
up getting it.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #4
In article <11************ *********@g43g2 000cwa.googlegr oups.com>,
"gamja" <as****@gmail.c om> wrote:
Hi all.

I know that some padding bits are inserted between data members of a
structure. Is this rule also applied for the variables on local stack
or global??? For example, in following code dumps...

------------
struct foo foo1;
...
struct bar bar1;
...
char byte;
int dword;

int main(void)
{
}
--------------
Is it allowed to expect that each start address of foo1, bar1, byte and
dword are aligned 4bytes?

Does the C99 specification state any rules related with this problem?


Every variable, whether static or local, is aligned in the way that it
needs to be aligned on your implementation. If your implementation
requires that the address of a struct foo or an int is aligned to a
multiple of four bytes, then it will be aligned that way.

On a different implementation, the alignment requirements might be
different.

You can't expect anything for the alignment of the variable "byte".
Nov 15 '05 #5

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

Similar topics

4
24184
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
1
2326
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that passes __FILE__ and __LINE__). On destruction, it reports all undeleted blocks (memory leaks). The class is implemented in a separate .h/.cpp pair, and should be linked to
59
3943
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a candidate for Python 3000 yet? Chris
14
1958
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write C code, especially design a structure, I pay attention to the order and size of member variables. Because, my boss always says that all variables should be aligned by 4byte boundary, if not a data abort will be occurred on a specific machine such...
5
1867
by: Jim Ward | last post by:
After we installed the latest Solaris patch, 109147-37, the following code fails with an EFAULT error (14); int main(int argc, char* argv) { struct sockaddr_in sockaddr; /* code snipped */ child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size); /* code snipped */
15
1857
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
53
26394
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
1
29380
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
112
5474
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.