Question

Considering the following code fragment: #include<stdio.h> #include <stdlib.h> int base = 0; static int bonus; int*...

Considering the following code fragment:

#include<stdio.h>

#include <stdlib.h>

int base = 0;

static int bonus;


int* demage (int dmg) {

int* ptr = (int*) malloc(sizeof(int));

static int constant = 1;

  *ptr = dmg + constant;

return ptr;

}


int main() {
int HP = 0;

int* dm;

base = 30;

bonus = 20;

for(int i=1; i<4; i++) { HP += i * base; }

dm = demage(10);

HP += bonus - *dm;

printf("Total HP: %i", HP);

return 0;

}

Please indicate which memory segment each of the following values is allocated to.

Homework Answers

Answer #1

The genral rule is that local variables are always stored in stack memory and static, global and dynamic variabes use heap memory location.

So in the above code

  1. base is global variable -> heap
  2. bonus is static -> heap
  3. dmg is a function parameter , all parameters are in local scope and hence use stack
  4. ptr is dynamically allocated variable -> heap
  5. *ptr is the pointer having the dynamic memory allocation -> heap
  6. constant is static -> heap
  7. Hp is local ->stack
  8. dm is also local -> stack
Know the answer?
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
Similar Questions
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8;...
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8; int x[8] = {2, 3, 5, 7, 11, 13, 17, 19}; scanf("%d",&k); aux = x[k]; for (int i = k; i < size - 1; i++) x[i] = x[ i + 1]; x[ size - 1] = aux;   for (int i = 0; i < size; i++) printf("%d ", x[i]); } change this program to Write a program to remove an element from an...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2;...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } There is an issue with this...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include <stdio.h> #define MAX_STR_LEN 100 struct Node { char *data; struct Node *left; struct Node *right; }; struct Node *new_node(char *d) { struct Node *r = malloc(sizeof(struct Node)); r->data = malloc(sizeof(char) * MAX_STR_LEN); strncpy(r->data, d, MAX_STR_LEN); free(d); r->left = NULL; r->right = NULL; return r; } void freeSubTree(struct Node *r) { free(r->data); if (r->left != NULL) freeSubTree(r->left); if (r->right != NULL) freeSubTree(r->right); free(r); } void...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
Expand the code below to display both the value of the loop variable and this value...
Expand the code below to display both the value of the loop variable and this value squared. Put the pairs of values on a new line. #include <stdio.h> #include <stdlib.h> int main(void) { int i; int a; int b; do { printf("Please enter the lower value: "); scanf("%d", &a); printf("Please enter the upper value: "); scanf("%d", &b); if (a>b) printf("The upper value must be greater than the lower value\n\n"); } while (a>b); for(i=a; i<=b; i++) printf("%d ", i); return 0;...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...