stack

When exactly is stack allocated [duplicate]

▼魔方 西西 提交于 2019-12-08 12:13:10
问题 This question already has answers here : At what exact moment is a local variable allocated storage? (5 answers) Closed 3 years ago . Even in C (not just C++) you can declare variables at the start of a code block, which is enclosed in curly braces. Example: #include <stdio.h> void use_stack(int cnt) { if (cnt<=16) { int a[16]; int i; a[0]=3; a[1]=5; for (i=2;i<cnt;i++) { a[i]=a[i-1]+a[i-2]; } printf("a[%d] == %d\n",cnt-1,a[cnt-1]); } else { printf("cnt is too big\n"); } } Now I know that

Access Violation writing location 0xCDCDCDCD

丶灬走出姿态 提交于 2019-12-08 11:29:58
问题 I am trying to make a small linked list using one structure "students" and another sturcture "stack", which holds the student structure and the pointer to the next element. However i constantly keep getting a memmory access error. I double checked to make sure all pointers are initialized (only one pointer, Stacktop, initialized to NULL) Here are the structure definitions: #include <stdio.h> #include <string> #include <iostream> #include <stdlib.h> using namespace std; struct students { int

Writing a DFS with iterative deepening without recursion

狂风中的少年 提交于 2019-12-08 10:28:34
问题 So currently i have a DFS with the following pseudocode procedure DFS(Graph,source): create a stack S push source onto S mark source while S is not empty: pop an item from S into v for each edge e incident on v in Graph: let w be the other end of e if w is not marked: mark w push w onto S How do I alter this function to accept a third argument that limits the depth of the search? 回答1: Let Node a structure for each node of the graph, add a field called level and then: procedure DFS(Graph

Reducing stack load, memory allocation in C and easly casting malloc()'s return value

て烟熏妆下的殇ゞ 提交于 2019-12-08 10:02:30
问题 It's known that big local/global variables may cause to a stack overflow. I know that using pointers and allocating space in memory helps to overcome this problem. But is it the only option? What happens if I have (or need) too-many pointers in global scope? Regarding the stack space: Is a global struct-type variable takes space in the stack, or acts like a pointer? Do I need to create a pointer of a struct variable type in order to reduce the stack load? Does the following code allocates

Pseudo Range Minimum Query

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 09:29:23
问题 I have a problem with my assignment which requires me to solve a problem that is similar to range-minimum-query. The problem is roughly described below: I am supposed to code a java program which reads in large bunch of integers (about 100,000) and store them into some data structure. Then, my program must answer queries for the minimum number in a given range [i,j]. I have successfully devised an algorithm to solve this problem. However, it is just not fast enough. The pseudo-code for my

Segmentation fault for pthreads in a recursive call

老子叫甜甜 提交于 2019-12-08 08:50:41
问题 Given the code below, I get a segmentation fault if I run it with n>16. I think it has something to do with the stack, but I can't figure it out. Could anyone give me a hand? The code is not mine, and really not important. I would just like someone to give me a hand with what is happening. This SO question is very similar, but there's not enough information (the person who posts the answer briefly talks about the problem, but then goes on to talk about a different language). Besides, notice

Why do we have to reverse the string when converting from infix to prefix

梦想的初衷 提交于 2019-12-08 08:12:39
问题 In the first step itself of converting an infix to prefix can someone explain in simple terms why should we reverse the string? Is there any alternative method to convert? 回答1: Yes, you are absolutely right that if you have to convert infix to prefix then you have to scan the string from right to left. Why not from left to right? If you scan from left to right then you will require future knowledge of operators in the string. Example 1 : Infix : 2+3 Prefix : +23 Now, when you convert it from

%sp register doesn't point to stack

有些话、适合烂在心里 提交于 2019-12-08 07:40:58
问题 I'm writing on a very basic kernel. I tried to write a function, with parameters passed through the stack. The kernel is compiled with nasm (like described in this question) and run with QEMU. I'm using gdb for debugging. After a long while having problems I wrote this to test some basic stack operations: BITS 16 global start start: mov ax, 0x7C00 add ax, 288 mov ss, ax mov sp, 4096 mov ax, 0x7C00 mov ds, ax test: push 42 push 43 push "T" pop ax pop ax push 44 pop ax pop ax jmp $ Going

Method to “remember” past entries

爷,独闯天下 提交于 2019-12-08 07:24:29
问题 Part of an application I'm building in VB has terminal functionality, and I'd like to add functionality to it so it remembers past commands in chronological order, similarly to how a windosw terminal works. In short I'd like you to be able to press the up arrow when the text area is focused and be able to cycle through a list of commands that were entered previously. I had two ideas for how to do this: A combobox that, when you hit enter, reads whatever is in combobox.text, whether that be a

Recursive Stack in C

ⅰ亾dé卋堺 提交于 2019-12-08 07:14:32
问题 during recursion a stack is created what does that stack contains, does it contains the values or it stores the addresses of the operands void recursiveReverse(struct node** head_ref) { struct node* first; struct node* rest; /* empty list */ if (*head_ref == NULL) return; /* suppose first = {1, 2, 3}, rest = {2, 3} */ first = *head_ref; rest = first->next; /* List has only one node */ if (rest == NULL) return; /* reverse the rest list and put the first element at the end */ recursiveReverse(