Hewlett Packard Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 3 vote

I am not sure where it won't compile. It works fine on codepad!
Code: h t t p://codepad.org/IzbqB7Mc

a[1] translates to *(a + 1).
a[-1] translates to *(a - 1).
In words: value at (base address of array 'a' minus a integer size).

By all means, it should compile. However, at runtime one might hit segmentation fault/acces-violation if the address does not belong to this thread. But, array is a local variable in this case inside function main. Which means, the allocation of array 'a' is on the stack. Minus four bytes just takes to the call stack frame of function main. So, in most cases you get unpredictable value (and may not really hit AV/SegFault).

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, u r correct. It compiles, and I fortunately was able to revert my answer during the interview to say yes, it will compile :P but wasn't sure.

But, Its not overwriting the previous argument..for instance
char a, b[10];
then b[-1] shouldn't overwrite a for a function stack??

- Varun November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah it even compiles if you write it "upside-down", i.e.

(-1)[a] = 4;

because for the compiler a[i] and i[a] are semantically the same))

- Anonymous November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Compiler wont throw any error. There will be no runtime error or segmentation fault. If the memory address of a is 1000. Then a[-1] is stored at 996. You can store and retrieve it with no problem.

- smuralireddy May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

Good Question..

Lemme Tell ya...
1> It will compile and run . I swear there will be NO crash as well. By that what I mean is C/C++ unlike Java Does NOT have bound check for arrays. So than what will happen to the value ??

Here it goes .. basically it will depend on OS but basically here what the compiler calculates: (base address of array a - datatype of array a). so here if a is 1000 and assuming 32 bit compiler , value of 20 is going to be stored at logical address 1000-4 i.e. 996.

- hprem991 November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

I did this in my cent os,

int main()
{
    int a[20];
    int i = 9
    a[-1] = 4;
    printf("%d",i);
    return 0;
}

So it printed 4, it will not work if i exchange the declaration of i and a[], you can check by printing the memory addresses.

- Anonymous November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that's nice. Actually this is what I answered him, and was 100% sure, this should happen, but on my DevC++ windows IDE, i wasn't seeing this happening.

Maybe, I will boot with unix and try it.
Thnx for confirming.

- Varun November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice thought....

- mukesh kumar dhaker November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

the program will definitely compile, but the behaviour is undefined. why here is the reason.

declaration int a[10] // it allocates 40 bytes and suppose 1000 is the starting address.
a[-1]=10 // access memory location 996 and put 10 at this location.
but we are not sure whether 996 memory location is read only ,part of stack, part of code segment.
if it is read only,part of stack or part of code segment, then surely we will get error like segmentation fault or access violation.

- getjar.com/todotasklist my android app November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You might change value of other variable on the stack or you might corrupt the stack frame. If the array is static, you might get segmentation fault.

- Anonymous November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in any case it wont give compilation error. It may or may not give run time error.

- Saurya November 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{
int a[10];
a[-1]=20;
printf("%d",a[-1]);

o/p is 20

- robin November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of my professors was talking about this. He basically said that the prefix to an array is made up of the size of the array. So basically at a[-1] there exists a variable that has the size of the array. So that is where you would be writting to. Please double check this, this is just what I remember from last quarter.

- Jedan Garcia February 07, 2014 | Flag Reply


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