LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-14-2005, 06:52 AM   #1
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
gcc stack problem


when i create two buffers in C as below:
char buf1[5];
char buf2[10];

it will take up 8 bytes for buf1 and 12 bytes for buf2 totally 20 bytes from the stack.However on my athlon xp 2000 it takes 40 bytes why is this happening?? OS is slackware compiler gcc3.2.
 
Old 08-14-2005, 05:50 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
How to do you calculate the amount of memory the program uses?
 
Old 08-14-2005, 06:20 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,496

Rep: Reputation: 68
yea exactly. I think I brought up that question and was told there is not a way
 
Old 08-14-2005, 10:05 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Buffers are usually allocated to the multiple of the word size == sizeof(int) on most machines.
In the first case, sizeof(int) == 4 so sizeof(buf1) == 8 && sizeof(buf2) == 12.
The reason for this is efficiency.

Quote:
How to do you calculate the amount of memory the program uses?
With size(1), ie: "size /bin/ls"
The size of individual buffers may be calculated as sizeof(buf) if it wasn't declared as "char *buf"

The second case puzzles me. Is sizeof(char) == 2 ?
 
Old 08-15-2005, 12:23 AM   #5
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Original Poster
Rep: Reputation: 16
Here is a sample C program:

Code:
void f(int a, int b) {
char x[5];
//char y[10];
}

void main() {
f(1,2);
}
And the GCC generated assembly code of the program:

Code:
	.file	"x.c"
	.text
.globl f
	.type	f, @function
f:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	leave
	ret
	.size	f, .-f
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	$2
	pushl	$1
	call	f
	addl	$8, %esp
	leave
	ret
	.size	main, .-main
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.5"
When a function is called from the main, needed space is reserved from stack by decreasing the esp (subl $16, %esp)
Here there is only one local which is char[5] and it must be reserved 8 bytes (2 words) But as you see gcc allocates 16 bytes. For some reason it is doing some alligning but i dont understand what it is doing and why it is doing? However everything is OK if i use a char with a size of multiple of 4. For example if i use char [4] instead of char [5] than exactly 4 bytes allocated by gcc. Anyone can help??
 
Old 08-15-2005, 05:23 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Try "gcc -mpreferred-stack-boundary=2 ..."
 
Old 08-15-2005, 06:46 AM   #7
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Original Poster
Rep: Reputation: 16
Above code is already using -mpreferred-stack-boundary=2
 
Old 08-15-2005, 05:42 PM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
It allocates more than it needs. For future use, probably. To see it yourself make something like
Code:
void f(int a, int b) {
char x[5];
//char y[10];
char z[4];
}

void main() {
f(1,2);
}
Still 16B of stack allocated.
 
Old 08-16-2005, 12:44 AM   #9
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Original Poster
Rep: Reputation: 16
I know that..when you do char [5] and char [12] it allocates 32 bytes...somehow it is alligning by multiple of 16...but there must be some configuration not to allocate more just needed...gcc version 2.95 and before does not do that only newer versions...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference b/t Kernel stack and User stack hazzyb Linux - Software 2 09-29-2008 08:40 PM
allocation problem [static stack storage] kranti Programming 1 10-18-2005 10:10 AM
Solved Problem Second GCC-Pass(or gettext sanity check problem) hoes Linux From Scratch 0 09-04-2005 11:20 AM
stack problem C eagle683 Programming 3 05-17-2005 05:52 PM
2052 stack size is not enough! - glibc problem Yerp Linux From Scratch 2 12-26-2004 04:12 PM


All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration