I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at program start time. int globalgarbage; unsigned int anumber = 42; But what about static ones defined within a function? void doSomething() { static bool globalish = true; // ... } When is the space for globalish allocated? I'm guessing when the program starts. But does it get i
我非常有信心,在程序开始时,全局声明的变量会被分配(并且在适用的情况下被初始化)。 int globalgarbage; unsigned int anumber = 42; 但是在函数中定义的静态函数呢? void doSomething() { static bool globalish = true; // ... } globalish分配的空间何时分配? 我猜测节目什么时候开始。 但是它是否也被初始化了? 或者当doSomething()被首次调用时它被初始化了吗? 我对此很好奇,所以我写了下面的测试程
I want to know which variables are stored in stack, heap, etc. Suppose i have a variable, an array , a pointer , etc. Where will these be stored(stack,heap,unknown)? And where are static variables stored. Where will a variable, an array, a pointer be stored when they are declared as static? And where are global variables and variables declared as extern stored. Statics and globals are stored
我想知道哪些变量存储在堆栈,堆等中。假设我有一个变量,一个数组,一个指针等。这些将被存储在哪里(堆栈,堆,未知)? 哪里存储静态变量。 变量,数组和指针在声明为静态时将存储在哪里? 全局变量和变量在哪里声明为extern存储。 静态和全局数据存储在全局数据存储器(不是堆栈或堆)中。 extern对存储有影响。
Lets say I have the following program. Which part of memory is a allocated in? Also is the behavior same for both c and c++ ? // a is allocated in the ? int a[3] = {1, 2, 3}; int main() { // x is allocated in the stack int x[3] = {4, 5, 6} // y is allocated in the heap int* y = malloc(sizeof(int)*3); } In static storage, to use standard speak. It doesn't really say much
可以说我有以下程序。 这部分内存是a在分配呢? c和c++的行为也是一样的吗? // a is allocated in the ? int a[3] = {1, 2, 3}; int main() { // x is allocated in the stack int x[3] = {4, 5, 6} // y is allocated in the heap int* y = malloc(sizeof(int)*3); } 在静态存储中,使用标准说话。 关于静态存储应该如何实现的问题并没有多少说明,除了应该在程序的整个时间内持续存在,并且如果没有给
If this sounds like a kindergarden question then forgive me ;) But, in C++ a heap is used for memory allocation since... ever (at least the 80's). Is it the best algorithm for the job, or did we just get stuck with it (as happened with javascript...)? Do all (non embedded) OS's use a heap to store memory? Edit: So, what structures/algorithms ARE used. And how is it (if) related to th
如果这听起来像是一个幼儿园问题,那么请原谅我);但是,在C ++中,堆被用于内存分配,因为......至少在80年代以前。 它是这个工作的最佳算法,还是我们刚刚陷入困境(就像javascript所发生的那样)? 是否所有(非嵌入式)操作系统都使用堆来存储内存? 编辑:那么,什么结构/ 使用的算法。 它如何(如果)与堆算法有关? 没有必要与“堆栈”分配(它遍布整个网络)进行比较,或者讨论C ++语义 - 当前内存堆的一个tl; dr?
I don't know much about hacking, c, assembly, memory and all that stuff so I coudln't resolve my question by my self. So, buffer overflow overflows into other address of variables and corrupts them. So I tested and it really does. And I thought that if it can overflow constant variable buffer overflow must be super powered and I tested, but it doesn't overflow const variable. Wh
我对黑客,集会,记忆和所有这些东西都不太了解,所以我没有自己解决我的问题。 所以,缓冲区溢出溢出到变量的其他地址并破坏它们。 所以我测试过,它确实如此。 我认为,如果它可以溢出常量变量缓冲区溢出必须超级供电和我测试,但它不会溢出const变量。 这是为什么? int a; char buffer[8]; 和 const int a; char buffer[8]; 通过'缓冲区'的大小在变量'a'的地址前面具有变量'缓冲区'的地址
Take this example. #include "stdio.h" int global_var=5; int main () { int local_var=6; //some statements return 0; } If the main function is the only entry point, then when does the declaration and assignment of global_var happen? On a related note, is the global_var allocated in the heap or the stack? Also, is there a way to declare a global variable from a function, while r
以这个例子。 #include "stdio.h" int global_var=5; int main () { int local_var=6; //some statements return 0; } 如果主函数是唯一的入口点,那么global_var的声明和赋值global_var发生? 在相关说明中, global_var分配在堆还是堆栈中的? 另外,是否有办法从函数声明全局变量,同时尊重入口点? 从概念上讲,全局变量的初始化发生在输入main之前。 在这里,我假设你的所有代码都被编译在一个翻译
Suppose we have a simple code : int* q = new int(13); int main() { return 0; } Clearly, variable q is global and initialized. From this answer, we expect q variable to be stored in initialized data segment (.data) within program file but it is a pointer, so it's value (which is an address in heap segment) is determined at run time. So what's the value stored in data segment withi
假设我们有一个简单的代码: int* q = new int(13); int main() { return 0; } 显然,变量q是全局的并且已初始化。 从这个答案中,我们希望q变量存储在程序文件中的初始化数据段(.data)中 ,但它是一个指针,所以它的值(它是堆段中的地址)在运行时确定。 那么程序文件中存储在数据段中的值是什么? 我的尝试: 在我看来,编译器为数据段中的变量q (通常为8个字节,用于64位地址)分配了一些空间,没有任何有意
In the following code, 2 is printed. int x = 1; int f(int y) { return x; } int main() { x = 2; printf("%d", f(0)); } How is it happening if we have static scoping in C? Why isn't 1 printed? Printing 2 in this case isn't a dynamic scoping, is it? I thought that in static scoping it should take the nearest x to the function definition. It does take the nearest x , but
在下面的代码中,打印2 。 int x = 1; int f(int y) { return x; } int main() { x = 2; printf("%d", f(0)); } 如果我们在C中进行静态范围设定,情况如何? 为什么不是1打印? 在这种情况下打印2不是动态范围,是吗? 我认为,在静态范围内,它应该采用与函数定义最接近的x。 它确实需要最近的x ,但由于你只有一个x所以它并不重要。 如果您将代码更改为 int x = 1; int f(int y) { return x ;
When should i use the pointer and when not? Whats the better Way? Must i delete the cat object of the pointer at the end of the programm? Example: cat piki; piki.miao(); cat *piki2 = new cat(); piki2->miao(); The piki object will be autodeleted when the current scope is closed. piki2 must be explicitly deleted. delete piki2; Whenever possible try to avoid creating the object using ne
我应该什么时候使用指针,什么时候不用? 最好的方法是什么? 我必须在程序结束时删除指针的猫对象吗? 例: cat piki; piki.miao(); cat *piki2 = new cat(); piki2->miao(); 当前范围关闭时, piki对象将被自动删除。 必须明确删除piki2 。 delete piki2; 尽可能避免使用new (即在堆上)创建对象,因为您必须自己进行内存管理(或者至少需要使用智能指针)。 如果你在堆栈中分配对象(即cat piki; ),当piki超
I am getting a heap corruption error when trying to compile my program. The code in question is a pointer cparticle * particles. It is initialized to NULL and then set to particles = new cparticle[amount] I am only using delete once in the destructor, and it is causing windows to trigger a breakpoint. I have attempted to use application verifier, and it give me this info: ================
试图编译我的程序时,我收到堆损坏错误。 有问题的代码是一个指针 cparticle * particles. 它被初始化为NULL,然后设置为 particles = new cparticle[amount] 我只在析构函数中使用了一次删除操作,并导致窗口触发断点。 我试图使用应用程序验证器,它给了我这个信息: =========================================================== VERIFIER STOP 0000000000000013: pid 0x17C0: first chance access violation for cur