最近有人在我使用的一段代码中向我指出
char* name = malloc(256*sizeof(char));
// more code
free(name);
Run Code Online (Sandbox Code Playgroud)
我的印象是这种设置阵列的方式与使用相同
char name[256];
Run Code Online (Sandbox Code Playgroud)
并且两种方式都需要使用free().我错了,如果是这样,有人可以用低级别的术语解释有什么区别?
我一直在追逐这个bug,我只是不明白.我忘了一些基本的C或什么?
==28357== Conditional jump or move depends on uninitialised value(s)
==28357== at 0x4C261E8: strlen (mc_replace_strmem.c:275)
==28357== by 0x4E9280A: puts (ioputs.c:36)
==28357== by 0x400C21: handlePath (myshell.c:105)
==28357== by 0x400B17: handleInput (myshell.c:69)
==28357== by 0x400AAD: acceptInput (myshell.c:60)
==28357== by 0x4009CF: main (myshell.c:33)
==28357== Uninitialised value was created by a heap allocation
==28357== at 0x4C25153: malloc (vg_replace_malloc.c:195)
==28357== by 0x400BDE: handlePath (myshell.c:99)
==28357== by 0x400B17: handleInput (myshell.c:69)
==28357== by 0x400AAD: acceptInput (myshell.c:60)
==28357== by 0x4009CF: main (myshell.c:33)
==28357==
(095) void handlePath(char *input) { …Run Code Online (Sandbox Code Playgroud) 嗨,
我对C的malloc函数有点新,但据我所知,它应该将值存储在堆中,因此您可以使用来自原始范围之外的指针来引用它.我创建了一个应该执行此操作的测试程序,但在运行程序后,我一直得到值0.我究竟做错了什么?
#include <stdio.h>
#include <stdlib.h>
int f1(int *b) {
b = malloc(sizeof(int));
*b = 5;
}
int main(void) {
int *a;
f1(a);
printf("%d\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我饶有兴趣地阅读了malloc和calloc之间的后C差异.我在我的代码中使用了malloc,想知道我使用calloc会有什么不同.
我目前的(伪)代码与malloc:
场景1
int main()
{
allocate large arrays with malloc
INITIALIZE ALL ARRAY ELEMENTS TO ZERO
for loop //say 1000 times
do something and write results to arrays
end for loop
FREE ARRAYS with free command
} //end main
Run Code Online (Sandbox Code Playgroud)
如果我使用calloc而不是malloc,那么我将:
Scenario2
int main()
{
for loop //say 1000 times
ALLOCATION OF ARRAYS WITH CALLOC
do something and write results to arrays
FREE ARRAYS with free command
end for loop
} //end main
Run Code Online (Sandbox Code Playgroud)
我有三个问题:
如果阵列非常大,哪个场景更有效?
如果阵列非常大,哪个场景会更有时间效率? …
有没有什么方法可以对大型数组进行malloc,但是用2D语法引用它?我想要的东西:
int *memory = (int *)malloc(sizeof(int)*400*200);
int MAGICVAR = ...;
MAGICVAR[20][10] = 3; //sets the (200*20 + 10)th element
Run Code Online (Sandbox Code Playgroud)
#define INDX(a,b) (a*200+b);
Run Code Online (Sandbox Code Playgroud)
然后参考我的blob:
memory[INDX(a,b)];
Run Code Online (Sandbox Code Playgroud)
我更喜欢:
memory[a][b];
Run Code Online (Sandbox Code Playgroud)
int *MAGICVAR[][200] = memory;
Run Code Online (Sandbox Code Playgroud)
没有这样的语法吗?请注意我不仅使用固定宽度数组的原因是它太大而无法放在堆栈上.
void toldyou(char MAGICVAR[][286][5]) {
//use MAGICVAR
}
//from another function:
char *memory = (char *)malloc(sizeof(char)*1820*286*5);
fool(memory);
Run Code Online (Sandbox Code Playgroud)
我收到警告,passing arg 1 of toldyou from incompatible pointer type但代码有效,我已经确认访问了相同的位置.没有使用其他功能有没有办法做到这一点?
虽然有很多不同的复杂的实现中malloc/ free的C/C++,我在寻找一个非常简单的和(特别是)小一个在一个固定大小的缓冲区和支持工作realloc.不需要螺纹安全等,并且我的物体很小并且尺寸变化不大.有没有可以推荐的实施方案?
编辑:
我将使用该实现在接收器处的通信缓冲区来传输具有可变大小的对象(接收器未知).分配的对象不会长寿,但可能同时使用多个对象.
由于每个人似乎都推荐标准的malloc,我或许应该重新提出我的问题.我需要的是在缓冲区之上的"最简单"的malloc实现,我可以根据自己的需要开始优化.也许最初的问题不清楚,因为我不是在寻找一个优化的malloc,只是为了一个简单的.我不想从一个glibc-malloc开始并扩展它,但是它的重量轻.
我想所有都malloc(sizeof(structure)) 可以这样替换:
char[sizeof(structure)]
Run Code Online (Sandbox Code Playgroud)
那么什么时候malloc需要?
我最近了解了jemalloc,它是firefox使用的内存分配器.我已经尝试通过覆盖new和delete运算符并调用jemalloc等效的malloc和free即je_malloc和je_free来集成jemalloc到我的系统中.我编写了一个执行1亿次分配的测试应用程序.我已经使用glibc malloc运行应用程序jemalloc,与jemalloc一起运行花费较少的时间进行此类分配时CPU利用率相当高,而且与malloc相比,内存占用量也更大.阅读本文关于jemalloc分析 似乎jemalloc的脚印可能比malloc更大,因为它采用技术来优化速度而不是内存.但是,我没有任何关于Jemalloc的CPU使用情况的指示.我想说明我在多处理器机器上工作的细节如下.
处理器:11 vendor_id:GenuineIntel cpu系列:6型号:44型号名称:Intel(R)Xeon(R)CPU X5680 @ 3.33GHz步进:2 cpu MHz:3325.117缓存大小:12288 KB物理ID:1个兄弟:12核心ID :10个cpu核心:6个apicid:53个fpu:是fpu_exception:是cpuid等级:11个wp:是标志:fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm系统调用nx pdpe1gb rdtscp lm constant_tsc ida nonstop_tsc arat pni monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr sse4_1 sse4_2 popcnt lahf_lm bogomips:6649.91 clflush size:64 cache_alignment:64地址大小:40位物理,48位虚拟电源管理:[8]
我正在使用top -c -b -d 1.10 -p 24670 | awk -v time = $ TIME'{print …
我正在尝试共享像这个
例子的结构:
typedef struct {
int* a;
int b;
int c;
} ex;
Run Code Online (Sandbox Code Playgroud)
在进程之间,问题是当我使用malloc初始化'a'时,它变为私有的进程堆执行此操作(或者至少我认为这是发生的事情).有没有办法用这个有效的结构创建共享内存(使用shmget,shmat)?
编辑:我在Linux上工作.
编辑:我有一个初始化缓冲区的进程,如下所示:
key_t key = ftok("gr", 'p');
int mid = shmget(key, sizeof(ex), IPC_CREAT | 0666);
ex* e = NULL;
status b_status = init(&e, 8); //init gives initial values to b c and allocate space for 'a' with a malloc
e = (ex*)shmat(mid, NULL, 0);
Run Code Online (Sandbox Code Playgroud)
另一个进程将自己附加到共享内存,如下所示:
key_t key = ftok("gr", 'p');
int shmid = shmget(key, sizeof(ex), 0);
ex* e;
e = (ex*)shmat(shmid, NULL, 0);
Run Code Online (Sandbox Code Playgroud)
然后从a获取一个元素,在这种情况下,在位置1
int i …Run Code Online (Sandbox Code Playgroud) 如何malloc在gdb中包含/查看源代码?
我想逐步执行gdb,并malloc.c在调用任何malloc函数时进入源代码.
目前gdb所说的是:
malloc.c: No such file or directory.
这个人在这里遇到了同样的问题,但他们没有提到解决方案,即如何实际进入malloc的源代码.
我在Ubuntu server 14.04,我已经尝试安装以下软件:
libc6-dbg,libc6-dev,和libc6-dbgsym.我甚至不知道其中一个软件包是否有帮助,但是安装它libc-dbgsym会给我以下错误:
dpkg: error processing archive /var/cache/apt/archives/libc6-dbgsym_2.19-0ubuntu6.6_amd64.ddeb (--unpack): trying to overwrite
'/usr/lib/debug/usr/lib/x86_64-linux-gnu/audit/sotruss-lib.so', which
is also in package libc6-dbg:amd64 2.19-0ubuntu6.6 dpkg-deb: error:
subprocess paste was killed by signal (Broken pipe)
Run Code Online (Sandbox Code Playgroud)