stack

Why is memory allocation on heap MUCH slower than on stack?

醉酒当歌 提交于 2019-12-28 08:07:49
问题 I have been told this many times. But I don't know WHY...What extra cost is involved when allocating memory from heap? Is it hardware related? Is it related to CPU cycles? So many guesses but no exact answers...Could someone give me some elaboration? Just as "unwind" said, the Heap data structure is more complicated than Stack. And In my opinion, some memory space is allocated to a thread as its Stack when it starts to run, while the heap is shared by all the threads within a process. This

free() on stack memory

泄露秘密 提交于 2019-12-28 06:46:33
问题 I'm supporting some c code on Solaris, and I've seen something weird at least I think it is: char new_login[64]; ... strcpy(new_login, (char *)login); ... free(new_login); My understanding is that since the variable is a local array the memory comes from the stack and does not need to be freed, and moreover since no malloc/calloc/realloc was used the behaviour is undefined. This is a real-time system so I think it is a waste of cycles. Am I missing something obvious? 回答1: You can only free()

Store results of std::stack .pop() method into a variable

限于喜欢 提交于 2019-12-28 05:57:10
问题 I'd like to do the following: std::stack <int> s; int h = 0; s.push(2); h = s.pop(); Such as to have h hold the value 2. When I try my method, I get “void value not ignored as it ought to be”. Is this not the intention of the .pop() method? What is the preferred way to do this? 回答1: The standard library containers separate top() and pop() : top() returns a reference to the top element, and pop() removes the top element. (And similarly for back() / pop_back() etc.). There's a good reason for

How to group android notifications like whatsapp?

≡放荡痞女 提交于 2019-12-28 02:42:10
问题 I don´t know how to group two or more notifications into only one and show a message like "You have two new messages". 回答1: Steps to be taken care from the below code. NotificationCompat.Builder:contains the UI specification and action information NotificationCompat.Builder.build() :used to create notification (Which returns Notification object) Notification.InboxStyle: used to group the notifications belongs to same ID NotificationManager.notify():to issue the notification. Use the below

Fields of class, are they stored in the stack or heap?

99封情书 提交于 2019-12-27 19:12:10
问题 I saw a question yesterday which raised (for me) another question. Please look at the following code: public class Class1 { int A; //as I uderstand, int is value type and therefore lives in the stack } class Class2 { Run() { Class1 instance1 = new Class1(); instance1.A = 10; //it points to value type, but isnt this reference (on heap)? } } Or while creating the instance of Class1, its field types are created on the heap as well? But then I do not understand when it would really be on the

How to evaluate an infix expression in just one scan using stacks?

删除回忆录丶 提交于 2019-12-27 11:11:23
问题 I want to know if there is a way to solve infix expressions in a single pass using 2 stacks? The stacks can be one for operator and the other for operands... The standard way to solve by shunt-yard algorithm is to convert the infix expression to postfix(reverse polish) and then solve. I don't want to convert the expression first to postfix. If the expression is like 2*3-(6+5)+8 , how to solve? 回答1: Quite late, but here is the answer. Take two stacks: operator stack { for operators and

Is a Java array of primitives stored in stack or heap?

﹥>﹥吖頭↗ 提交于 2019-12-27 10:43:08
问题 I have an array declaration like this: int a[]; Here a is an array of primitive int type. Where is this array stored? Is it stored on heap or stack? This is a primitve type int , all primitive types are not stored on heap. 回答1: As gurukulki said, it's stored on the heap. However, your post suggested a misunderstanding probably due to some well-intentioned person propagating the myth that "primitives always live on the stack". This is untrue. Local variables have their values on the stack, but

Memory Allocation (Pointers and Stacks)

北慕城南 提交于 2019-12-26 06:49:10
问题 I've created a stack of pointers, which is being used to create a binary tree. While I can fill the stack with individual nodes, upon trying to allocate the top node's memory to a new node so I can create an actual tree, it segfaults. As an example: TreeNode *c = new TreeNode; c = stack.top(); //this segfaults I'm not sure if I'm misunderstanding how this works, but since both are of the same type, shouldn't c be able to equal the top of the stack? I've been stuck on this for hours now. 回答1:

Memory Allocation (Pointers and Stacks)

混江龙づ霸主 提交于 2019-12-26 06:49:04
问题 I've created a stack of pointers, which is being used to create a binary tree. While I can fill the stack with individual nodes, upon trying to allocate the top node's memory to a new node so I can create an actual tree, it segfaults. As an example: TreeNode *c = new TreeNode; c = stack.top(); //this segfaults I'm not sure if I'm misunderstanding how this works, but since both are of the same type, shouldn't c be able to equal the top of the stack? I've been stuck on this for hours now. 回答1:

Variable arguments in C functions

只愿长相守 提交于 2019-12-25 19:04:44
问题 I've read about variable arguments functions " int func(int, ...) ". Where do the arguments of these functions get allocated (stack or heap)? Because I read that the va_end() macro frees space assigned to va_list , so that word "frees" caught my eyes. Note: I know that regular functions go to stack, but this type of function is interesting as the number of arguments is not known. I just want to know for sure that it's not like arrays with no pre-defined space; we use malloc() and free() at