0

I'm stuck understanding a problem i have and i would appreciate your help. I'm very new to C so please bear with me.

I have the following code:

#include <stdlib.h>
#include <stdio.h>

int h(int value){
    if (value % 2 == 0){
    // (***)
    printf("Even");}
    return 0;
}

void g(double value){
    int i;
    for(i=1; i<value; i++){
        printf("%d", i);
    }
    h(value);
}


int f(int value){
    static int sum;
    sum += value;
    printf("%d", sum + value);
    g(sum);
    return 0;
}

int main(){
   int *p,
   double *q;
   p = (int *)malloc(10*sizeof(int));
   q = (double *)malloc(10*sizeof(double));
   f(1);
   f(2);
   f(3);
}

and i know how to find the output but now i'm being asked to draw how the memory looks at the place (***) in function h(). suppose that the stack starts at the address 100 and the length of return address is 4 bytes . i need to write on the stack the length of each cell containing a memory.

Can anyone teach me how to do this?

hyde
  • 60,639
  • 21
  • 115
  • 176
  • See http://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml for a description of what happens in a C function call. – patthoyts Mar 09 '14 at 12:18
  • How the memory looks depends entirely on compiler choices outside the programmer's control, or indeed consideration. – Pascal Cuoq Mar 09 '14 at 12:39
  • Also **Don't cast the result of malloc() in C**. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Pascal Cuoq Mar 09 '14 at 12:41

0 Answers0