释放分配给一系列空指针的内存。

时间:2022-09-06 12:09:25

I am declaring an array of void pointers. Each of which points to a value of arbitary type.
void **values; // Array of void pointers to each value of arbitary type

我声明了一个空指针数组。每一个都指向任意类型的值。void * *值;//空指针的数组,指向任意类型的每个值

Initializing values as follows:

初始化的值如下:


    values = (void**)calloc(3,sizeof(void*));
    //can initialize values as: values = new void* [3];
    int ival = 1;
    float fval = 2.0;
    char* str = "word";
    values[0] = (void*)new int(ival);
    values[1] = (void*)new float(fval);
    values[2] = (void*)str;

    //Trying to Clear the memory allocated
    free(*values); 
    //Error: *** glibc detected *** simpleSQL: free(): invalid pointer: 0x080611b4
    //Core dumped
    delete[] values*;
    //warning: deleting 'void*' is undefined
    //Similar Error.

Now how do I free/delete the memory allocated for values ( the array of void pointers)?

现在如何释放/删除为值(空指针数组)分配的内存?

7 个解决方案

#1


6  

You have 3 things that are dynamically allocated that need to be freed in 2 different ways:

你有三个动态分配的东西需要以两种不同的方式释放:

delete reinterpret_cast<int*>( values[0]);    
delete reinterpret_cast<float*>( values[1]);

free( values); // I'm not sure why this would have failed in your example, 
               //    but it would have leaked the 2 items that you allocated 
               //    with new

Note that since str is not dynamically allocated it should not (actually cannot) be freed.

注意,由于str不是动态分配的,所以不应该(实际上不能)释放它。

A couple of notes:

一些笔记:

  • I'm assuming that the sizeof(void) was meant to be sizeof(void*) since what you have won't compile
  • 我假设sizeof(void)应该是sizeof(void*),因为您所拥有的不会编译
  • I'm not going to say anything about your seemingly random casting except that it looks like code that ready for disaster in general
  • 我不会说任何关于你的随机选择的东西,除了它看起来像为灾难做好准备的代码

#2


7  

I suspect the issue is with the way that you allocated values: values = (void*)calloc(3,sizeof(void)). That should be sizeof(void *) rather than just sizeof(void).

我怀疑问题在于您分配值的方式:values = (void*)calloc(3,sizeof(void))。应该是sizeof(void *)而不是sizeof(void)。

sizeof(void) may be zero or something else that makes no sense, so you're not really allocating any memory to begin with... it's just dumb luck that the assignments work, and then the error pops up when you try to deallocate the memory.

sizeof(void)可能是零或者其他没有意义的东西,所以你并没有真正地分配任何内存。幸运的是,这些赋值是有效的,当你试图重新分配内存时,错误就会出现。

EDIT: You're also asking for trouble by alternating between C++-style new/delete with C-style malloc/free. It is okay to use them both as long as you don't delete something you malloc'ed or free something you new'ed, but you're going to mix them up in your head if you go like this.

编辑:在c++风格的新/删除和C风格的malloc/free之间切换也会带来麻烦。只要你不删除你不喜欢的东西或释放你喜欢的东西,你可以同时使用它们,但是如果你这样做的话,你会把它们混合在你的脑海里。

#3


4  

This is the perfect situation for the boost::any class
Also you may want to consider using a vector rather than allocating your own memory.

对于boost:::任何类都可以考虑使用向量,而不是分配自己的内存。

std::vector<boost::any>   data;
boost::any i1 = 1; // add integer
data.push_back(i1);

boost::any f1 = 1.0; // add double
data.push_back(f1);

data.push_back("PLOP"); // add a char *

std:: cout << boost::any_cast<int>(data[0]) + boost::any_cast<double>(data[1])
           << std::endl;

Going back to your original code the main problem was:

回到原始代码,主要问题是:

values = (void*)calloc(3,sizeof(void));

// This should  have been
void** values = (void**)calloc(3,sizeof(void*));

// Freeing the members needs care as you need to cast them
// back to the correct type before you release the memory.

// now you can free the array with
free(values);

Also note: Though it is not illegal to use both new/delete and calloc/free in the same piece of code it is frowned upon. Mainly because it is easy to get things mixed up and that could potentially be fatal.

同时注意:虽然在同一段代码中使用新的/删除和calloc/free并不违法,但这是不允许的。主要是因为很容易把事情搞混,这可能是致命的。

#4


1  

You're mixing new and *alloc(). That's a no-no, and can lead to undefined results.

你混合了new和*alloc()这是不允许的,会导致未定义的结果。

#5


0  

I'm not sure why you are using new if you're doing things in C (referencing the tag here).

我不知道为什么你在用C语言(引用这里的标签)来使用new。

I would malloc the individual pieces of the array I need and then free them when I'm done I suppose. You can't free something you didn't first malloc. You also can't delete a void pointer.

我要malloc,我需要的数组的各个部分,然后在我完成的时候释放它们。你不能免费获得你没有的东西。你也不能删除空指针。

#6


0  

Note that you're also not deleting values[0] and values[1] which is a memory leak, Yet by your design you can't free values[2] since its a pointer into you .data section.

注意,您也没有删除值[0]和值[1],这是一个内存泄漏,但是根据您的设计,您不能释放值[2],因为它是指向您的.data部分的指针。

#7


0  

You'd have to keep track of how many void* were originally calloc'd, and iterate over them, free-ing each one, then free the original values variable.

