Adobe Interview Question for Member Technical Staffs


Country: India
Interview Type: In-Person




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

Not sure if this will always work, but after a bit of tinkering I came up with a (gross) solution.

int is_stack(void* p) {
	int junk;
	return p > &junk;
}

int main(int argc, char** argv) {
	int a, b, c;
	int *d = malloc(1);
	int e;
	printf("%d %d %d %d %d\n", is_stack(&a), is_stack(&b), is_stack(&e), is_stack(d), is_stack(&e));
}

$ ./sht
1 1 1 0 1

Should be relatively self-explanatory, if ugly.

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

for me output is 1 1 1 1 1 on VS.

- keyurpatel80 May 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this solution won't work on Descend/Ascend stack both, since the stack can grow upside or downside in each scenario.
as the other way, I did a tiny tweak based on the fact that stack and heap won't exist in the same page ,
=======================================
#define PageSize 0x1000
bool is_stack(void* p) {
int junk;
int b=(int)p;
int c=(int)(&junk);
return ((c-b)>PageSize)?0:1;
}
================================

- Anonymous December 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain why (is_stack(d)) instead of (is_stack(&d)) while calling is_stack() in printf() of main function ?

- Anonymous March 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

There are a couple of ways to know it...
1. The way we check it in industry, for every executable we have a map file. Where we can see the address of every variable. We know the address range of hour heap and stack respectively.So by checking the map files we answer it.

2. Take two variable allocate memory from stack and heap for it. print its address and print the address of the variable for which you have answer. Check their address and answer on which range its lying.

3. Brute force approach, try to free() it, if u r able to do it successfully then its from heap. If the program crash its from stack.

- rs November 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. C and C++ know nothing about a map file. If you have a map file, you probably also have the source code making the problem trivial.
2. No one says you can modify the program. But even if you could, now you have three addresses. You can conclude nothing based on that. You don't know in what direction the stack grows. You don't know the order of allocation of local variables, the heap need not be consecutive.
3. If the program doesn't crash you can conclude nothing.

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

if I consider c/c++ as the source language, every time we compile the source code, the compiler is also configured to generate the map file. With the information about stack/heap expansion. It is located from there. There is nothing trivial in that. If you find anything suspicious, comments are welcome.
Second thing, if I have the source code. It can be easily seen that if the variable is allocated dynamically its from heap, if the variable is global and initialized it goes to Data Section. if its static, global uninitialized then its goes to BSS, if the variable is part of any function and memory has not allocated dynamically, its goes to stack.

- rs November 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah, I told him that if addresses of two consecutive variables are given then it can be found out and that too only when we know the direction in which stack grows. but, he rejected the answer.

Actually i was thinking that is there a particular system call which can tell whether it is allocated on stack or on heap which he was expecting from me.??

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

gnu.org/software/libc/manual/html_node/Summary-of-Malloc.html#Summary-of-Malloc

int mcheck (void (*abortfn) (void))
Tell malloc to perform occasional consistency checks on dynamically allocated memory, and to call abortfn when an inconsistency is found. See Heap Consistency Checking.

- anon.coder November 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

any more details regarding point 1 will be really appreciated. could you please provide ??

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

while compiling the source code we configure the linkage and generate the map file. For a general/specific OS or for gcc compiler u can find how to configure the linker/generate the .map file. This file is very useful at the time of debugging.

- rs November 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

gdb can be used? break into function and check via info locals or args?

- Varun November 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isFromStack(void* ptr)
{
int x=0;
void * ptr2=&x;
return ptr2 > ptr;
}

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

What the function return in case below:
void* ptr = 0;
isFromStack(ptr);

- AndrewJD November 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You should use that function by supplying the address of the variable, like isFromStack(&ptr) in your example.

- Selmeczy, Péter November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The only problem with this code is that it does not work :)

Why do you think that an address from the heap is above or below the address of the current stack? (Your guess on that the current stack is above any old stack is OK, but the opposite is not).

Furthermore what is the case with static variables? They are neither on heap, nor on stack, just sitting in some data segment (if the undelying model has "segment" at all)

And if we go a little bit further a variable that contains a reference to the heap is on the stack (or static)... I mean the variable itself. The address it contains is on the heap.

- Selmeczy, Péter November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I checked by declaring a variable on the stack and another on the heap. When I run the program multiple times, I see the same address for the stack variable but different addresses being given to the heap variable. Trying to relate this.

- Anubhav December 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The gap between App stack address and heap address is big.
Some apis could print out current stack information.

- Ian Feng April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

suppose the given pointer is ptr;
we can call realloc(ptr,1). if the new address of ptr is same as older then it was a heap variable. If it changes then its a stack variable. The program might crash if its a stack variable.

- Akash Mahajan June 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Consider this(Verified on 32bit system):

int a=10;
	int *b=(int*)malloc(sizeof(int));
	*b=10;

	//now treat as if you don't know from where a and b are allocated
	//now free both
	free(a);
	free(b);

	if(a==10)
	printf("\n a is on the stack");

	if(b==10)
	printf("\n b is on the stack");

- Kartik July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stack always grows downward and heap grows upwards.

- vk41286 September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If we have variable names, assign some value to them. let say assign 10 to both. Now free both variables. If the memory has been allocated through heap it will lose value 10 and now contain some junk value. But the variable whose memory is allocated through stack will still contain 10 even after free since it do not affect memory locations on stack.
I have verified this using gcc on 32 bit system

Code snippet:

a=10;
b=10;

//now free both
free(a);
free(b);

if(a==10)
printf("\n a is on the stack");

if(b==10)
printf("\n b is on the stack");

- Kartik March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

free function always take pointer as argument.
Your code wont work.

- keyurpatel80 May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Verified solution:

int a=10;
	int *b=(int*)malloc(sizeof(int));
	*b=10;

	//now free both
	free(a);
	free(b);

	if(a==10)
	printf("\n a is on the stack");

	if(b==10)
	printf("\n b is on the stack");

- Kartik July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This does not work.

free(a); and if(b==10) wont compile...

- Anonymous August 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

very simple .....

- kuldeep singh December 21, 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