I want to know how malloc and free work. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p); // Obvious Crash, but I need how it works and why crash. cout << p; return 0; } I would be really grateful if the answer is in depth at
我想知道如何malloc和free工作。 int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p); // Obvious Crash, but I need how it works and why crash. cout << p; return 0; } 如果答案在内存层面上是深入的,如果可能的话,我会很感激。 好的,
I noticed there's not question with a list of common causes of segmentation faults in C++, so I thought I'd add it. Naturally it's community Wiki, since there's no one correct answer. I think this might be useful for newer programmers learning C++, feel free to close it if you disagree. Segmentation fault is caused by bad accesses to memory, only if your OS has a MMU. Other
我注意到C ++中的段错误的常见原因没有问题,所以我想我会添加它。 当然这是社区维基,因为没有一个正确的答案。 我认为这对于学习C ++的新程序员可能有用,如果你不同意,可以随时关闭它。 分段错误是由于对内存访问不良造成的,只有当您的操作系统具有MMU时。 否则,你不会得到它,但只有奇怪的行为。 虚拟内存(您可以访问的整个内存= 2 ^ sizeof(pointer type) )被映射到名为页或段的单元中的物理内存(分页取代分
Today is my first time using c++ in a while. I am normally a python programmer. I keep getting segfaults and I've isolated it to the commented lines. (the ones that are commented cause segfaults when uncommented.) #include <iostream> #include <fstream> #include <string> using namespace std; #include "defaultfile.h" int main() { ifstream mapin; string map; st
今天是我第一次使用c ++。 我通常是一个Python程序员。 我不断收到segfaults,并将它隔离到注释行中。 (注释的那些在未注释时会导致段错误。) #include <iostream> #include <fstream> #include <string> using namespace std; #include "defaultfile.h" int main() { ifstream mapin; string map; string s; int i = 0; while (i<=22){i++;top[i][0]="__";i++;};i=0; while
This question already has an answer here: Trouble getting memory allocated for char* in c++ 1 answer float* hu_geometry = new float(dim); int* hu_temp = new int(dim); those are 1-char arrays containing the value dim . At some point you're hitting a MMU boundary and crashes randomly. You want to write: float* hu_geometry = new float[dim]; int* hu_temp = new int[dim]; or maybe better w
这个问题在这里已经有了答案: 无法获取分配给char *的内存在C ++ 1答案 float* hu_geometry = new float(dim); int* hu_temp = new int(dim); 那些是包含值dim 1字符数组。 在某个时候,你正在碰到一个MMU边界并随机崩溃。 你想写: float* hu_geometry = new float[dim]; int* hu_temp = new int[dim]; 或者可能会更好地使用矢量,预先分配了dim元素 #include <vector> std::vector<float> hu_geometry(d
What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related? Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” It's a helper mechanism that keeps you from corrupting the memory and introducing hard-to-debug memory bugs. Whenever you get a segfault you know you are doing someth
什么是分段错误? C和C ++有什么不同? 分段错误和悬挂指针如何相关? 分段错误是由于访问“不属于你”的内存而导致的一种特定类型的错误。它是一种助手机制,可以防止破坏内存并引入难以调试的内存错误。 每当你遇到段错误时,你都知道你正在做的是内存错误 - 访问已被释放的变量,写入内存的只读部分等等。在大多数语言中,分段错误基本上是一样的,内存管理,C和C ++中的段错误之间没有主要区别。 至少在C(++)等低级语
What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? Feel free to edit/add your own. Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote]. Physical source
什么是未定义的引用/未解析的外部符号错误? 什么是常见原因以及如何修复/防止它们? 随意编辑/添加自己的。 编译一个C ++程序需要几个步骤,如2.2所指出的(授予Keith Thompson作为参考): 翻译的语法规则中的优先顺序由以下几个阶段规定[见脚注]。 必要时,以实现定义的方式将物理源文件字符映射到基本源字符集(为行尾指示符引入新行字符)。 [SNIP] 删除每个反斜线字符()后面紧跟着一个换行符的字符,拼接物
I am attempting to profile some c++ code, compiled with g++ including the option -pg, using gprof. However, in spite of the fact that the program takes 10-15 minutes to run on my computer (with the CPU maxed out), the % time, cumulative seconds and self seconds columns of the table produced by gprof are entirely 0.00s! The calls column contains correct looking data, for example over 150,000 cal
我试图分析一些c ++代码,用g ++编译,包括使用gprof的选项-pg。 但是,尽管程序需要10-15分钟才能在我的计算机上运行(CPU超时),但gprof生成的表的%time,cumulative seconds和self seconds列完全是0.00s! 调用列包含正确的外观数据,例如超过150,000个基本功能的调用。 以下是收集的数据样本: % cumulative self self total time seconds seconds calls Ts/call Ts/call
Is it possible to use gprof to line-profile a single function in C++? Something like: gprof -l -F function_name ... , which does not seem to work. That can be done easily with valgrind. It is a wonderful tool if you have the chance to use it in your development environment. It even have and graphical interface kcachegrind. Try using options with [symspec] to filter the results. gprof 2
是否有可能使用gprof在C ++中对单个函数进行行布局? 就像是: gprof -l -F function_name ... ,这似乎不起作用。 这可以通过valgrind轻松完成。 如果您有机会在您的开发环境中使用它,它是一个非常棒的工具。 它甚至有图形界面kcachegrind。 尝试使用[symspec]的选项来过滤结果。 gprof 2.18.0表示-F和-f不推荐使用,而是使用symspec。 另外, -l可能不适用于使用新版本的gcc编译的二进制文件。 试试gcov 。
I tend to write rather large templated header-only C++ libraries and my users commonly complain about compilation times. After thinking about the matter, it occurred to me that I have no idea where the time is going. Is there some simple way to profile the C++ compilation process with common compilers, such as g++, icc, and xlC? For instance, is it possible to get an idea of how much time is s
我倾向于编写相当大的模板化头文件C ++库,而我的用户通常会抱怨编译时间。 想到这件事后,我想起了我不知道时间在哪里。 是否有一些简单的方法来用常见编译器(如g ++,icc和xlC)剖析C ++编译过程? 例如,是否有可能知道在C ++编译的每个阶段花了多少时间? 对于GCC ,有调试选项可以查找how much time is spent within each of the phases of C++ compilation? -Q使编译器在编译时输出每个函数名称,并在每次传递完成
I am trying to figure out where my C++ program is spending its time, using gprof. Here's my dilemma: if I compile with the same optimization settings I use for my release build, pretty much everything gets inlined, and gprof tells me, unhelpfully, that 90% of my time is spent in a core routine, where everything was inlined. On the other hand, if I compile with inlining disabled, the program
我试图找出我的C ++程序在哪里花费时间,使用gprof。 这就是我的两难困境:如果我使用与我的发布版本相同的优化设置进行编译,几乎所有内容都被内联,并且gprof告诉我,90%的时间都花在核心例程中,其中一切都被内联了。 另一方面,如果我在禁用内联的情况下进行编译,程序运行速度会降低一个数量级。 我想知道当我的程序在启用内联编译时,我的核心例程中调用的过程需要多少时间。 我在四核英特尔机器上运行64位Ubuntu 9