简体   繁体   English

Malloc和释放多个指针

[英]Malloc and free multiple pointers

I'm working with a c++ program that uses multiple buffers and I'm getting a seg fault a few minutes into running the program. 我正在使用使用多个缓冲区的c ++程序,并且在运行该程序几分钟后出现段错误。 I'm unsure of the correct way to go about it. 我不确定执行此操作的正确方法。 The code that I think is giving me the fault is currently as such: 我认为给我带来错误的代码目前是这样的:

int *myBuf1, *myBuf2;
myBuf1 = (int*)malloc(sizeof(int) * maxPacketSize);
myBuf2 = myBuf1;
// do work

if(myBuf1) {
    free(myBuf1);
}
myBuf1 = NULL;
myBuf2 = NULL;

My first question is: with the assignment 'myBuf2 = myBuf1', does myBuf2 allocate a different block of memory with sizeof(int) * maxPacketSize, or is it just a pointer to the same block of memory allocated by myBuf1? 我的第一个问题是:通过赋值“ myBuf2 = myBuf1”,myBuf2是否使用sizeof(int)* maxPacketSize分配不同的内存块,还是仅指向myBuf1分配的同一块内存的指针?

Secondly, should I be freeing myBuf2 as well as myBuf1, then setting both to NULL? 其次,我应该同时释放myBuf2和myBuf1,然后将它们都设置为NULL吗? Above I'm only freeing myBuf1. 上面,我只释放myBuf1。 OR, is it best to do: 或者,最好这样做:

myBuf1 = (int*)malloc(sizeof(int) * maxPacketSize);
myBuf2 = (int*)malloc(sizeof(int) * maxPacketSize);

then free both? 然后都释放吗?

Q: with the assignment 'myBuf2 = myBuf1', does myBuf2 allocate a different block of memory? 问:分配为“ myBuf2 = myBuf1”时,myBuf2是否分配其他内存块?

A: No 答:不可以

Q: Secondly, should I be freeing myBuf2 as well as myBuf1? 问:其次,我应该同时释放myBuf2和myBuf1吗?

A: No. One malloc (), one free(). 答:不可以。一个malloc(),一个free()。

Q: [Should I] then set both to NULL?

A: Yes, I would strongly encourage setting ALL pointers to NULL. 答:是的,我强烈建议将ALL指针设置为NULL。

First, you haven't declared two pointers. 首先,您尚未声明两个指针。

int *myBuf1, myBuf2;

This declares a myBuf1 as an int* and myBuf2 as an int (not a pointer). 这将myBuf1声明为int*并将myBuf2int (不是指针)。 You need to write: 您需要写:

int *myBuf1, *mBuf2;

Second, no, assigning one pointer to another does not copy the memory, it just creates two pointers that point to the same thing. 其次,不,将一个指针分配给另一个指针不会复制内存,它只会创建两个指向同一事物的指针。

You only need to free one. 您只需要free一个即可。 Also, there is no need to set to NULL afterwards, although in some cases it can help in avoiding some tricky bugs. 此外,尽管在某些情况下它可以帮助避免某些棘手的错误,但之后无需设置为NULL

Assigning myBuf1 to myBuf2 simply makes myBuf2 point to the same location as myBuf1 . 分配myBuf1myBuf2只是使myBuf2指向同一位置myBuf1 Consequently doing this: 因此这样做:

myBuf1 = (int*)malloc(sizeof(int) * maxPacketSize);
myBuf2 = (int*)malloc(sizeof(int) * maxPacketSize);

and then myBuf1 = myBuf2; 然后myBuf1 = myBuf2; will just allocate memory for myBuf2 for no reason. 只会myBuf2myBuf2分配内存。

Conclusion: freeing any one of them will be sufficient. 结论:释放其中任何一个就足够了。

And BTW your myBuf2 is just an int , not an int* , which is probably the cause for your segfault. 顺便说一句,您的myBuf2只是一个int ,而不是int* ,这可能是造成段错误的原因。

Change 更改

int *myBuf1, myBuf2;

to

int *myBuf1, *myBuf2;

If you want to have to distinct blocks of memory, you will need two separate allocations: 如果要区分内存块,则需要两个单独的分配:

myBuf1 = (int*)malloc(sizeof(int) * maxPacketSize);
myBuf2 = (int*)malloc(sizeof(int) * maxPacketSize);

Consider using calloc . 考虑使用calloc

After the line: 行后:

myBuf1 = (int*)malloc(sizeof(int) * maxPacketSize);

you must check that malloc( ) did not return a NULL pointer, indicating failure. 必须检查malloc()没有返回指示失败的NULL指针。 eg: 例如:

myBuf1 = (int*)malloc(sizeof(int) * maxPacketSize);
if (myBuf1 == NULL) {
  printf ("malloc ( ) failed!\n");
  exit(1);
}

Q: [Should I] then setting both to NULL? 问:[我应该]然后将两者都设置为NULL吗?

A: Yes, I would strongly encourage setting ALL pointers to NULL. 答:是的,我强烈建议将ALL指针设置为NULL。

If you are inside a block, there no use of setting back pointers to null. 如果您在一个块内,则不会使用将后指针设置为null的方法。 For the compiler, it's doing nothing and for the reader it's more confusing. 对于编译器,它什么也不做,对于读者,则更令人困惑。

In C++, it's a good practice to use blocks to limit the scope of identifiers, especially pointers. 在C ++中,使用块限制标识符(尤其是指针)的范围是一种好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM