stack-memory

How to create a struct on the stack in C?

℡╲_俬逩灬. 提交于 2019-11-28 20:47:06
问题 I understand how to create a struct on the heap using malloc . Was looking for some documentation regarding creating a struct in C on the stack but all docs. seem to talk about struct creation on heap only. 回答1: The same way you declare any variable on the stack: struct my_struct {...}; int main(int argc, char **argv) { struct my_struct my_variable; // Declare struct on stack . . . } 回答2: To declare a struct on the stack simply declare it as a normal / non-pointer value typedef struct { int

Creating Objects on the stack memory in java ?

荒凉一梦 提交于 2019-11-28 20:44:10
This is just a simple theoretical question out of curiosity. I have always been like a java fan boy. But one thing makes me wonder why java does not provide mechanism for creating objects on the stack ? Wouldn't it be more efficient if i could just create small Point(int x,int y ) object on the stack instead of the heap like creating a structure on C# . Is there any special security reason behind this restriction in java ? :) The strategy here is that instead of leaking this decision into the language, Java lets the JVM/Hotspot/JIT/runtime decide where and how it wants to allocate memory.

Stack vs heap allocation of structs in Go, and how they relate to garbage collection

a 夏天 提交于 2019-11-28 14:57:26
I'm new to Go and I'm experiencing a bit of congitive dissonance between C-style stack-based programming where automatic variables live on the stack and allocated memory lives on the heap and and Python-style stack-based-programming where the only thing that lives on the stack are references/pointers to objects on the heap. As far as I can tell, the two following functions give the same output: func myFunction() (*MyStructType, error) { var chunk *MyStructType = new(HeaderChunk) ... return chunk, nil } func myFunction() (*MyStructType, error) { var chunk MyStructType ... return &chunk, nil } i

Creating Objects on the stack memory in java ?

元气小坏坏 提交于 2019-11-27 13:06:55
问题 This is just a simple theoretical question out of curiosity. I have always been like a java fan boy. But one thing makes me wonder why java does not provide mechanism for creating objects on the stack ? Wouldn't it be more efficient if i could just create small Point(int x,int y ) object on the stack instead of the heap like creating a structure on C# . Is there any special security reason behind this restriction in java ? :) 回答1: The strategy here is that instead of leaking this decision

Stack vs heap allocation of structs in Go, and how they relate to garbage collection

我是研究僧i 提交于 2019-11-27 09:08:47
问题 I'm new to Go and I'm experiencing a bit of congitive dissonance between C-style stack-based programming where automatic variables live on the stack and allocated memory lives on the heap and and Python-style stack-based-programming where the only thing that lives on the stack are references/pointers to objects on the heap. As far as I can tell, the two following functions give the same output: func myFunction() (*MyStructType, error) { var chunk *MyStructType = new(HeaderChunk) ... return

How is heap and stack memories managed, implemented, allocated?

倖福魔咒の 提交于 2019-11-27 08:43:18
In C/C++ we can store variables, functions, member functions, instances of a class either on a stack or a heap. How is each implemented? How is it managed (high level)? Does gcc preallocates a chunk of memory to be used for the stack and heap, and then doles out on request? Is original memory coming from RAM? Can a function be allocated on the heap instead of a stack? Clarification I am really asking about implementation and management of heap and stack memories. After reading referenced question, I didn't find anything that addresses that... thanks for the link Modern operating systems do not

How is heap and stack memories managed, implemented, allocated?

烂漫一生 提交于 2019-11-26 14:16:11
问题 In C/C++ we can store variables, functions, member functions, instances of a class either on a stack or a heap. How is each implemented? How is it managed (high level)? Does gcc preallocates a chunk of memory to be used for the stack and heap, and then doles out on request? Is original memory coming from RAM? Can a function be allocated on the heap instead of a stack? Clarification I am really asking about implementation and management of heap and stack memories. After reading referenced

How are multi-dimensional arrays formatted in memory?

白昼怎懂夜的黑 提交于 2019-11-26 00:07:01
问题 In C, I know I can dynamically allocate a two-dimensional array on the heap using the following code: int** someNumbers = malloc(arrayRows*sizeof(int*)); for (i = 0; i < arrayRows; i++) { someNumbers[i] = malloc(arrayColumns*sizeof(int)); } Clearly, this actually creates a one-dimensional array of pointers to a bunch of separate one-dimensional arrays of integers, and \"The System\" can figure out what I mean when I ask for: someNumbers[4][2]; But when I statically declare a 2D array, as in