473,405 Members | 2,167 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,405 software developers and data experts.

negative index in array

say i have a two dimensional array a[512][512]. There is one more 2D array
b[512][512]. The algorithm is like this depending on value of a[i][j] i have compare b[j][k] with two of its neighbour.
This is a part of that algorithm
if(a[j][k]==0)
{
if(b[j][k]>=b[j][k+1] && b[j][k]>=b[j][k-1])
some code here
}

the problem is when k=0 b[j][k-1] will be b[j][0-1]=b[j][-1].
To handle this i have padded b[i][j] in such a way that b[j][-1] and b[-1][k] is zero for all j and k.

My question here is is it the right way to handle the negative indexes in array??
should there be any modification made in the code.
Mar 24 '10 #1
7 9442
Banfa
9,065 Expert Mod 8TB
That is not the correct way to handle negative array indexes. Using a negative array index is undefined behaviour, it the computer will do it you will certainly have accessed memory outside of the boundary of the object.

The correct solution is to re-write the code so that it does not make any attempt to access an array using a negative index.
Mar 24 '10 #2
jkmyoung
2,057 Expert 2GB
It depends on what your algorithm is trying to do, and if space/time is an issue. Building a [514][514] array requires 2052 more elements of space.
Mar 24 '10 #3
Banfa
You said that it is not hte right way to handle negative indexes in array then what should be the right way to access a negative index.
Take a simple example
int a[10]={1,2,3,4,5};
suppose i further declare a[-1]=0 and then use it in the code will that be a wrong thing to do?
In the original question i have padded the array and then using it.Is that a wrong thing?
Mar 24 '10 #4
donbock
2,426 Expert 2GB
The point is that you must not declare a[-1]=0.

You can either modify your logic to treat the first and last entries as special cases. Or instead of declaring the array with size N and using index values of 0 to N-1; you could declare the array with size N+1 and use index values of 1 to N, with index value 0 available for those rare cases when your algorithm accesses the element before the first element.
Mar 24 '10 #5
donbock
I read in K&R so i thought its possible to do such thing
This is quote from K&R
"If one is sure that the elements exist, it is also possible to index backwards in an array; p[-1], p[-2], and so on are syntactically legal, and refer to the elements that immediately precede p[0]. Of course, it is illegal to refer to objects that are not within the array bounds."
chapter 5 (5.3 Pointers and Array) paragraph just before start of 5.4.

What do they mean by this statement?
Mar 24 '10 #6
jkmyoung
2,057 Expert 2GB
In terms of the address space, if you also had a pointer to the middle of the array, you could go relatively backwards by inserting in a negative index.

What does your algorithm do? Fundamentally, this should be your first question.
Mar 24 '10 #7
donbock
2,426 Expert 2GB
@destination
They are talking about the case where a pointer variable points into the middle of an array. In the following example, a[0] is 0, pa[0] is 2, and pa[-2] is 0.
Expand|Select|Wrap|Line Numbers
  1. int i;
  2. int a[10];
  3. int *pa = &a[2];
  4.  
  5. for(i=0; i<10; i++)
  6.     a[i] = i;
The index used with a can be any value between 0 and 9.
The index used with pa can be any value between -2 and 7.
As long as you honor these restrictions you will never access a location outside the bounds of the array.
Mar 24 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: dan | last post by:
I was recently surprised, and quite shocked in fact, to find that Python treats negative indices into sequence types as if they were mod(length-of-sequence), at least up to -len(seq). This fact...
2
by: Mantorok Redgormor | last post by:
Are negative values used as an index for an array valid or is this illegal and if it is illegal where does it state it in the standard? The only way I can see where a negative value can be used...
6
by: David Osborn | last post by:
I have a pointer "a" that points to an element in an array that is not the first element. I want to use a negative index to derefence the pointer, like so: a. Is this valid C99? If not, I can...
8
by: Joakim Hove | last post by:
Hello, I have the following code: #define N 99 double *ptr; double *storage; int index; storage = calloc(N , sizeof(double));
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
1
by: Ryan Liu | last post by:
Hi, When my code run to DataRow.Delete() It throws: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: length at System.Array.Copy(Array sourceArray, Int32...
35
by: Martin Wells | last post by:
Plain char may be signed or unsigned. Typical ranges could be: CHAR_MIN == -128, CHAR_MAX == 127 CHAR_MIN == 0, CHAR_MAX == 255 The Standard says that the behaviour is undefined if we...
7
by: klays | last post by:
Hi all, I have created char array, when I use tab compiler shouts out: error: size of array 'tab' is negative I tried to cheat compiler and a little modify this code. Look at the code: ...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
6
by: shashi shekhar singh | last post by:
Respected Sir, I am facing problem when i try to deploy my website on iis 7.0 on test page. i have to display some .mht files on iframe in gridview and error looks like below, Server Error in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.