1 / 6

Program Compilation and Execution

Learn the importance of runtime stack for C, memory division, comparing stack and heap, and variable storage. Includes code example and memory layout explanation.

Download Presentation

Program Compilation and Execution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Compilation and Execution

  2. Today’s Objectives • Explain why runtime stack needed for C • Draw logical division of memory into stack, heap • Compare and contrast stack and heap • List variables stored in heap; stack; neither

  3. Logical Memory Layout Stack D Y N A M I C Currently not in use Currently not in use Heap Static Space

  4. Dice.c int NUM = 100000; main() { int i, roll, *counts; counts = (int *) malloc (13 * sizeof(int)); for (i = 0; i < 13; i++) counts[i] = 0; for ( i = 0; i < NUM; i++) roll = rand() % 6 + rand() %6 + 2 counts[roll]++; for (i = 2; i < 13; i++) printf(“There were %d rolls of %d\n”, counts[i],i); }

  5. Where Are the Variables? Stack D Y N A M I C i; roll; counts Currently not in use Currently not in use Space for 13 ints – pointed to by counts Heap Num “There were %d rolls of %d\n” Static Space

  6. Runtime Stack Activation record for Hanoi; 1 2 3 1 Activation record for Hanoi; 1 3 2 2 Activation record for Hanoi; 1 2 3 3 Activation record for Hanoi; 1 3 2 4 Activation Record for Main Activation record for main

More Related