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.
Hi, as i understand c doesnot perform bound checking , and any acess of previous index will just get the prev indexed mem contents and same for beyond index.
main()
{
int y = 98;
int a[5]={11,12,13,14,15};
int z = 541;
[santosh@localhost test]$ ./a.out
a[0]=11 a[4]=15
a[-1]=134518428 .. why it is not 98? as it refrers to prev mem location
a[5]=98 ... and i 'm confused how its 98 but should be 541.
If the same set of local variables is made global s it works , so is theat difference because of stack ?
Plz provide clarification, thanks in advance.
Last edited by Santoshkb; 01-31-2010 at 02:46 PM.
Reason: update observation
As far as I am aware the language doesn't dictate to the compiler in what order it should allocate the memory for your program variables. So one way that a compiler could work would be to collect details of each variable as it parses your code. In the example that you have given the compiler would identify three variables y, a and z. When it is parsing the code it will notice when these variables are being used. "Heck," says the compiler, "You don't even use the variables y and z" and so it will throw a warning and possibly not even allocate memory for those two. But given that it will allocate memory how might it do that? Well if the identified variables have been placed on a stack data structure then it will pop them off one at a time and generate the code to allocate the memory for them. Given that it will have them in reverse order.
As I said this is just one way that a compiler could approach the problem; and I've not mentioned any approaches to optimisation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.