allocation

How does gcc push local variables on to the stack?

送分小仙女□ 提交于 2021-02-19 08:54:30
问题 void f () { int a[1]; int b; int c; int d[1]; } I have found that these local variables, for this example, are not pushed on to the stack in order. b and c are pushed in the order of their declaration, but, a and d are grouped together. So the compiler is allocating arrays differently from any other built in type or object. Is this a C/C++ requirement or gcc implementation detail? 回答1: The C standard says nothing about the order in which local variables are allocated. It doesn't even use the

Memory allocations in Julia

天大地大妈咪最大 提交于 2021-02-18 21:01:44
问题 I'm extremely dissatisfied after translating a program from Python to Julia: for small/very small inputs, Python is faster for medium inputs, Julia is faster (but not that much) for big inputs, Python is faster I think the reason is that I don't understand how memory allocation works (autodidact here, no CS background). I would post my code here but it is too long and too specific and it would not be beneficial for anybody but me. Therefore I made some experiments and now I have some

What does the “new ” keyword in .net actually do?

微笑、不失礼 提交于 2021-02-18 05:24:39
问题 I know that the new keyword is calling the class constructor but at which stage do we allocate memory for the class? In my understanding it should correspond to the GCHandle.Alloc(Object) method but I'm unable to find the connection. 回答1: The new operator is implemented in the CLR. It allocates memory from the garbage collected heap and executes the class constructor. GCHandle.Alloc() is not the same. That takes advantage of a separate mechanism in the GC to create references to objects,

Trying to code Graph in c++, getting bad_alloc some of the time

不问归期 提交于 2021-02-08 12:01:20
问题 I'm new to c++ after learning basic Object Oriented Programming in Java so I'm having a difficult time grasping memory deallocation. The assignment was to create a Weighted Directed Graph... I'm getting the error: "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" when I run certain inputs through my code, and I'm having a difficult time figuring out what is causing it. I googled the error and found that it was a memory problem, so I attempted to go

Is char pointer address initialization necessary in C?

百般思念 提交于 2021-02-07 20:21:05
问题 I'm learning C programming in a self-taught fashion. I know that numeric pointer addresses must always be initialized, either statically or dynamically. However, I haven't read about the compulsory need of initializing char pointer addresses yet. For example, would this code be correct, or is a pointer address initialization needed? char *p_message; *p_message = "Pointer"; 回答1: I'm not entirely sure what you mean by "numeric pointer" as opposed to "char pointer". In C, a char is an integer

c6386 buffer overrun while writing

回眸只為那壹抹淺笑 提交于 2021-01-29 16:20:28
问题 Function Name: expandStack Input: a pointer to a Stack type (Stack*) Output: none Function Operation: The function expands a stack void expandStack(Stack* stack){ //Check the stack and the array are allocated if (stack == NULL ||stack->content == NULL) { return; } //Allocating a new sized array (*2 from the previous) Element* expandedStack = (Element*)malloc(2 * (stack->size) * sizeof(Element)); //Case malloc failed if (expandedStack == NULL) { printf("Error! Malloc has failed in file 'stack

std::string allocate memory 2 times for 1 string

烂漫一生 提交于 2021-01-27 05:40:34
问题 #include <iostream> #include <string> void* operator new(size_t size) { std::cout << "Allocated: " << size << " Bytes\n"; return malloc(size); } void operator delete(void* var) { std::cout << "Deleted\n"; free(var); } int main() { std::string name0 = "Ahmed Zaki Marei"; //std::string name1 = "Lara Mohammed"; std::cout << name0 << "\n"; //std::cout << name1 << "\n"; } When I try to run this code it gives me this output: Allocated: 8 Bytes Allocated: 32 Bytes Ahmed Zaki Marei Deleted Deleted

Double pointer array in c++

丶灬走出姿态 提交于 2021-01-20 13:41:43
问题 I was reading a program about BTree, there I came across this : BTreeNode **C . I understand that it is a 2d array but it was initialized as C=new BTreeNode *[2*t]; . I can't understand this: is this a 2d array with dynamic rows and 2t columns ? Thanks. 回答1: You probably well know that double* is a pointer to a double element. In the same way, double** is a pointer to a double* element, which is itself a pointer. Again, double*** is a pointer to a double** element, and so on. When you

Double pointer array in c++

ぐ巨炮叔叔 提交于 2021-01-20 13:41:09
问题 I was reading a program about BTree, there I came across this : BTreeNode **C . I understand that it is a 2d array but it was initialized as C=new BTreeNode *[2*t]; . I can't understand this: is this a 2d array with dynamic rows and 2t columns ? Thanks. 回答1: You probably well know that double* is a pointer to a double element. In the same way, double** is a pointer to a double* element, which is itself a pointer. Again, double*** is a pointer to a double** element, and so on. When you

Freeing memory error?

天大地大妈咪最大 提交于 2020-12-23 11:55:41
问题 I need to free the bitpointer , because this function is executed multiple times and memory usage is growing for a reason I don't understand and it crashes after reaching 22mb of ram usage.. If I try to delete the bitpointer like this delete []bitpointer or free(bitpointer) I get access violation error. But I don't understand why, because the function shouldn't use the pointer any more and the new red blue green values are set.. void Get_Color(int x,int y,int w,int h,int &red,int &green,int