LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-31-2010, 02:33 PM   #1
Santoshkb
Member
 
Registered: Sep 2007
Posts: 56

Rep: Reputation: 15
Question Regarding Out of bound array element acess


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;

printf("a[0]=%d a[4]=%d\n",a[0], a[4]);

printf("a[-1]=%d\n",a[-1]);
printf("a[5]=%d\n",a[5]);
return 0;
}

[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
 
Old 01-31-2010, 03:50 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Santoshkb View Post
...so is the difference because of the stack?
Yes, the difference is because of the stack.

Modify your program to print out the addresses of the stack variables, and you will see how your compiler uses the stack.

Disassemble your program, and you will become even more enlightened.

What architecture (x86, ARM, etc.) and what compiler (GCC, MSVC++, etc.) are you using?
 
Old 01-31-2010, 07:27 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
  


Reply



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
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
Every Element in C++ Array Points to the Same Thing anamericanjoe Programming 5 12-14-2006 08:26 PM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

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
Open Source Consulting | Domain Registration