Question

C PROGRAM The following is code prints the current activation record number, the memory address of...

C PROGRAM

The following is code prints the current activation record number, the memory address of the current array, followed by the estimated size of the current activation record as a distance between the current array address and the array address from the previous activation record. I need it to run until a segmentation fault occurs and also it must print the estimated size of the runtime stack as a product of the size of current activation record and the total count of activation records so far.

(Please help if you can)

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000

void f1(){


char values[SIZE];
static int n = 0;
long int addr = (long int )&values[0];
static int i=0;
n++;
i++;
printf("Call #%d",n);
addr = (long int )&values[n];
printf(" at %p\n",addr);
printf("AR size #%d",n);
printf(" is : %d\n",((long int)&values[n] - (long int)&values[n-1]));
}

void main()
{
for (int i=0; i<=10; i++)
{
f1();
}
for (int i=0; i<=100000; i++)
{
f2();
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

include <stdio.h>
#include <stdlib.h>
#define SIZE 1000

void f1()
{
int x;
char values[SIZE];
static int n=0;
values[n]=n;
char *addr;
static int i=0;
addr=&values[n];
n++;
i++;
printf("Call #%d",n);
addr = &values[n];
printf(" at %p\n",addr);
printf("AR size #%d",n);
printf(" is : %p\n",(&values[n]-&values[n-1]));
x=sizeof(addr)*n;
printf("Estimated Size of Runtime stack :%d\n",x);
}
void main()
{
for (int i=0; i<=100000; i++)
{
f1();
}
}

/*Segmentation fault occurs when we try to access that part of the memory which is not accessible to us and is allowed for reading purpose only.

So what i did above is i filled whole array with the values of n and when it becomes full we tried to access that part of memory which is not accessible to us because we declared size of array 1000 so this much memory is accessible to us and trying to access more will result in segmentation fault(core dump).*/

Add a comment
Know the answer?
Add Answer to:
C PROGRAM The following is code prints the current activation record number, the memory address of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following code. Assume that the array begins at memory address Ox200. What is printed...

    Consider the following code. Assume that the array begins at memory address Ox200. What is printed by the following code. Assume sizeof(int) is 4 bytes and sizeof(char) is 1 byte. Do not include the Ox or leading zeros in your answer. Enter -1 if the answer is indeterminate, or -2 if the code generates a compile error, or -3 if the code generates a runtime error. #include <stdio.h> int main() { int a[] {1,2,3}; int *iptr a; printf("%p\n", iptr+1); return...

  • I am trying to figure out why my C code is outputting "exited, segmentation fault". The...

    I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...

  • I am writing a program that reads ten integers from standard input, and determines if they...

    I am writing a program that reads ten integers from standard input, and determines if they are going up or down or neither. Can I get some help? I have supplied what I have already. #include <stdio.h> #include <string.h> #include <stdlib.h> int isGoingUp(int [a]); int isGoingDown(int [b]); int main(void) { int array[10]; printf("Enter ten integers : "); for (int a = 0; a < 10; a++) scanf("%d", &array[a]); if (isGoingUp(array) == 1) printf("The values are going up\n"); else if (isGoingDown(array)...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • P1) Write a complete C program that prints out the word YES, if its string command...

    P1) Write a complete C program that prints out the word YES, if its string command line argument contains the sequence the somewhere in it. It prints out the word NO otherwise. Both the word the and partial sequences like in the words theatre or brother qualify. Note: You can use string functions or not but if you do the only ones allowed are strcpy and strlen. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P2) What is the output of the following program (one answer per...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • 9. The purpose of the following program is to generate 10 random integers, store them in...

    9. The purpose of the following program is to generate 10 random integers, store them in an array, and then store in the freq frequency array, the number of occurrences of each number from 0 to 9. Can you find any errors in the program? If yes, correct them. #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 int main(void) int i, num, arr[SIZE], freq[SIZE]; srand (time (NULL)); arr[1] rand(); SIZE; for(i i 〈 i++) num arr[i]; freq[num]++; printf("InNumber occurrences...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT