Exploring Pop & Push: Memory Requirements in C

In summary, we discussed the algorithms Pop and Push of a stack. We learned that in C, S=P does not work because S is passed by value instead of by reference. To make it work, we would need to pass a pointer to a pointer or use a reference in C++. We also talked about the required memory for the algorithms, which includes the memory for passing the parameter S, the local variable x, and the stack frame. Additionally, we discussed the concept of a stack frame and how it is used in memory management. Finally, we touched on the memory requirements for a RAM machine and how they affect the execution of a function.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Smile)

I am looking at the algorithms [m]Pop[/m] and [m]Push[/m] of a stack:

Code:
void Push(info x, pointer S)
       pointer P; /* temporary pointer */
       P=NewCell(NODE); /* malloc() */
       P->data=x;
       P->next = S;
       S=P; /* This wouldn't give the right result in C*/

Why doesn't [m]S=P[/m] work in C?

Code:
info Pop(pointer S)
      info x;
      if (IsEmptyStack(S)) then error;
      else
           x=Top(S);
           S=S->next;
      return x;

How can we find how much memory the algorithm [m]Pop[/m] needs? (Thinking)
 
Technology news on Phys.org
  • #2
Hey! (Smile)

evinda said:
Code:
void Push(info x, pointer S)
       ...
       S=P; /* This wouldn't give the right result in C*/

Why doesn't [m]S=P[/m] work in C?

That is because S is passed by value instead of by reference.
Changing S won't change the original pointer that was passed to the function. (Nerd)

To make it work in C, we would need:
Code:
void Push(info x, pointer *pS)
       ...
       *pS=P; /* This will give the right result in C*/
This means the pS is a pointer to a pointer, giving us the opportunity to change the original pointer. (Wasntme)

Alternatively, we can do in C++:
Code:
void Push(info x, pointer& S)
       ...
       S=P; /* This will give the right result in C++ "if" we use a reference */
The symbol & means that S is passed by reference.

Code:
info Pop(pointer S)
      info x;
      if (IsEmptyStack(S)) then error;
      else
           x=Top(S);
           S=S->next;
      return x;

How can we find how much memory the algorithm [m]Pop[/m] needs? (Thinking)

