malloc

Sample example program to get the malloc consolidate error

大憨熊 提交于 2020-04-10 05:52:49
问题 I want to test the mcheck functionality on my PC first to detect the malloc consolidate error. This way, i will be sure that this will help to figure out a similar crash on embedded-linux box. Unfortunately, the crash takes atleast 3-4 days. Thus, I am looking for a sample program that will generate the similar kind of crash as shown below. Program terminated with signal 6, Aborted. #0 0x2c73ebb8 in __syscall_kill (pid=900, sig=6) at kill.c:15 15 static inline _syscall2(int, __syscall_kill, _

malloc要不要强制转换返回值

╄→гoц情女王★ 提交于 2020-03-27 14:58:20
3 月,跳不动了?>>> 对于这个问题,很多人肯定想当然的说必须强制转换之类的云云……实则不然,今天看《C语言核心技术》的时候明确说到 可以将一个void指针值指定给另一个对象指针数据类型;反之也行,这都不需要进行显式数据类型转换。 然后在c语言邮件组常见问题里发现这个了,原文是 http://c-faq.com/malloc/mallocnocast.html ,内容如下: Q: What's wrong with casting malloc 's return value? A: Suppose that you call malloc but forget to #include <stdlib.h>. The compiler is likely to assume that malloc is a function returning int, which is of course incorrect, and will lead to trouble. Now, if your call to malloc is of the form char *p = malloc(10); the compiler will notice that you're seemingly assigning an integer to a pointer, and will

How to treat a pointer returned by malloc as a multidimensional array?

╄→гoц情女王★ 提交于 2020-03-22 09:03:31
问题 Is there a way to tell the compiler that I've allocated a memory of size N * M and I wanna treat this pointer as N * M array? In other words, is there a way to write something like this?: int arr[N][M] = (int[N][M])malloc(N * M * sizeof(int)); arr[x][y] = 123; I know that the compiler doesn't know the dimensions of the array, all it knows is that that's a pointer. so my question is: can I somehow tell the compiler that this pointer returned by malloc is an array pointer and it's dimensions

How to override standard libc functions?

混江龙づ霸主 提交于 2020-03-13 06:18:42
问题 For example, if I want to override malloc(), what's the best way to do it? Currently the simplest way I know of is: malloc.h #include <stdlib.h> #define malloc my_malloc void* my_malloc (size_t size); foobar.c #include "malloc.h" void foobar(void) { void* leak = malloc(1024); } The problem with this approach is that we now have to use "malloc.h" and can never use "stdlib.h". Is there a way around this? I'm particularly interested in importing 3rd party libraries without modifying them at all,

How to override standard libc functions?

故事扮演 提交于 2020-03-13 06:18:28
问题 For example, if I want to override malloc(), what's the best way to do it? Currently the simplest way I know of is: malloc.h #include <stdlib.h> #define malloc my_malloc void* my_malloc (size_t size); foobar.c #include "malloc.h" void foobar(void) { void* leak = malloc(1024); } The problem with this approach is that we now have to use "malloc.h" and can never use "stdlib.h". Is there a way around this? I'm particularly interested in importing 3rd party libraries without modifying them at all,

SIGSEGV, (seemingly) caused by printf

青春壹個敷衍的年華 提交于 2020-03-02 06:48:07
问题 First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here, but I was unable to find this elsewhere (via Google and Stack Overflow). Here's the gist of the error. If I call printf , sprintf or fprintf anywhere within my code, to display a float, I get a SIGSEGV (EXC_BAD_ACCESS) error. Let me give an example. The following throws the error: float f = 0.5f; printf("%f\n",f); This code does not: float f = 0.5f; printf("%d\n",f); I realize there's an implicit

用brk实现sbrk,关于brk的返回值

好久不见. 提交于 2020-03-01 03:58:32
首先我们已经知道linux下,malloc最后调用的是sbrk函数,而sbrk是对brk的简单封装。 用sbrk模仿malloc很简单,sbrk(0)得到当前breakpoint,再调用sbrk(size)即可。(PS:breakpoint表示堆结束地址) 一直以来让我困惑的是,怎么用brk去实现sbrk,换句话说,就是只有brk系统调用,如何能得知当前的breakpoint...难道就没有人想过这个问题嘛?搜索了各种关键字,来来回回都围绕着sbrk讲,算了,自己动手,丰衣足食,咱求人不如求己,还是自己分析分析好了, glibc中brk的wrapper如下: #include <unistd.h> int brk(void *addr); man手册中对此函数的描述: brk() sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory, and the process does not exceed its maximum data size (see setrlimit(2)). RETURN VALUE On success, brk() returns zero. On error,

【C语言】malloc和realloc的区别以及使用

妖精的绣舞 提交于 2020-03-01 02:45:13
ANSI C说明了三个用于存储空间动态分配的函数 (1) malloc分配指定字节数的存储区。此存储区中的初始值不确定 (2) calloc为指定长度的对象,分配能容纳其指定个数的存储空间。该空间中的每一位(bit)都初始化为0 (3) realloc 更改以前分配区的长度(增加或减少)。当增加长度时,可能需将以前分配区的内容移到另一个足够大的区域,而新增区域内的初始值则不确定 .分配函数时再分配 realloc() 使我们可以增、减以前分配区的长度(最常见的用法是增加该区)。 如果先分配一个可容纳长度为512的数组的空间,并在运行时填充它,但又发现空间不够,则可调用realloc扩充该存储空间。 如果在该存储区后有足够的空间可供扩充,则可在原存储区位置上向高地址方向扩充,并返回传送给它的同样的指针值。 如果在原存储区后没有足够的空间,则realloc分配另一个足够大的存储区,将现存的5 1 2个元素数组的内容复制到新分配的存储区。 因为这种存储区可能会移动位置,所以不应当使用任何指针指在该区中。 注意,realloc的最后一个参数是存储区的newsize(新长度),不是新、旧长度之差。作为一个特例,若ptr是一个空指针,则realloc的功能与malloc相同,用于分配一个指定长度newsize的存储区。 这些分配例程通常通过sbrk(2)系统调用实现。该系统调用扩充(或缩小

Can still print a string after I freed it?

半世苍凉 提交于 2020-02-27 23:20:39
问题 I am learning and testing memory allocation in C and I want to test what happens if free() is called. I expected there could be a segmentation fault or pointer is NULL after I run the program below. However, I can still successfully print the string as in Output. I also tried to free str twice, then an error as Output 2 occurred. It seems the previously allocated memory is successfully deallocated, but the data on the memory is not cleaned up. Is that correct? If that is the case, when will

What does *p mean when **p is already declared

删除回忆录丶 提交于 2020-02-23 04:30:09
问题 Code short **p = (short **)malloc(sizeof(short *)); *p = malloc(sizeof(short)); **p = 10; printf("**p = %d", **p); Output **p = 10 In this code, a multiple pointer **p is declared and *p is used without any declaration(maybe it's by **p ). What does *p mean in my case? Sorry for very simple question. I saw C standard and stack overflow, but I couldn't find out something. 回答1: For any array or pointer p and index i , the expression p[i] is exactly equal to *(p + i) (where * is the unary