关于指针,双指针,malloc,realloc和new的问题数组(直接设置记录)

mat*_*usa 1 c++ arrays pointers memory-management

我正在赶上指针.我写了几行代码来测试我可以动态分配2d数组的不同方法(在底部发布)

我的问题如下:

  1. 我应该在c ++或new中使用malloc吗?如果我使用new,我仍然可以使用realloc吗?
  2. 我什么时候应该使用realloc?在性能和错误方面使用它有什么含义?
  3. 在下面的例子中,我应该使用哪个对象新版本?如果答案取决于应用程序,它依赖于什么?

非常感谢,马特

#include <stdio.h>
#include <stdlib.h>

struct myObject
{
    int data;
    myObject(int i)
    {
        data = i;
    }
    myObject()
    {
        data = 0;
    }

};

int main(){
    int r = 7;
    int c = 6;

    printf("Objects using NEW===============================\n");
    //notice the triple pointer being assigned a new double pointer array
    myObject*** objectNew = new myObject** [r];

    for(int i=0;i<r;i++)
    {
        //objectNew is a 1D array of double pointers, however if we assign another layer of pointers, it becomes a 2D array of pointers to myObject
        objectNew[i] = new myObject* [c];
        for(int j=0;j<c;j++){
            objectNew[i][j] = new myObject(10*i+j);
            //notice that we dereference data (->)
            printf("objectNew[%2d][%2d]=%02d\n",i,j,objectNew[i][j]->data);
        }
    }
    delete objectNew;

    printf("Objects using NEW version 2===============================\n");
    //notice the triple pointer being assigned a new double pointer array
    myObject** objectNew2 = new myObject* [r];

    for(int i=0;i<r;i++)
    {
        //objectNew is a 1D array of double pointers, however if we assign another layer of pointers, it becomes a 2D array of pointers to myObject
        objectNew2[i] = new myObject [c];
        for(int j=0;j<c;j++){
            objectNew2[i][j] = myObject(10*i+j);
            //notice that we dereference data (->)
            printf("objectNew2[%2d][%2d]=%02d\n",i,j,objectNew2[i][j].data);
        }
    }
    delete objectNew2;

    printf("Objects using MALLOC===============================\n");
    //notice the double pointer being allocated double pointers the size of pointers to myObject
    myObject** objectMalloc =(myObject**) malloc(sizeof(myObject*)*r);

    for(int i=0;i<r;i++)
    {
        //now we are assigning array of pointers the size of myObject to each double pointer
        objectMalloc[i] = (myObject*) malloc(sizeof(myObject)*c);
        for(int j=0;j<c;j++){
            objectMalloc[i][j] = myObject(10*i+j);
            //notice that we access  data without dereferencing (.)
            printf("objectMalloc[%2d][%2d]=%02d\n",i,j,objectMalloc[i][j].data);
        }
    }
    free((void*) objectMalloc);

    //same as Malloc
    printf("Objects using CALLOC===============================\n");
    myObject** objectCalloc = (myObject**) calloc(r,sizeof(myObject*));

    for(int i=0;i<r;i++)
    {
        objectCalloc[i] = (myObject*) calloc(c,sizeof(myObject));
        for(int j=0;j<c;j++){
            objectCalloc[i][j] = myObject(10*i+j);
            printf("objectCalloc[%2d][%2d]=%02d\n",i,j,objectCalloc[i][j].data);
        }
    }
    free((void*) objectCalloc);

    printf("Int using NEW===============================\n");
    //int is not an object
    int** intNew = new int* [r];

    for(int i=0;i<r;i++)
    {
        intNew[i] = new int[c];
        for(int j=0;j<c;j++){
            intNew[i][j] = 10*i+j;
            printf("intNew[%2d][%2d]=%02d\n",i,j,intNew[i][j]);
        }
    }
    delete intNew;

    printf("Int using malloc===============================\n");
    int** intMalloc =(int**) malloc(sizeof(int*)*r);

    for(int i=0;i<r;i++)
    {
        intMalloc[i] =(int*) malloc(sizeof(int)*c);
        for(int j=0;j<c;j++){
            intMalloc[i][j] = 10*i+j;
            printf("intMalloc[%2d][%2d]=%02d\n",i,j,intMalloc[i][j]);
        }
    }
    free((void*) intMalloc);
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

wkl*_*wkl 6

  • new并且new[]是构造函数意识到的,C内存分配函数不是.
  • 同样,delete并且delete []是析构函数,而free不是.
  • realloc不能与分配的内存一起使用new,没有标准定义类似于realloc具有C++内存管理功能的功能.
  • 不要混合newdeletemalloc/ calloc/ reallocfree(即不free与分配的任何内存new,不delete与分配的任何内存malloc)

  • 如果您正在做new [],您很可能应该使用an std::vector,它为您封装了所有内存管理.

  • 如果您正在使用内存分配的原始指针,那么根据您的需要,使用智能/半智能指针(如boost::shared_ptrboost::scoped_ptr或更好)可能会更好std::auto_ptr.

  • 感谢David Thornley提出这个问题:

  • 切勿混合标量newdelete它们的数组形式,即不要delete[]分配内存new,也不要delete分配内存new[].未定义的行为到处都是.

  • 在你的例子中,你应该使用new,new[]如果你必须,因为正如我所说,它们是构造函数意识,并且你的类不是POD数据类型,因为它有一个构造函数.

有关更多信息,请参阅C++ FAQ: