Adobe Interview Question
Member Technical StaffsCountry: India
Interview Type: In-Person
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;
}
================================
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.
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.
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.
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.??
any more details regarding point 1 will be really appreciated. could you please provide ??
You should use that function by supplying the address of the variable, like isFromStack(&ptr) in your example.
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.
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");
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");
Not sure if this will always work, but after a bit of tinkering I came up with a (gross) solution.
$ ./sht
- Anonymous November 18, 20121 1 1 0 1
Should be relatively self-explanatory, if ugly.