Tag: 内存管理

对传递引用感到困惑

考虑以下示例,我尝试以C方式传递引用: // Function prototypes void increment(unsigned* number); int main() { unsigned* thing; increment(thing); cout << *thing; return 0; } void increment(unsigned* number) { number = (unsigned*) malloc(sizeof(unsigned)); *number = 1; } 我在行cout << *thing遇到程序崩溃。 是的,我在这里使用C ++但是我想尝试C版本的pass-by-reference,因为我的主要项目是在C. 我通过更改代码修复它,如下所示: // Function prototypes void increment(unsigned** number); int main() { unsigned* thing; increment(&thing); cout << *thing; return 0; } […]

谁处理C ++“新”内存分配失败?

在C中,人们经常在使用malloc后检查空指针。 但是,在C ++中,我们将使用“new”。 我已经做了一些搜索,一个解释malloc和new之间的区别如下: https : //isocpp.org/wiki/faq/freestore-mgmt#new-malloc-diff 从上面的链接引用: malloc()通过抛出exception(bad_alloc)返回0.报告分配和初始化错误来报告内存耗尽。 但是OS(Linux或Windows)如何对这个bad_allocexception做出反应?

返回alloca指针

此代码是否返回对堆栈上分配的变量的无效引用? 或者是什么: void *f(size_t sz) { return alloca(sz); } 或者这是一个由alloca实现/编译器支持处理的特殊情况,如f(alloca(size), alloca(size))会是什么?

如何在C程序中删除此分段错误

这里我想解决此代码中的堆栈溢出问题。 在这个代码中,我递归调用函数p 350000次,所以当我删除350000时,我得到分段错误并且放置300000比它工作正常这里分段错误来自因为我更多次调用函数p的递归调用或者调用递归函数太深。 这不起作用,因为我采取if(i != 350000) 。 它的停留时间可能在300000到327480之间。我测试了10次 码: #include void p(char *, int); int main() { char *a = “HI”; int b = 10; p(a, b); printf(“\nComplete”); return 0; } void p(char *a, int b) { static long int i = 0; if (i != 350000) { printf(“\n%ld \t at Hi hello”, i); i++; […]

是什么决定了可以分配多少内存?

这是我之前关于为什么size_t是必要的问题的后续跟进。 鉴于size_t保证足够大以表示你可以分配的内存块的最大大小(意味着仍然可能有一些大于size_t的整数),我的问题是…… 是什么决定了你可以一次分配多少钱?

中断系统调用posix_memalign

我正在使用posix_memalign获得此exception。 知道为什么我们得到它吗? 提前致谢。

C中二维数组的内存分配

我正在写一个multithreadingC程序,我有一个错误。 我有一个全局声明的2D数组array worker_table : int **worker_table; 并在主要分配如下: worker_table = (int**) calloc(number_of_workers*2,(sizeof(int))); 这是工作者function: void *Worker(void *worker_id) { my_id = (int)worker_id; //id of the worker printf(“Line 231\n”); printf(“My id is %d\n”,my_id); my_customer = worker_table[my_id][1];//line 233 printf(“Line 234\n”); int my id; 错误发生在第234行之前,我认为第233行出了什么问题,但我无法弄清楚它是什么。

Windows等效于sys / mman.h

我在尝试在Win64上编译我的C代码时遇到了问题。 更具体地说,编译器找不到sys/mman.h头文件,我理解只能在Unix环境中找到它。 我已经知道这是内存分配的处理。 是否有一个等效的Windows我可以用来移植代码(第一次尝试)? 代码导致问题: /* Allocate memory required by processes */ buf = (int*) malloc (sizeof(int)); if (!buf) { perror(“Error”); free (buf); return -3; } /* Lock down pages mapped to processes */ puts(“Locking down processes.”); if(mlockall (MCL_CURRENT | MCL_FUTURE) < 0) { perror("mlockall"); free (buf); return -4; }

如何知道哪个进程分配内存?

我想知道进程分配内存的区域。 我知道我可以使用 cat /proc/pid/status 并且 cat /proc/zoneinfo 这两个命令都没有回答进程分配给内存的位置。 还有其他命令吗? 如果有可以在内核中实现它?

malloc:匿名映射和魔术区域

我只是在摆弄内存映射,并希望查看用户空间虚拟内存区域映射。 写了一些像 char *ptr = NULL; printf(“Allocating 300KB\n”); ptr = malloc (300*1024); printf(“Allocated at %p.. sleeping\n”, ptr); sleep (30); free (ptr); printf(“Freed… sleeping\n”); sleep (30); 在运行程序时,pid上的pmap将分配的区域显示为: 00007f73b1e57000 316K rw— [ anon ] 而程序o / p说: Allocated at 0x7f73b1e57010.. sleeping 对于我们称之为魔法区域的分配,这是16KB的额外分配吗? 在内核中,相应的vm_area_struct将保存程序可见范围或魔术区域起始的整个范围?