是指针变量相同的指针将一个元件的阵列,或零元素? [英] Is pointer to variable the same as a pointer to an array of one element, or of zero elements?

查看:141
本文介绍了是指针变量相同的指针将一个元件的阵列,或零元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大小为0 数组的长度为零的数组好的解释,肯定是值得的,相关。我没有看到它比较单元素数组和指针与对可变长度为零。

Array with size 0 Has good explanations of zero-length arrays and is certainly worthwhile and pertinent. I am not seeing it compare zero-length with single-element arrays and with pointer-to-variable.

当我问过(是C ++删除相当于删除[1]?)我没有前preSS自己好。我的问题似乎是在对新,新的[],删除更普遍的答案相同或纳入和删除[]。一些了解,我只问一个元素。在所有的评论似乎是答案正确和一致。

When I asked before (Is c++ delete equivalent to delete[1]?) I did not express myself well. My question seemed to be the same or included in more general answers about new, new[], delete, and delete[]. Some understood that I was asking only about a single element. All answers in comments seemed correct and consistent.

有是看起来像与此相同的问题的问题。但身体是如何使用C ++和Java在一起。在这里,我们只讨论C ++。

There is a question that looks like the same as this question. But the body is about using C++ and Java together. Here, we are talking only about C++.

我会建议$相当于报表的对$ psent对。的语句是声明或指针的单个可变随后的指针将一个元件的阵列的实例。然后,我会说明为什么我会认为他们是等价的或没有。

I will present pairs of proposed equivalent statements. The statements are declarations or instantiations of a pointer to a single variable followed by a pointer to an array of one element. Then I will state why I would think they are equivalent or not.

这些是对相同呢?

int one = 1;

// Sample 1. Same in the sense of pointing to an address somewhere
// whose contents equal one. Also same in the sense of not being able to 
// change to point to a different address:
int * const p_int = &one;
int a_int[1] = {1};

// Sample 2.
int * p_int1 = new int;
int * a_int1 = new int[1];

// Sample 3.
delete p_int1;
delete[] a_int1;

// Sample 4. If Sample 3 is an equivalent pair, then (given Sample 2) 
// we can write
delete[] p_int1;
delete a_int1;

当然,样品4不好的做法。

Granted, Sample 4 is bad practice.

我想:删除将调用该对象的析构函数。删除[]将调用数组中的每个元素的析构函数,然后调用数组的析构函数。在样品2个新的malloc会(这么说)变量。新[]将MALLOC一种元素的阵列,然后MALLOC的一种元素。然后,一个元素会如此设置为1,我想的这是为什么我需要调用删除[],而不是删除时,我有连一个元素的数组。我是否理解?

I am thinking: "delete" will call the destructor of the object. delete[] will call the destructor for each element of the array, and then call the destructor for the array. new in Sample 2 would malloc (so to speak) the variable. new[] would malloc an array of one element, then malloc the one element. And then that one element would be set equal to 1. So, I'm thinking THAT'S why I need to call delete[] and not delete when I have an array of even one element. Am I understanding?

如果我的理解,然后调用删除,而不是删除[]来释放一种元素的数组,那么我肯定会存在内存泄漏。内存泄漏是具体的坏事会发生。

And if I am understanding, then calling delete instead of delete[] to free an array of one element, then I will certainly have a memory leak. A memory leak is the specific "bad thing" that will happen.

不过,这个是什么:

int * a_int0 = new int[0];
delete a_int0;

那会导致内存泄漏?

Would THAT result in a memory leak?

我邀请我的术语和其他任何滥用的更正。

I invite corrections of my misuse of terminology and anything else.

推荐答案

示例1:

int const * p_int = &one;
int a_int[1] = {1};

NO ,这些都不是等价的。指针是不一样的事,作为一个数组。他们不是等同于同样的原因, 1 是不一样的的std ::矢量< INT> {1} :一个范围内的一个元素的是不一样的东西作为一个元件。

NO, these are not equivalent. A pointer is not the same thing as an array. They are not equivalent for the same reason that 1 is not the same as std::vector<int>{1}: a range of one element is not the same thing as one element.

示例2:

int * p_int1 = new int;
int * a_int1 = new int[1];

这是排序当量。你必须删除他们不同,但除此之外,你可以使用的方式 p_int1 a_int1 是一样的。你可以把无论是作为一个范围(结束于 p_int1 + 1 a_int1 + 1 ,分别)。

These are sort of equivalent. You have to delete them differently, but otherwise the way you would use p_int1 and a_int1 is the same. You could treat either as a range (ending at p_int1+1 and a_int1+1, respectively).

示例3:

delete p_int1;
delete[] a_int1;

这是我想相当于在这个意义上,无论正确地释放这两个变量各自的记忆。

These are I suppose equivalent in the sense that both correctly deallocate the respective memory of the two variables.

示例4:

delete[] p_int1;
delete a_int1;

这是我想相当于在这个意义上,无论正确释放这两个变量各自的记忆。

These are I suppose equivalent in the sense that both incorrectly deallocate the respective memory of the two variables.

这篇关于是指针变量相同的指针将一个元件的阵列,或零元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