Concurrent Programming, Stacks and Heaps in C/C++

Well, I am sorry if this feels like a repetition of old questions, I have gone through several questions on Stack Overflow, the Modern Operating Systems Book by tanenbaum, and have still to clear my doubts regarding this. First off, I would appreciate any book/resource that I should go through in more detail to better understand this structure. I fail to understand if these are concepts genera

C / C ++中的并行编程,堆栈和堆

那么,如果这感觉像是重复了一些旧问题,我很抱歉,我已经通过tanenbaum回顾了Stack Overflow的几个问题,现代操作系统书,并且仍然要清除我对此的疑问。 首先,我希望能够更详细地阅读任何书籍/资源,以便更好地理解这种结构。 我无法理解这些概念是否在操作系统书籍或编程语言或体系结构书籍中有一般解释。 在问我的问题之前,我会根据关于堆栈/堆的读数列出我的发现 堆 仅包含所有实例变量,动态分配(new / malloc

Dynamically switching between stack and heap

Suppose I'm writing a simple buffer class. This buffer would act as a simple wrapper for a standard C array of objects. It should also be backwards-compatible to work with existing functions that take simple arrays as input. The goal here is to make this buffer efficient in both speed and memory usage. Since stack allocation is always faster than heap, I want to allocate everything on a

在堆栈和堆之间动态切换

假设我正在写一个简单的缓冲区类。 这个缓冲区将作为标准C对象数组的简单包装。 它也应该向后兼容,以使用以简单数组作为输入的现有函数。 这里的目标是使这个缓冲区在速度和内存使用方面都有效。 由于堆栈分配始终比堆快,因此我希望将堆栈中的所有内容分配给某个阈值,如果堆栈分配的容量变大,请在堆上重新分配。 这如何有效地完成? 我研究过,显然std :: string做了类似的事情。 我只是不知道如何。 我所拥有的最

Equivalent of `using` aliases for `template`s

C++11 added alias templates such as: template<typename T> using identity = T; template<bool b, typename T = void> using EnableIf = typename std::enable_if<b, T>::type; These are much easier to use than the old template type maps that give you the return value in a ::type field because even if your type arguments are dependent on the local context, you don't need to infor

“模板”的“使用”别名的等价物

C ++ 11添加了别名模板,例如: template<typename T> using identity = T; template<bool b, typename T = void> using EnableIf = typename std::enable_if<b, T>::type; 这些都是很容易比旧的使用template类型的地图,让您在返回值::type ,因为即使你的类型参数都依赖于当地环境领域,你不需要告诉编译器的结果是一种。 实际上,您将typename从使用位置提升为using别名。 有什么等价物可以用来摆

Dynamically allocated memory storage clarification

This question already has an answer here: Stack Memory vs Heap Memory [duplicate] 3 answers I thought I'd make this an answer: Very simple: If you use "new", the memory will be allocated from the heap. "p" is a local variable. It's a pointer, and it's stored on the stack. The 10,000 ints it points to were allocated by "new"; they're stor

动态分配内存存储说明

这个问题在这里已经有了答案: 堆栈内存vs堆内存[复制] 3个回答 我想我会做出这个答案: 很简单: 如果使用“新建”,内存将从堆中分配。 “p”是一个局部变量。 它是一个指针,它存储在堆栈中。 它指出的10,000个整数由“新”分配; 它们存储在堆中。 int* p = new int[100000]; 将始终从堆中分配内存(正确的术语是动态存储)。 这意味着使用new或new[] 。 只有指针变量本身会分别在main() ,本地存储器之外获得静

Where are static variables stored (in C/C++)?

In what segment (.BSS, .DATA, other) of an executable file are static variables stored so that they don't have name collision? For example: foo.c: bar.c: static int foo = 1; static int foo = 10; void fooTest() { void barTest() { static int bar = 2; static int bar = 20; foo++; foo++; bar++;

静态变量存储在哪里(在C / C ++中)?

在可执行文件的段(.BSS,.DATA,其他)中存储了哪些静态变量,以避免名称冲突? 例如: foo.c: bar.c: static int foo = 1; static int foo = 10; void fooTest() { void barTest() { static int bar = 2; static int bar = 20; foo++; foo++; bar++; bar++; printf("%d,%d", foo, bar); printf("

heap vs data segment vs stack allocation

Am looking at the following program and not sure how the memory is allocated and why: void function() { char text1[] = "SomeText"; char* text2 = "Some Text"; char *text = (char*) malloc(strlen("Some Text") + 1 ); } In the above code, the last one is obviously in the heap. However, as I understand text2 is in data segment of the program and text1 may be on the stack. Or is my assum

堆vs数据段vs堆栈分配

我正在看下面的程序,不确定内存是如何分配的以及为什么: void function() { char text1[] = "SomeText"; char* text2 = "Some Text"; char *text = (char*) malloc(strlen("Some Text") + 1 ); } 在上面的代码中,最后一个显然是在堆中。 但是,据我了解,text2在程序的数据段中,而text1可能在堆栈中。 或者我的假设是错误的? 这里有什么正确的假设? 这个编译器是否依赖? 谢谢 // Array allocated on

Probably heap corruption, but could it be something else?

I have the following c++ code running in a multi-threaded environment: A* a = (A*) (_x->b); a->DoStuff(); A is a concrete class, _x is a member of A (let's say of type X* ) and b is a void * . In addition, this is also an A* , and it has a different memory location, but similar data to what the dump shows is at _x->b . I am encountering an inexplicable crash at the dereferenc

可能堆腐败,但它可能是别的东西?

我有以下在多线程环境中运行的c ++代码: A* a = (A*) (_x->b); a->DoStuff(); A是一个具体的类, _x是A的成员(比方说X*类型), b是void * 。 另外, this也是一个A* ,它具有不同的内存位置,但与转储显示的_x->b相似的数据。 我在解除引用时遇到了莫名其妙的崩溃。 生成的转储文件非常详细,它告诉我_x处于良好状态, _x->b指向有效的非零内存地址。 转储还显示,在第二个语句中, a是0,这是首先发生

C++ Heap Corruption

I haven't done any C++ in a while, but decided to finish a big project I was working on for someone. I am getting the following error message now though... HEAP CORRUPTION DETECTED: after Normal Block (#1761) at 0x17DEB940. CRT Detected that the application wrote to memory after end of heap buffer. I have been stepping through all of the functions I thought might have caused it but I am

C ++堆损坏

我有一段时间没有做过任何C ++,但决定完成一个我正在为某人工作的大型项目。 我现在收到以下错误消息,但... HEAP CORRUPTION DETECTED:Normal Block(#1761)at 0x17DEB940。 CRT检测到应用程序在堆缓冲区结束后写入内存。 我一直在考虑所有我认为可能导致它的功能,但我很茫然。 有没有什么方法可以使用更高级的调试功能来追究? 它听起来像一个经典的内存损坏错误。 该平台将是有用的信息。 没有看到你的代码,

Heap corruption under Win32; how to locate?

I'm working on a multithreaded C++ application that is corrupting the heap. The usual tools to locate this corruption seem to be inapplicable. Old builds (18 months old) of the source code exhibit the same behaviour as the most recent release, so this has been around for a long time and just wasn't noticed; on the downside, source deltas can't be used to identify when the bug was i

堆Win32下的腐败; 如何定位?

我正在研究一个正在破坏堆的多线程 C ++应用程序。 找到这种腐败的常用工具似乎不适用。 源代码的旧版本(18个月)显示与最新版本相同的行为,所以这已经存在了很长时间,并且没有被注意到; 在缺点方面,源代码增量不能用于识别何时引入了错误 - 存储库中有很多代码更改。 崩溃行为的提示是在这个系统中产生吞吐量 - 数据的套接字传输被转化为内部表示。 我有一组测试数据会定期导致应用程序异常(各种地方,各种原因 - 包

Freeing memory allocated in a different DLL

I have an EXE file using a DLL file which is using another DLL file. This situation has arisen: In DLL file 1: class abc { static bool FindSubFolders(const std::string & sFolderToCheck, std::vector< std::string > & vecSubFoldersFound); } In DLL file 2: void aFunction() { std::vector<std::string> folders; std::string sLocation;

释放分配在不同DLL中的内存

我有一个使用另一个DLL文件的DLL文件的EXE文件。 这种情况已经出现: 在DLL文件1中: class abc { static bool FindSubFolders(const std::string & sFolderToCheck, std::vector< std::string > & vecSubFoldersFound); } 在DLL文件2中: void aFunction() { std::vector<std::string> folders; std::string sLocation; ... abc::FindSubFolders