Intricacies of malloc and free

I have two related questions hence I am asking them in this single thread. Q1) How can I confirm if my OS is clearing un-"free"'ed memory (allocated using malloc) automatically when a program terminates? I am using Ubuntu 11.04, 32-bit with gcc-4.5.2 As per a tutorial page by Steven Summit here, "Freeing unused memory (malloc'ed) is a good idea, but it's not mandat

错综复杂的malloc和免费

我有两个相关的问题,因此我在这个单线程中询问他们。 Q1)当程序终止时,如何确认我的操作系统是否自动清除非“免费”编辑内存(使用malloc分配)? 我使用Ubuntu 11.04,32位gcc-4.5.2 根据Steven Summit的教程页面,“释放未使用的内存(malloc'ed)是一个好主意,但它不是强制性的。当你的程序退出时,它已经分配但没有释放的任何内存应该被自动释放。计算机只是因为你的程序忘记释放内存而“失去”内存,这就表明你的操

What happens to memory after '\0' in a C string?

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset, and, depending on processing, the size may shrink. The question is, is there anything wrong with allocating enough heap space (the upper bound) and th

C字符串中'\ 0'之后的内存会发生什么变化?

令人惊讶的简单/愚蠢/基本问题,但我不知道:假设我想返回我的函数的用户一个C字符串,其长度我不知道在函数的开始。 我可以在开始时仅放置长度的上限,并且根据处理的不同,尺寸可能会缩小。 问题是,分配足够的堆空间(上限)是否有问题,然后在处理过程中终止了字符串? 即如果我在分配的内存中间插入' 0',(a。) free()仍然正常工作,并且(b)在' 0'之后的空间变得无关紧要? 一旦添加' 0'

Should one really set pointers to `NULL` after freeing them?

There seem to be two arguments why one should set a pointer to NULL after freeing them. Avoid crashing when double-freeing pointers. Short: Calling free() a second time, by accident, doesn't crash when it's set to NULL . Almost always this masks a logical bug because there is no reason to call free() a second time. It's safer to let the application crash and be able to fix it.

在释放它们之后,真的应该设置指向NULL的指针吗?

似乎有两个参数为什么在释放它们之后应该将指针设置为NULL 。 当释放双指针时避免崩溃。 简而言之:无意中调用free() ,当它被设置为NULL时不会崩溃。 几乎总是这样掩盖了一个逻辑错误,因为没有理由再次调用free() 。 让应用程序崩溃并修复它会更安全。 它不能保证崩溃,因为有时候新的内存分配在相同的地址。 当有两个指向相同地址的指针时,主要发生双重空闲。 逻辑错误也可能导致数据损坏。 避免重复使用已释

Big array gives segmentation error in C

I am really new to C, so I am sorry if this is a absolute beginner question, but I am getting a segmentation error when I am building large array, relevant bits of what I am doing is: unsigned long long ust_limit; unsigned long long arr_size; /* ust_limit gets value around here ... */ arr_size = ((ust_limit + 1) / 2) - 1; unsigned long long numbs[(int)arr_size]; This works for some values of

大数组给C中的分段错误

我对C非常陌生,所以如果这是一个绝对的初学者问题,我很抱歉,但是当我构建大型数组时,我遇到了分割错误,我正在做的是相关的部分: unsigned long long ust_limit; unsigned long long arr_size; /* ust_limit gets value around here ... */ arr_size = ((ust_limit + 1) / 2) - 1; unsigned long long numbs[(int)arr_size]; 这适用于ust_limit的某些值,但是当它达到大约4.000.000以上时会出现分段错误。 我想要的是

Assistance to draw a stack using C code and assembly code

I am trying to draw a stack as it would appear just before the “return count” line in the secondCall function. I am trying to draw it so that it would show all three frames (or activation records) for the three active functions, main, firstCall and secondCall. Will someone help me complete the stack diagram? I am trying to draw the positions of the base (ebp) and stack (esp) pointers as they

协助使用C代码和汇编代码绘制堆栈

我想绘制一个堆栈,因为它会出现在secondCall函数的“返回计数”行之前。 我试图绘制它,以便显示三个活动函数main,firstCall和secondCall的所有三个帧(或激活记录)。 有人会帮我完成堆栈图吗? 我试图在调用下一个函数之前绘制基本(ebp)和堆栈(esp)指针的位置,因为它们位于每个堆栈帧中。 C代码如下: #include <stdio.h> #include <stdlib.h> #include <unistd.h> int secondCall(int a, int

Address of local variable on stack

I wrote a little C code, opened it in GDB, put a breakpoint on line exploit = (long long *)&exploit+2; and run the program. #include<stdio.h> char Shellcode[] = "x48xc7xc0x01x00x00x00" "x48xc7xc3x1ax00x00x00" "xcdx80"; int main() { long long *exploit; exploit = (long long *)&exploit+2; *exploit = (long long)Shellcode;

堆栈上局部变量的地址

我写了一个小小的C代码,在GDB中打开它,在线exploit = (long long *)&exploit+2;上放置一个断点exploit = (long long *)&exploit+2; 并运行该程序。 #include<stdio.h> char Shellcode[] = "x48xc7xc0x01x00x00x00" "x48xc7xc3x1ax00x00x00" "xcdx80"; int main() { long long *exploit; exploit = (long long *)&exploit+2; *exploit = (

x86 way to tell instruction from data

Is there a more or less reliable way to tell whether data at some location in memory is a beginning of a processor instruction or some other data? For example, E8 3F BD 6A 00 may be call instruction ( E8 ) with relative offset of 0x6ABD3F , or it might be three bytes of data belonging to some other instruction, followed by push 0 ( 6A 00 ). I know the question sounds silly and there is probab

x86方式来告诉数据的指令

有没有一种或多或少的可靠方式来判断内存中某个位置的数据是处理器指令的开始还是其他数据? 例如, E8 3F BD 6A 00可以是相对偏移量为0x6ABD3F call指令( E8 ),也可以是属于某个其他指令的三个字节的数据,然后push 0 ( 6A 00 )。 我知道这个问题听起来很愚蠢,可能没有简单的方法,但也许指令集是为了解决这个问题而设计的,也许一些检查位置周围的+ -100字节的简单代码可以给出很可能是正确的答案。 我想知道这一

Need help in clarifying simply Assembly code and feedback about my analyzis

So I create Assembly code from C and try to understand how it works. I need your help to clarify some points and tell me if I am analysing Assembly work in a right way. Here is C code int mult(int x, int y){ int result = x * y; return result; } int main(int argc, char *argv[]){ int x = 10; int y = 2; return mult(x, y); } And here is generated Assembly mult: .LFB0: .

只需要帮助澄清汇编代码和有关我的分析的反馈

所以我从C创建汇编代码并尝试了解它是如何工作的。 我需要你的帮助来澄清一些观点,并告诉我是否以正确的方式分析大会工作。 这里是C代码 int mult(int x, int y){ int result = x * y; return result; } int main(int argc, char *argv[]){ int x = 10; int y = 2; return mult(x, y); } 这里是生成的Assembly mult: .LFB0: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi

Recognizing stack frames in a stack using saved EBP values

I would like to divide a stack to stack-frames by looking on the raw data on the stack. I thought to do so by finding a "linked list" of saved EBP pointers. Can I assume that a (standard and commonly used) C compiler (eg gcc) will always update and save EBP on a function call in the function prologue? pushl %ebp movl %esp, %ebp Or are there cases where some compilers might skip

使用保存的EBP值识别堆栈中的堆栈帧

我想通过查看堆栈中的原始数据来将堆栈分成堆栈。 我想通过找到保存的EBP指针的“链接列表”来实现。 我能否假设一个(标准和常用的)C编译器(例如gcc)会在函数序言中的函数调用中始终更新并保存EBP? pushl%ebp movl%esp,%ebp 或者有些情况下,有些编译器可能会跳过那部分函数,​​它们没有得到任何参数,也没有局部变量? x86调用约定和Wiki文章中对函数序言的帮助不大。 有没有更好的方法通过查看原始数据来

Handling signals in different ways

Can anyone help me to understand what are the differences between the following three ways of handling a signal? I'm operating in a client/server in C. Edit: I've understood that the 1st example is wrong, now I'd like to know which is better/possible, the 2nd one ore the third one? I've also read around the web that someone does a mix between them, using the "struct sigac

以不同的方式处理信号

任何人都可以帮助我理解以下三种处理信号的方式之间有什么区别? 我在C的客户端/服务器上运行。 编辑:我明白,第一个例子是错误的,现在我想知道哪个更好/可能,第二个是第三个? 我还在网络上看到有人在他们之间进行混合,使用“sigemptyset”和“sigaddset”中的“struct sigaction sig”,没有任何sigprocmask。 这比我的解决方案好吗? 处理器: void closeSig(){ close(socketAccept); close(socket); exit(1