bbe9 gcc options to remove space between arrays and other variables - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, C++, Python, Perl, Shell, etc.

Reply
 
Thread Tools Display Modes
  #1  
Old August 19th, 2012, 06:31
asp5 asp5 is offline
Junior Member
 
Join Date: May 2012
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default gcc options to remove space between arrays and other variables

Hi all,

Code:
main() 
{
    int d[4];
    int b = 55;
    int c = 34;
    int e = 23;

    return 0;
}
For the above code I see that in some implementations of Gcc
  1. &d[4] = &b and
  2. other implementations &d[5] = &b.
As we can see the second implementation leaves some space between the arrays and other variables. b, c and e have no spaces between them in both the implementations.

Is there a gcc option which tell it not to leave such spaces?

Also, the second is on a powerpc (OS: OpenBSD) and the first is a x86 machine running FreeBSD. Both are 32 bit processors.

Thanks.
Reply With Quote
  #2  
Old August 19th, 2012, 06:48
kpa kpa is online now
Giant Locked
 
Join Date: Jul 2010
Location: People's Technocratic Republic of Finland
Posts: 2,136
Thanks: 46
Thanked 507 Times in 430 Posts
Default

I would think that on the 2nd system (powerpc) the compiler tries to keep the stack boundary aligned at 4 times 32bits equaling 128bits. On i386 there may not be the same requirements. There seems to be an option that might be what you're looking for, this is an i386 and amd64 specific option:

Code:
-mpreferred-stack-boundary=num
       Attempt to keep the stack boundary aligned to a 2 raised to num
       byte boundary.  If -mpreferred-stack-boundary is not specified, the
       default is 4 (16 bytes or 128 bits).
But it seems to default to the same alignment as in the powerpc example

Which version of gcc(1) you used in the FreeBSD case? The manual page I quoted is from version 4.6.
Reply With Quote
  #3  
Old August 19th, 2012, 17:37
asp5 asp5 is offline
Junior Member
 
Join Date: May 2012
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by kpa View Post
Which version of gcc(1) you used in the FreeBSD case? The manual page I quoted is from version 4.6.
Thanks, will look for similar options in PowerPC. I've FreeBSD 9.0 RELEASE and gcc version 4.2.1.

EDIT: I've seen that the stack-boundary option changes how the stack is setup, For eg:
Code:
 
0x0804843d <main+13>:   push   %ecx
0x0804843e <main+14>:   sub    $0x20,%esp   
0x08048441 <main+17>:   mov    $0x0,%eax
0x08048446 <main+22>:   add    $0x20,%esp
Though we need only 7 words of memory for the purpose of alignment, the compiler setup 8 words (0x20 bytes). This means that there is some space left after/before all the variables (array, b, c and e) ? But in the 2nd case (powerpc) the compiler decided to leave space between the array and the other variables, (in the x86 case the compiler leaves space before/after all the variables)? Is that how it works? I'm not familiar with the powerpc arch, so can not understand the assembly . Please let me know if the above interpretation is correct. If it is correct then how can I make the compiler leave space after/before the variables and not between the array and variables.

Thanks.

Last edited by asp5; August 19th, 2012 at 22:13.
Reply With Quote
  #4  
Old August 19th, 2012, 22:51
rolfheinrich's Avatar
rolfheinrich rolfheinrich is offline
Member
 
Join Date: Nov 2010
Location: São Paulo - Brazil
Posts: 369
Thanks: 33
Thanked 115 Times in 76 Posts
Default

#pragma pack(n) where n may be 2, 4, 8, ... is what you are looking for.

For performance reasons, you might want to enclose only some structs with this directive. Anyway, the most portable option would be to use unions where possible.

Best regards

Rolf
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaration of Variables Sarouel Userland Programming & Scripting 8 October 14th, 2012 09:19
Kernel Space / User Space bi-directional communication perleo FreeBSD Development 3 July 23rd, 2012 07:23
ssh variables eeluve Web & Network Services 1 July 11th, 2012 08:59
[Solved] fsck command for oversized RAID arrays? ghostcorps System Hardware 6 November 29th, 2010 22:37
[Solved] How do you reestablish zpool/ZFS arrays after reinstalling the OS? palmboy5 Installing & Upgrading 11 August 14th, 2010 07:33


All times are GMT +1. The time now is 16:56.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0