Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote
As you will probably know the name of the array identifies the address of its first element. When you try to compile a program that uses an array with a negative index the compiler will not warn you about anything because it is a legal operation. Example: {{{ array[2] //its the same of saying take the address array and sum 2 to the address and then take the value in that portion of memory *(array+2) array[-2] //its the same of saying take the address array and subtract 2 to the address and then take the value in that portion of memory *(array-2) }}} Now the problem is that when you do something like {{{int array[10];}} You allocate the space only for 10 slots of memory addresses after the array start position in memory. This means that array[0], array[1], array[2], ... array[9] are safe but there is no guarantee for array[10], array[11], ... or array[-1], array[-2] etc.... However when creating arrays using pointers this is possible to do if you know exactly that at array[-2] there is valid data that you need to use. - Giovanni Murru July 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In C and C++ yes, it is allowed.
In java you will get 'ArrayIndexOutOfBoundsException'.
You can easily mess around with addresses in C. That is both the beauty and the drawback of using C language.

Now try predicting the output of this question:

#include<stdio.h>
int main()
{
	int a[3],i;
	for(i=-1;i<3;i++)
	{
//		printf("i is %d\n",i);
		a[i]=-2;
//		printf("i is %d\n",i);
	}
}

This gives you an infinite loop because a[-1] points to i
a[i]=-2 means i=-2.
Then i++ changes i to -1
and it gets stuck in an infinite loop!

Due to these security issues is C, there came the arrival of a new programming language- Java, which is nothing but modified C++ to remove security vulnerabilities and have better checks for overflows,etc.

- neerajlakhotia08 July 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1 and -1.

-1 for "...there came the arrival of a new programming language- Java, which is nothing but modified C++ to remove security vulnerabilities and have better checks for overflows,etc". That is just LOL worthy.

- Anonymous July 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C,it does not give any compilation error but we cant print the array value for that particular index,as it hangs the compiler during run time.

- rex.rishabh801 July 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But why a[-1] points to i??

- Anonymous August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a[-1] points to i because when the line

int a[3],i;

is encountered during execution, space is allocated in stack equal to space of 3 ints (for array a) + space for 1 integer (ie i). Let the adress of a[0] be 0x00000008 (for simplification and assuming 32 bit int size). Then a[1] would be at address 0x0000000c and a[2] would have address 0x000000010. The address of i would be 4 bytes before a[0]'s address in stack ie 0x00000006 because the space is allocated in Right To Left order ie i first, followed by a[3].

- kkaushi August 03, 2014 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More