Interview Question


Country: United States




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

@Barney, @luv, I want to share some of my concepts.
I request you to get your basic concepts clear & brushed up.
When you are calling f(), then memory is being allocated for array a[] on the stack, where you are initializing its sots with 1. Now, since this array has been allocated on the stack, the scope is local. When you return from the function, the memory gets freed up. However memory doesn't go anywhere. The OS maintains a free list of all the memory which has been freed & so, can be used by any other process. By garbage value, the process means that it is expecting any unusual value[it may mean 1 or value with figure like 84638290(Both are considered as garbage for that process)].
Now, coming to the point, When you call function g(), memory is being allocated for array b[], However, its just an accident that the same memory gets allocated that was being allocated to a[] & so, it contains all 1.
However, its never safe to rely upon such type of allocation[as its just an accident].
A different compiler, a different optimization settings would trap you.

Hope everything is clear.

- Aashish July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shondik is correct. We would get different results on different machine and also on different compilers.
+1 for Griffindor!

- Pavan Dittakavi July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aashish yes buddy you are right.

- agmegharaj November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The explanation by Aashish shows a complete lack of understanding about how stack memory works in a (typical) OS.

- Anonymous November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

It prints garbage value .... not a 12 times 1...

1073836921
0
-1078604124
1074324994
1075105060
1075105048
-1078604284
1073841448
1073841184
1073831176
1073829312
1073844912

- Raj August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Garbage value.

- Aashish July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check the output, its not printing garbage values

- Luv July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Luv can you tell why won't it print the garbage value if memory allocated to array is in stack?

- hubulu July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik : But its not printing garbage values . It is printing 1's . It will print garbage value only when if in function g() , size of array >12 . When size of array in g() > 12 , then it is printing 12 1's and rest as garbage values .
Explain this behavior .

- Shobhit July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@hubulu because the memory allocated to array b is same as the memory allocated to array a previously, hence that was already initialized to 1, that's why i think o/p is coming 1

- Luv July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Garbage values will be printed.

- Avi July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

garbage value.....because array allocated memory on stack(ie at compile time) and stack memory contain same garbage value that y it print garbage.

- Anonymous July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

garbage value will be printed

- lourdhu July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Output will be...

1
1
1
1
1
1
1
1
1
1
1
1

- Raj Naik July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

12 times 1....

- Raj Naik July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for array greater than 12 it will print some of a and some of garbage value

- ankur July 04, 2012 | 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