ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ?
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??
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.