简体   繁体   English

如何在 C++ 中初始化指针数组

[英]How to initialize a pointers array in c++

I used valgrind to test my code, and found out that the problem might be with the way I initialized the pointer array.我使用 valgrind 来测试我的代码,发现问题可能出在我初始化指针数组的方式上。 But I am not sure where the problem is.但我不确定问题出在哪里。

Here's the error:这是错误:

Conditional jump or move depends on uninitialised value(s)

==7846==    at 0x41065A: Scene::drawscene() const (scene.cpp:142)

==7846==    by 0x4111C3: main (testscene.cpp:48)

==7846==  Uninitialised value was created by a heap allocation

==7846==    at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==7846==    by 0x50C1829: operator new(unsigned long) (in /usr/lib/libc++abi.so.1.0)

==7846==    by 0x40FBE8: Scene::copy(Scene const&) (scene.cpp:45)

==7846==    by 0x40FB7C: Scene::Scene(Scene const&) (scene.cpp:22)

==7846==    by 0x41108E: main (testscene.cpp:41)

==7846== 
==7846== Conditional jump or move depends on uninitialised value(s)

==7846==    at 0x4107AB: Scene::drawscene() const (scene.cpp:149)

==7846==    by 0x4111C3: main (testscene.cpp:48)

==7846==  Uninitialised value was created by a heap allocation

==7846==    at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==7846==    by 0x50C1829: operator new(unsigned long) (in /usr/lib/libc++abi.so.1.0)
==7846==    by 0x40FBE8: Scene::copy(Scene const&) (scene.cpp:45)
==7846==    by 0x40FB7C: Scene::Scene(Scene const&) (scene.cpp:22)
==7846==    by 0x41108E: main (testscene.cpp:41)

And the way I initial the array is below:我初始化数组的方式如下:

 Image** image;
    int** sequence;
    images = new Image*[max];
    sequence = new int*[max];

To my eye, this makes little sense:在我看来,这毫无意义:

int** sequence;
sequence = new int*[max];

I omit images here, because this integer pointer array indicates what may be the flaw in your thinking, reflected in the images array as well.我在这里省略了图像,因为这个整数指针数组表明你的想法可能存在什么缺陷,也反映在图像数组中。

Why would anyone make an array of integer pointers?为什么会有人制作一个整数指针数组? On most hardware, the integers are the same size as the pointers, and where are the actual integers they would point to?在大多数硬件上,整数与指针的大小相同,它们指向的实际整数在哪里?

Is it possible you really mean有没有可能你真的是这个意思

int* sequence;
sequence = new int[max];

An array of integers, which could now actually store numbers?一个整数数组,现在可以实际存储数字?

Perhaps that's the same problem with your images array, because you are forming an array of image pointers.也许这与您的图像数组存在相同的问题,因为您正在形成一个图像指针数组。 Now, an image, whatever that actually is, may indeed be more deserving of an array of pointers to images - that is actually a more common concept than an array of pointers to integers.现在,一个图像,不管它实际上是什么,可能确实更应该得到一个指向图像的指针数组——这实际上是一个比指向整数的指针数组更常见的概念。

So, the question now becomes, if you have an array of image pointers, where are the images you are pointing to?所以,现在的问题变成了,如果你有一个图像指针数组,你指向的图像在哪里? Are you, sometime later, doing:稍后,您是否正在执行以下操作:

images[ n ] = new image;

Or something to that effect?或者有什么效果? That would point the entry n to an image, at least.至少,这会将条目 n 指向图像。 Or, do you really mean to create an array of images, as above I suggested an array of integers:或者,你真的想创建一个图像数组,就像上面我建议的一个整数数组:

Image* image;
images = new Image[max];

I think that what you are trying to do is initialize a dynamic 2D array of ints and of the type Image.我认为你想要做的是初始化一个动态二维数组的整数和类型的图像。 Here is a working example for initializing the sequences variable:这是初始化序列变量的工作示例:

int** sequences = new int*[max];
    for(int i = 0; i < max; ++i)
        sequences [i] = new int[max];

This is assuming that your 2D array is of the NxN .这是假设您的二维数组NxN

A 2D array is just a pointer to an array of pointers so what the code does, it first initializes the pointer to the array of pointers:二维数组只是一个指向指针数组的指针,所以代码所做的,它首先初始化指向指针数组的指针:

int** sequences = new int*[max];

But now you have an array of pointers pointing to nowhere, so you have to also initialize them:但是现在你有一个指向无处的指针数组,所以你还必须初始化它们:

for(int i = 0; i < max; ++i)
    sequences [i] = new int[max];

I'll leave it to you to do the same for the images variable.我会让你对 images 变量做同样的事情。

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

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