473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Array Out Of Boundaries

int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this

Sep 25 '06 #1
17 1763

krish wrote:
int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this.
Hey, the indexing starts from 0. So you have 5 elements in the array
starting from 0 to 4. So k[5] would be 6th element outside the bounds
of the array.

-kondal

Sep 25 '06 #2

kondal wrote:
krish wrote:
int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this.

Hey, the indexing starts from 0. So you have 5 elements in the array
starting from 0 to 4. So k[5] would be 6th element outside the bounds
of the array.

-kondal
That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.

Sep 25 '06 #3
krish said:
kondal wrote:
>krish wrote:
int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this.

Hey, the indexing starts from 0. So you have 5 elements in the array
starting from 0 to 4. So k[5] would be 6th element outside the bounds
of the array.

-kondal

That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.
What value did you expect j to have?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 25 '06 #4

Richard Heathfield wrote:
krish said:
kondal wrote:
krish wrote:
int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this.

Hey, the indexing starts from 0. So you have 5 elements in the array
starting from 0 to 4. So k[5] would be 6th element outside the bounds
of the array.

-kondal
That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.

What value did you expect j to have?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Some garbage value...But it is showing the values I am assigning there
in the line K[5]= 50

Sep 25 '06 #5
krish wrote:
Richard Heathfield wrote:
>>krish said:

>>>kondal wrote:

krish wrote:

>int main()
>{
>
int i,j;
>int k[]={1,2,3,4,5};
>k[5]=50;
>printf("%d %d",i,j);
>}
>
>This program is compiling fine and the result is -858993460 50
>I am unable to find why this is printing the above value. Request help
>on this.

Hey, the indexing starts from 0. So you have 5 elements in the array
starting from 0 to 4. So k[5] would be 6th element outside the bounds
of the array.

-kondal

That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.

What value did you expect j to have?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Some garbage value...But it is showing the values I am assigning there
in the line K[5]= 50
You're invoking the demon known as undefined behaviour, your 50 is just
a facet of the layout of your automatic variables.

--
Ian Collins.
Sep 25 '06 #6
krish wrote:
>
kondal wrote:
krish wrote:
int main()
{
>
int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.


You've created undefined behavior, ANYTHING can happen. In this case,
you're likely writing into the space occupied by j when do that illegal
access.

Why are you doing this? Such tests tell you nothing. There is no
defined behavior for undefined behavior.

Brian
Sep 25 '06 #7
krish said:
>
Richard Heathfield wrote:
>krish said:
<snip>
But how come j is printing the value 50 here all
the time or what ever value you are assigning.

What value did you expect j to have?

Some garbage value...
And that's what it has. So your expectations were met.
But it is showing the values I am assigning there
in the line K[5]= 50
Garbage is garbage. Any apparent patterns you see therein are meaningless,
especially when the program has other errors (such as the reference to
k[5], which does not exist).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 25 '06 #8

krish wrote:
kondal wrote:
krish wrote:
int main()
{
>
int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}
>
>
>
This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this.
Hey, the indexing starts from 0. So you have 5 elements in the array
starting from 0 to 4. So k[5] would be 6th element outside the bounds
of the array.

-kondal

That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.
You are experiencing what is called 'buffer overflow'.

First the three variables are in stack starting with i, j, k[] one
after the other. This could be compiler and operating system specific.
What I see is that k[5] is the location for j and k[6] would be the
location for i. K[7] would probably would be the return address.

This is the same concept what hackers do during the shell coding,
modifying the return address to point to an inserted function.

This scenario would not work for all compilers ot operating systems.

-kondal

Sep 25 '06 #9
"krish" <kr************@gmail.comwrites:
int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this
You invoke undefined behavior by assigning to k[5].

You invoke undefined behavior by accessing the values of i and j,
which are uninitialized.

You invoke undefined behavior by calling printf() with no visible
prototype (you need to add "#include <stdio.h>").

Possibly your compiler happens to place j just after k. Another
compiler might do something else.

Analyzing the specific results of undefined behavior can sometimes be
useful in figuring out why a program is failing. Once you've figured
that out, the next step is to correct the program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 25 '06 #10
krish wrote:
int k[]={1,2,3,4,5};
k[5]=50;
You are accessing the sixth element of a five element array.
Sep 25 '06 #11
krish wrote:
>> int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
That is understood .But how come j is printing the value 50 here all
the time or what ever value you are assigning.
You are getting lucky with undefined behavior.

Apparently, on your system, in the runs you have checked, the storage of
j is just beyond the last element of the array. Or it may be some
unrelated "50". Who knows if these things are related at all?
Sep 25 '06 #12
krish wrote:

response below.
int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this
i'm guessing because when you wrote past the end of the array you
overwrote the value of j with the value 50, but the value of i is
uninitialized so you are getting garbage.

rCs

Sep 25 '06 #13
On 25 Sep 2006 00:00:21 -0700, in comp.lang.c , "krish"
<kr************@gmail.comwrote:
>int main()
{

int i,j;
int k[]={1,2,3,4,5};
k[5]=50;
printf("%d %d",i,j);
}

This program is compiling fine and the result is -858993460 50
I am unable to find why this is printing the above value. Request help
on this
There is no element [5], so you're writing to memory that doesn't
belong to you. The result of this is undefined, and anything could
happen, including crashing your programme.

Rembember C array start at 1.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 25 '06 #14
On 25 Sep 2006 00:32:22 -0700, in comp.lang.c , "krish"
<kr************@gmail.comwrote:
>
Richard Heathfield wrote:
the time or what ever value you are assigning.

Some garbage value...But it is showing the values I am assigning there
in the line K[5]= 50
The value you're seeing /is/ garbage.

It just so happens that your particular compiler arranges the
automatic variables in in such a way, and in debug mode is kind enough
to let you write over them. This is probably a Bad Thing.

This tricked you into thinking you were getting a 'good' value. Try
turning off debug mode and rebuilding it.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 25 '06 #15
In article <b3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>Rembember C array start at 1.
You're just checking to see if we're awake, right Mark?
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Sep 25 '06 #16

Mark McIntyre wrote:
Rembember C array start at 1.
I wonder what you mean by this statement. In C the indexing starts from
0.

-kondal

Sep 26 '06 #17
On Mon, 25 Sep 2006 23:19:47 +0000 (UTC), in comp.lang.c ,
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
>In article <b3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>>Rembember C array start at 1.

You're just checking to see if we're awake, right Mark?
Grin. I meant to type "don't" in there somewhere...

Mind you, you're the only person who does seem to be awake...
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 27 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
7
by: Bangalore | last post by:
Hi all, Plz clarify me, on the implementation of two or three dimensional array using overloaded operator. Thanks, in advance Bangalore
8
by: nkw | last post by:
Hi, I'm new to C and I'm stuck in working out the logic code for a cyclic array. I have an array of 5 cells, starting at index cell 0 and last cell is 4. ie 0 1 2 3 4 0 1 ..... and so on
11
by: pashmina g. | last post by:
Hi, I'm reading the book "The C programming Language". I'm trying to do an exercise asking to find the longest line. I'm reading the book in sequence hence I shouldn't know anything about...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
2
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main...
3
by: Andrew Gentile | last post by:
Hello, I am trying to write a function which takes several floats as inputs, then returns 1x6 array of floats. I normally program in Matlab, and I am often confused by C. It seems that this...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
3
by: luftikus143 | last post by:
Hi, I would like to turn a hidden field value which I receive from an HTML page into an array. It would just be that something like: $layer = "boundaries" should become: $layer =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.