Accessing data in the program stack

This is a discussion on Accessing data in the program stack within the C Programming forums, part of the General Programming Boards category; Little backgound. I'm working on a homework project, don't worry I don't just want it done for me. It need ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Post Accessing data in the program stack

    Little backgound. I'm working on a homework project, don't worry I don't just want it done for me. It need to have two procedures which take no input when called and return no data. They share a memory location and that hold one integer value. The point is to have these procedures called in any randon order have them call int peek() to retrive the value in the shared mem. And call void poke(int value) to put the next value in. Once poked that value is output to the screen. Here is the trouble. It needs to print out the sequence 1,2,3,....,10,9,8,....,1,0. There can be no static variables. My instructor provided peek, poke and the random function caller. The two procedures will be identical. I know that one will be called, placed on the stack and then remoced from the stack. Then the next one will be called and go into that recently vactaed stack space. I want to leave a bool on the stack to tell the procedures whether or not to poke the next higher or lower number than it peeked.

    Question1: Is my reasoning sound? I'm pretty sure it is, but if you can tell why not, I'll believe you.

    Question two: How can I access that bool value that I want to leave on the stack without passing anything between the procedures?

    This is must compile on a linux box, with gcc.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Solved

    To all interested parties,
    I solved the problem. I know it looks simple but the solution was difficult to determine.

    void poke(int value);
    int peek(void);

    void process1(void){

    int curVal;
    int newVal;
    int upordown[10];

    curVal = peek();
    newVal = curVal;

    if (curVal == 0) upordown[0] = 0;
    if (curVal == 10) upordown[0] = 1;
    if(upordown[0] == 0) newVal++;
    if(upordown[0] == 1) newVal--;

    poke(newVal);
    }

    void process2(void){

    int curVal;
    int newVal;
    int upordown[10];

    curVal = peek();
    newVal = curVal;

    if (curVal == 0) upordown[0] = 0;
    if (curVal == 10) upordown[0] = 1;
    if(upordown[0] == 0) newVal++;
    if(upordown[0] == 1) newVal--;

    poke(newVal);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21