Counting the memory may depend on the course you are following.
If we try to account for everything (and many courses don't), we have:
  • The required memory to pass the parameter S (1 unit).
  • The required memory for the local variable x (sizeof info).
  • The required memory for a stack frame and the return address (in practice 2 units, but this may depend on your course that may choose to neglect this).
  • The maximum of the required memory for IsEmptyStack(S) and Top(S).
(Wasntme)
 
  • #3
I like Serena said:
Hey! (Smile)
That is because S is passed by value instead of by reference.
Changing S won't change the original pointer that was passed to the function. (Nerd)

To make it work in C, we would need:
Code:
void Push(info x, pointer *pS)
       ...
       *pS=P; /* This will give the right result in C*/

A ok.. (Nod)

I like Serena said:
This means the pS is a pointer to a pointer, giving us the opportunity to change the original pointer. (Wasntme)

Could you explain me why pS is a pointer to a pointer? :confused:

I like Serena said:
Alternatively, we can do in C++:
Code:
void Push(info x, pointer& S)
       ...
       S=P; /* This will give the right result in C++ "if" we use a reference */
The symbol & means that S is passed by reference.

Aha.. interesting! (Nerd)

I like Serena said:
Counting the memory may depend on the course you are following.
If we try to account for everything (and many courses don't), we have:
  • The required memory to pass the parameter S (1 unit).
  • The required memory for the local variable x (sizeof info).
  • The required memory for a stack frame and the return address (in practice 2 units, but this may depend on your course that may choose to neglect this).
  • The maximum of the required memory for IsEmptyStack(S) and Top(S).
(Wasntme)

Could you explain me what a stack frame is? (Thinking)

According to my notes, the memory is: inputs & (n+1) pointers (if the stack has n elements).. (Talking)
 
  • #4
evinda said:
Could you explain me why pS is a pointer to a pointer? :confused:

Suppose we have the following code.
Code:
struct Node {
  int value;
};

Node node;
Node *pNode = &node;
Node *ppNode = &pNode;

Then [m]node[/m] is a global object, [m]pNode[/m] is a pointer to that node, and [m]ppNode[/m] is a pointer to a pointer to that node.
Could you explain me what a stack frame is? (Thinking)

The stack is some space in the memory of the computer where all parameters to functions, and all local variables go.
It's called a stack, because we "push" a function call on top of the stack together with all its parameters and local variables.
And when the function call is done, they all get "popped" again from the top.

When the function call is made, the computer needs to keep track where those parameters and local variables actually are, and also to which address he should return when the function call is done. To do that, a marker is also pushed onto the stack, together with the return address.
These make up the so called stack frame. (Nerd)
According to my notes, the memory is: inputs & (n+1) pointers (if the stack has n elements).. (Talking)

That sound like the total memory or something of a RAM machine.
A RAM machine keeps track of $n$ registers, and additionally it will get inputs that it will read.
If those are limited, we need to figure out how many of those a function will need, before we know if that function can be executed by the RAM machine. (Wasntme)
 
  • #5
I like Serena said:
Suppose we have the following code.
Code:
struct Node {
  int value;
};

Node node;
Node *pNode = &node;
Node *ppNode = &pNode;

Then [m]node[/m] is a global object, [m]pNode[/m] is a pointer to that node, and [m]ppNode[/m] is a pointer to a pointer to that node.

Code:
void Push(info x, pointer S)
       pointer P; /* temporary pointer */
       P=NewCell(NODE); /* malloc() */
       P->data=x;
       P->next = S;
       S=P;
I thought that when the function is like that in C, S is not a pointer, but only if the parameter would be *S.
So, why is *S a pointer to a pointer? (Worried)
I like Serena said:
The stack is some space in the memory of the computer where all parameters to functions, and all local variables go.
It's called a stack, because we "push" a function call on top of the stack together with all its parameters and local variables.
And when the function call is done, they all get "popped" again from the top.

When the function call is made, the computer needs to keep track where those parameters and local variables actually are, and also to which address he should return when the function call is done. To do that, a marker is also pushed onto the stack, together with the return address.
These make up the so called stack frame. (Nerd)

I haven't understood it... (Sweating) Could you explain it further to me? :confused:
I like Serena said:
That sound like the total memory or something of a RAM machine.
A RAM machine keeps track of $n$ registers, and additionally it will get inputs that it will read.
If those are limited, we need to figure out how many of those a function will need, before we know if that function can be executed by the RAM machine. (Wasntme)

So, what do we have to count, in order to find the needed memory? (Worried)
 
  • #6
evinda said:
I thought that when the function is like that in C, S is not a pointer, but only if the parameter would be *S.
So, why is *S a pointer to a pointer? (Worried)

There has to be a typedef:
Code:
typedef Node *pointer;
It means that when we refer to [m]pointer S[/m] it is actually [m]Node *S[/m].
And [m]pointer *pS[/m] is actually [m]Node **pS[/m].
I haven't understood it... (Sweating) Could you explain it further to me? :confused:

What is it that you do not understand? :confused:

To be fair, this is a concept that may requires a bit more than only a couple of lines to explain. (Tauri)
So, what do we have to count, in order to find the needed memory? (Worried)

What do you think we have to count? (Wondering)
 
  • #7
I like Serena said:
There has to be a typedef:
Code:
typedef Node *pointer;
It means that when we refer to [m]pointer S[/m] it is actually [m]Node *S[/m].
And [m]pointer *pS[/m] is actually [m]Node **pS[/m].

So, you mean that in C, it should be like that?

Code:
void Push(info x, pointer *S)
       pointer *P; /* temporary pointer */
       P=NewCell(NODE); /* malloc() */
       P->data=x;
       P->next = S;
       **S=P;

Or have I understood it wrong? :confused:
I like Serena said:
What do you think we have to count? (Wondering)
How can we figure out how many registers and inputs the function needs in our case? (Thinking)
 
  • #8
evinda said:
So, you mean that in C, it should be like that?

Code:
void Push(info x, pointer *S)
       pointer *P; /* temporary pointer */
       P=NewCell(NODE); /* malloc() */
       P->data=x;
       P->next = S;
       **S=P;

Or have I understood it wrong? :confused:

Does it compile? (Wondering)

How can we figure out how many registers and inputs the function needs in our case? (Thinking)

How many registers do you count that would be needed, counting the things we can? (Wondering)
 
  • #9
I like Serena said:
Does it compile? (Wondering)

I don't think so... (Sweating)



I like Serena said:
How many registers do you count that would be needed, counting the things we can? (Wondering)

How can we count how many registers are needed? (Worried)
 
  • #10
evinda said:
I don't think so... (Sweating)

Then what's going wrong? (Wondering)
How can we count how many registers are needed? (Worried)

We need 1 register to keep the value of the parameter S.
It's not clear how many registers we need for x. That depends on how much information the type [m]info[/m] holds, but can say for now that it is sizeof(info).
Then we call 2 functions that would again take the parameter S.
Those 2 functions probably don't need anything else, so that is 1 more register.
If we ignore the stack frame and return address, that makes for a total of:
$$1 + \text{sizeof}(info) + 1 = \text{sizeof}(info) + 2$$
(Mmm)
 
  • #11
I like Serena said:
Then what's going wrong? (Wondering)

We need 1 register to keep the value of the parameter S.
It's not clear how many registers we need for x. That depends on how much information the type [m]info[/m] holds, but can say for now that it is sizeof(info).
Then we call 2 functions that would again take the parameter S.
Those 2 functions probably don't need anything else, so that is 1 more register.
If we ignore the stack frame and return address, that makes for a total of:
$$1 + \text{sizeof}(info) + 1 = \text{sizeof}(info) + 2$$
(Mmm)

We use three times the value of the parameter S, right? (Thinking)

So, shouldn't it be $\text{sizeof}(info) + 3$?

Or have I understood it wrong? (Worried)
 
  • #12
evinda said:
We use three times the value of the parameter S, right? (Thinking)

So, shouldn't it be $\text{sizeof}(info) + 3$?

Or have I understood it wrong? (Worried)

The first time we're getting S as an input parameter, which takes up 1 register.
The second time, we use S to call IsStackEmpty(S), which takes up another register (although this could be optimized away).
After that we don't need this 2nd register anymore and we can reuse it.
Then, the third time, in the call to Top(S), we can reuse the 2nd register to pass S as a parameter. (Nerd)

Actually, Top(S) would keep x as a local variable before returning it, which would take another $\text{sizeof}(info)$, although that can be optimized away as well. (Thinking)

But to keep things clean we would get: $2\text{ sizeof}(info) + 2$ (excluding stack frame and return address). (Mmm)
 

Related to Exploring Pop & Push: Memory Requirements in C

1. What is "Exploring Pop & Push: Memory Requirements in C"?

"Exploring Pop & Push: Memory Requirements in C" is a research study that investigates the memory requirements of the pop and push functions in the C programming language. It aims to understand how these functions impact the memory usage of a program and provide insights for optimizing memory usage in C programs.

2. Why is it important to study the memory requirements of pop and push in C?

Understanding the memory requirements of pop and push is crucial for developing efficient and optimized C programs. These functions are commonly used in data structures, such as stacks and queues, and can significantly impact the performance of a program. By studying their memory requirements, we can identify potential memory issues and improve the overall efficiency of our code.

3. What methods were used in this study?

This study used experimental methods to measure the memory usage of pop and push functions in C. A series of tests were conducted on different data structures and input sizes to gather data on the memory requirements. The results were then analyzed to identify patterns and trends.

4. What were the main findings of this study?

The study found that the memory requirements of pop and push in C are dependent on the data structure being used and the size of the input. In general, push requires more memory than pop, and both functions have higher memory usage with larger input sizes. It also identified potential memory issues, such as stack overflow, when using these functions in certain scenarios.

5. How can these findings be applied in practice?

The findings of this study can be applied in practice by helping developers optimize the memory usage of their C programs. By understanding the memory requirements of pop and push, developers can make informed decisions when choosing data structures and input sizes for their programs. This can lead to more efficient and optimized code, resulting in better performance and potentially reducing the risk of memory issues.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
695
  • Programming and Computer Science
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
2
Views
411
Back
Top