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
Here is my code: //preprocessor files #include <stdio.h> #include <stdlib.h> void printScores(float scores[]); int main() {...
Here is my code: //preprocessor files #include <stdio.h> #include <stdlib.h> void printScores(float scores[]); int main() {    char s_name[] = "ECPI University";    float scores[6] = {78.7, 98.4, 100.0, 96.5, 100.0, 88.8};    float average = 0.0;    int counter;       printScores(scores);       for(counter = 0; counter < 6; counter++){        average += scores[counter];    }         average /= float(6); ===============this is where i am getting the error          printf("\nAt %s, your class average...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
could someone please explain this code and the output to me? #include <stdio.h> #include <unistd.h> int...
could someone please explain this code and the output to me? #include <stdio.h> #include <unistd.h> int main(void) { printf("one\n"); fork(); printf("two\n"); return 0; }
#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.
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h>...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h> int main() { printf("%d\n", parseint("32")); return 0; } int parseint(char *str) { int i=0; while(str[i] != '\0'){ return (str[i] - '0'); i++; } return 1;    }
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;...
Using this code (below) create a diagram that shows what the heap layout looks like and...
Using this code (below) create a diagram that shows what the heap layout looks like and explain how to exploit it (Include a diagram) Include the following items: Each chunk of memory allocated by malloc() and their metadata. Their sizes in bytes. The overflow direction in the heap. The size of the overflowing buffer to reach and overwrite the metadata. Overflow data that is meaningful for an exploit (this can be general). Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int...