Question

In: Computer Science

What happens if the stack and heap segments collide (i.e., one overwrites part of the other)?...

What happens if the stack and heap segments collide (i.e., one overwrites part of the other)? Why is this dangerous? How would you prevent it?

Solutions

Expert Solution

Answer:

If the stack grows into the heap, then the C compiler will silently start to overwrite the heap's data structures without keeping any concern. On recent and modern Operating Systems, there are one or more virtual memory guard pages which restrict the stack from growing indefinitely. And as long as the amount of memory in the guard pages is at least as large as the size of the growing procedure's activation record, the Operating System will assure us with a segfault.

And if the heap grows into the stack, then the operating system must beware of the situation as some sort of system call will fail. The implementation of malloc() almost certainly notices the failure and returns NULL. Anything after that depends upon the user then.

Also please note this is platform dependent. Doesn't happen everywhere. As in a lot of places stack and heap are allocated at separate pages.

It cannot be prevented if you have code that is in a runaway loop, creating huge amount of data that overruns the available memory. Therefore Check all places where you are in a for or while loop to be sure that they can exit. Then this collision can be prevented.

***********************************************************************************************************************************************

Hope this helps, Thanks :-)


Related Solutions

What is the difference between stack-dynamic and explicit heap-dynamic?  
What is the difference between stack-dynamic and explicit heap-dynamic?  
Estimate how often galaxies collide in a galaxy cluster, i.e., the stellar component of one galaxy...
Estimate how often galaxies collide in a galaxy cluster, i.e., the stellar component of one galaxy passing through another.(Model the average galaxy as a sphere with a radius of 10 kpc).
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different...
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. char[] class = {'C','O','M','P','1','2','2'"}; #define int n = 4; long long fibb(long long a, long long b, int n) { return (--n>0)?(fibb(b, a+b, n)):(a); } int main() { fib(3); //illustrate what memory looks like at this point return 0; }
C programming Illustrate the stack and the heap allocation. Specify what each variable value holds and...
C programming Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { char str[20]; scanf("%[^\n]%*c", str); //illustrate how memory is allocated at this point printf("%s", str); return 0; }
C Programming Illustrate the stack and the heap allocation. Specify what each variable value holds and...
C Programming Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. char[] class = {'C','O','M','P','1','2','2'"}; #define int n = 4; long long fibb(long long a, long long b, int n) { return (--n>0)?(fibb(b, a+b, n)):(a); } int main() { fib(3); //illustrate what memory looks like at this point return 0; }
2. What happens in a market when one side has more information than the other? How...
2. What happens in a market when one side has more information than the other? How might this affect financial markets? Which side of a financial market is more likely to have better information than the other. How might this have affected the financial markets in the 2007-08 crash? What can be done to solve this problem? Could any of these solutions avoided the 2007-08 crash?
ALL of the transactions/questions in this section are related to each other, i.e. if one transaction...
ALL of the transactions/questions in this section are related to each other, i.e. if one transaction decreases Allowance for Doubtful Accounts the balance will change for the next question. Starting Information: On July 31, 2019, the company's end-year, Diamond Drills had the account balances as follows: Accounts Receivable $340 00 (DR) Allowance for Doubtful Accounts 5 200 (CR) a. Diamond Drills' credit department determined that $5 900 should be written off. Record the transaction to do this. b. Diamond Drills'...
consider spherical aberration. Describe 1. the effect of the abberation (i.e. what happens to an image)...
consider spherical aberration. Describe 1. the effect of the abberation (i.e. what happens to an image) 2. what is thr optical basis of the problem 3. THREE factors contribute to it
What happens in Push Systems, what kind of wastes might be in the process, what other...
What happens in Push Systems, what kind of wastes might be in the process, what other types of LEAN tools might you implement as you re-sequence the activities, what kinds of new problems might arise once a pull system is implemented, how would you utilize standardized work or visual management, how would you identify value versus non-value added work, etc.
How does structure of the factory floor (i.e. classroom) influence what happens in the classroom? harry...
How does structure of the factory floor (i.e. classroom) influence what happens in the classroom? harry cleaver "on school work and the struggle against it"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT