stack

Printing Abstract Syntax Tree, infinite recursion issue

╄→尐↘猪︶ㄣ 提交于 2019-12-11 10:39:58
问题 For the final project in my C programming class, we are implementing a reverse polish notation calculator which can either evaluate an expression for correctness, return the corresponding infix expression, or print out mock assembly code. To do so, we are to implement both a stack and a Abstract Syntax Tree. struct snode /*stack data structure */ { int datum; struct snode* bottom; }; struct tnode /*tree data structure */ { int datum; struct tnode* left; struct tnode* right; }; Currently, I've

Stack allocation inside a loop

梦想与她 提交于 2019-12-11 10:14:19
问题 In C, when you write code like this: void some_function(void) { while (something) { char buf[4096]; ... } } Does the allocation of buf happen when the function is called? Or does a separate allocation happen for every iteration of the loop? Would there be any performance gain if I put the declaration of buf outside of the loop (i.e. at the beginning of the function)? 回答1: The buf is allocated in the frame of some_function on the stack. It only gets allocated once when some_function is called.

Dilemma related to function call's increment of SP

China☆狼群 提交于 2019-12-11 09:18:23
问题 In case of push during function call, why the stack pointer moves to a smaller value by subtracting 4 times the number of registers to be pushed on the stack? I got this while reading Understanding the stack 回答1: In the same page, it is clearly mentioned about the memory layout of stack :- It's useful to think of the following aspects of a stack. stack bottom The largest valid address of a stack. When a stack is initialized, the stack pointer points to the stack bottom. stack limit The

Order of variables in the stack (GCC) [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-11 08:47:23
问题 This question already has answers here : Order of local variable allocation on the stack (9 answers) Closed last year . When compiling C code in GCC, is there any way to guarantee that the stack variables will appear in the stack in the order i declare them (or in reversed order, doesn't matter to me)? I know this is possible via structs but I would rather not use them. 回答1: The only way would be a struct which includes all variables in the order you like. For local variables the compiler is

Inorder Tree Traversal algorithm for a Binary Search Tree using Stack

僤鯓⒐⒋嵵緔 提交于 2019-12-11 08:30:20
问题 My inputs results 24, 4, 2, 3, 9, 10, 32 , and I am getting following result 2, 3, 4, 24 . I am using a stack . When I have manually checked this program the node is not going through else if at 4 on stack, even if has right sub tree. public void inorderNonRcursive(Node root){ Stack s = new Stack(); Node node = root; Node k; int c=0; if(node != null) { s.push(node); } while(!s.stack.isEmpty()) { //node=(Node) s.stack.get(s.stack.size()-1); System.out.println("first condition" + (node.getleft(

Object return in C++ [duplicate]

落爺英雄遲暮 提交于 2019-12-11 07:38:29
问题 This question already has answers here : Why copy constructor is not called in this case? (4 answers) Copy constructor not called? (2 answers) Closed 6 years ago . When returning object, I thought returning pointer or reference is the right way because temporary object is created when returning an object. In this example, hello() function returns an object of type A, and the returned value is stored in object a. A hello() { A a(20); // <-- A object is created in stack cout << &a << endl;

implementing stack with a linked list in C++ , copy constructor

心不动则不痛 提交于 2019-12-11 07:12:57
问题 I am trying to implement a stack using a linked list in C++, and I don't know how to write correctly the copy constructor of the stack class. I am using the following classes: class Node{ int m_num; Node* m_pNext; public: Node(); ~Node(); //and the standard get&set functions.. } class LinkedList{ int m_size; Node* m_pHead; public: LinkedList(); LinkedList(const LinkedList& obj); ~LinkedList(); //and the standard get&set functions.. } class Stack{ LinkedList m_stack; public: Stack(); Stack

How is memory allocated for stack variables?

坚强是说给别人听的谎言 提交于 2019-12-11 06:37:35
问题 On VS (release), I run the following: int main(void) { char b[] = "123"; char a[] = "1234567"; printf("%x %x\n", b,a); return 0; } I can see that, the mem address of a is b+3(the length of the string). Which shows that the memory are allocated with no gaps. And this guarantee that least memories are used. So, I now kind of believe that all compilers will do so. I want to make sure of this guess here. Can somebody give me an more formal proof or tell me that my guess is rooted on a coincidence

Run-Time Check Failure #0 in embedded asm code

孤者浪人 提交于 2019-12-11 06:37:17
问题 I'm a bit new to assembler, but I'm trying to lookup the parameters from a C++ method in the esp stack, using embedded assembler code. So far I haven't even been able to copy the esp pointer to ebp so I can get a hold on the stack (in case it changes). Even this little piece of code gives me the failure: #include <stdlib.h> int main(int argc, char* argv[]) { __asm { mov ebp, esp } system("pause"); return 0; } After I run this, I get: Run-Time Check Failure #0 - The value of ESP was not

Lock Free stack implementation idea - currently broken

微笑、不失礼 提交于 2019-12-11 06:19:46
问题 I came up with an idea I am trying to implement for a lock free stack that does not rely on reference counting to resolve the ABA problem, and also handles memory reclamation properly. It is similar in concept to RCU, and relies on two features: marking a list entry as removed, and tracking readers traversing the list. The former is simple, it just uses the LSB of the pointer. The latter is my "clever" attempt at an approach to implementing an unbounded lock free stack. Basically, when any