你必须跟踪有多少void*最初被calloc调用,并对它们进行迭代,释放每个,然后释放原始值变量。

darn formatting... (the preview is working fine).

该死的格式…(预览效果很好)。

int ct = 3;
values = (void*)calloc(ct,sizeof(void));
//can initialize values as: values = new void* [3];
int ival = 1;
float fval = 2.0;
char* str = "word";
values[0] = (void*)new int(ival);
values[1] = (void*)new float(fval);
values[2] = (void*)str;

for ( int i = 0; i < ct; i++ ) [
    delete( values[i] );
}
free( values );

#1


6  

You have 3 things that are dynamically allocated that need to be freed in 2 different ways:

你有三个动态分配的东西需要以两种不同的方式释放:

delete reinterpret_cast<int*>( values[0]);    
delete reinterpret_cast<float*>( values[1]);

free( values); // I'm not sure why this would have failed in your example, 
               //    but it would have leaked the 2 items that you allocated 
               //    with new

Note that since str is not dynamically allocated it should not (actually cannot) be freed.

注意,由于str不是动态分配的,所以不应该(实际上不能)释放它。

A couple of notes:

一些笔记:

  • I'm assuming that the sizeof(void) was meant to be sizeof(void*) since what you have won't compile
  • 我假设sizeof(void)应该是sizeof(void*),因为您所拥有的不会编译
  • I'm not going to say anything about your seemingly random casting except that it looks like code that ready for disaster in general
  • 我不会说任何关于你的随机选择的东西,除了它看起来像为灾难做好准备的代码

#2


7  

I suspect the issue is with the way that you allocated values: values = (void*)calloc(3,sizeof(void)). That should be sizeof(void *) rather than just sizeof(void).

我怀疑问题在于您分配值的方式:values = (void*)calloc(3,sizeof(void))。应该是sizeof(void *)而不是sizeof(void)。

sizeof(void) may be zero or something else that makes no sense, so you're not really allocating any memory to begin with... it's just dumb luck that the assignments work, and then the error pops up when you try to deallocate the memory.

sizeof(void)可能是零或者其他没有意义的东西,所以你并没有真正地分配任何内存。幸运的是,这些赋值是有效的,当你试图重新分配内存时,错误就会出现。

EDIT: You're also asking for trouble by alternating between C++-style new/delete with C-style malloc/free. It is okay to use them both as long as you don't delete something you malloc'ed or free something you new'ed, but you're going to mix them up in your head if you go like this.

编辑:在c++风格的新/删除和C风格的malloc/free之间切换也会带来麻烦。只要你不删除你不喜欢的东西或释放你喜欢的东西,你可以同时使用它们,但是如果你这样做的话,你会把它们混合在你的脑海里。

#3


4  

This is the perfect situation for the boost::any class
Also you may want to consider using a vector rather than allocating your own memory.

对于boost:::任何类都可以考虑使用向量,而不是分配自己的内存。

std::vector<boost::any>   data;
boost::any i1 = 1; // add integer
data.push_back(i1);

boost::any f1 = 1.0; // add double
data.push_back(f1);

data.push_back("PLOP"); // add a char *

std:: cout << boost::any_cast<int>(data[0]) + boost::any_cast<double>(data[1])
           << std::endl;

Going back to your original code the main problem was:

回到原始代码,主要问题是:

values = (void*)calloc(3,sizeof(void));

// This should  have been
void** values = (void**)calloc(3,sizeof(void*));

// Freeing the members needs care as you need to cast them
// back to the correct type before you release the memory.

// now you can free the array with
free(values);

Also note: Though it is not illegal to use both new/delete and calloc/free in the same piece of code it is frowned upon. Mainly because it is easy to get things mixed up and that could potentially be fatal.

同时注意:虽然在同一段代码中使用新的/删除和calloc/free并不违法,但这是不允许的。主要是因为很容易把事情搞混,这可能是致命的。

#4


1  

You're mixing new and *alloc(). That's a no-no, and can lead to undefined results.

你混合了new和*alloc()这是不允许的,会导致未定义的结果。

#5


0  

I'm not sure why you are using new if you're doing things in C (referencing the tag here).

我不知道为什么你在用C语言(引用这里的标签)来使用new。

I would malloc the individual pieces of the array I need and then free them when I'm done I suppose. You can't free something you didn't first malloc. You also can't delete a void pointer.

我要malloc,我需要的数组的各个部分,然后在我完成的时候释放它们。你不能免费获得你没有的东西。你也不能删除空指针。

#6


0  

Note that you're also not deleting values[0] and values[1] which is a memory leak, Yet by your design you can't free values[2] since its a pointer into you .data section.

注意,您也没有删除值[0]和值[1],这是一个内存泄漏,但是根据您的设计,您不能释放值[2],因为它是指向您的.data部分的指针。

#7


0  

You'd have to keep track of how many void* were originally calloc'd, and iterate over them, free-ing each one, then free the original values variable.

你必须跟踪有多少void*最初被calloc调用,并对它们进行迭代,释放每个,然后释放原始值变量。

darn formatting... (the preview is working fine).

该死的格式…(预览效果很好)。

int ct = 3;
values = (void*)calloc(ct,sizeof(void));
//can initialize values as: values = new void* [3];
int ival = 1;
float fval = 2.0;
char* str = "word";
values[0] = (void*)new int(ival);
values[1] = (void*)new float(fval);
values[2] = (void*)str;

for ( int i = 0; i < ct; i++ ) [
    delete( values[i] );
}
free( values );