双指针问题

发布于 2024-11-13 23:23:52 字数 206 浏览 5 评论 0原文

谁能告诉我这段代码有什么问题

int* x=new int(5) ;
int i =0;
int** y = new int*[i];
for(int j = 0 ;j<5 ; j++)
{
     y[i++]=x;
}
delete[] y;

当我删除 y 时编译器总是触发一个断点 请注意,我不想删除对象“x” 谢谢

can anyone tell me what is wrong with this piece of code

int* x=new int(5) ;
int i =0;
int** y = new int*[i];
for(int j = 0 ;j<5 ; j++)
{
     y[i++]=x;
}
delete[] y;

the compiler always triggers a breakpoint when I delete y
note that I don't want to delete the object "x"
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

意中人 2024-11-20 23:23:52
int i =0;
int** y = new int*[i];

好吧,您刚刚分配了一个指向 int 的指针数组,该数组足够大以容纳...零个元素。在循环中,您将:

  1. 在数组边界之外写入(记住,零元素...)
  2. 每次迭代将循环计数器增加两次。
  3. 切勿使用循环变量j
  4. x 的值分配给...y 的每个其他元素...如前所述,超出数组范围的元素。

我真的不知道你想在这里完成什么。再来一点背景怎么样?您通过在 y 范围之外进行赋值来调用未定义的行为,因此此后任何事情都可能发生。

int i =0;
int** y = new int*[i];

Well, you have just allocated an array of pointers to int that big enough to fit... zero elements. In your loop you are:

  1. Writing outside the bounds of your array (remember, zero elements...)
  2. Incrementing your loop counter twice for each iteration.
  3. Never using your loop variable j.
  4. Assigning the value of x to... every other element of y... that is outside the bounds of the array as previously mentioned.

I don't really know what you are trying to accomplish here. How about a bit more background? You are invoking undefined behavior by assigning outside the bounds of y, so anything can happen after that.

秋叶绚丽 2024-11-20 23:23:52

当你初始化 'y' 时,它的长度为零,因为 i 是 0。

When you initialized 'y' it has zero length since i is 0.

别在捏我脸啦 2024-11-20 23:23:52

分配

int* x=new int(5) ;

您可能想

int* x=new int[5]; // square brackets

在“Then”中

int i = 0;
int** y = new int*[i];

0 个元素。在 lop 中,您可能打算迭代 j,但每次循环迭代都会对 i 进行两次增量:

for(int j = 0 ;i<5 ; i++)
{
     y[i++]=x;
}

同样在该循环中,您将每个元素设置为指向相同的 5元素数组。我很确定你确实想要这样的东西:

int const M = 5;
int const N = 5;

int** y = new int*[M];
for(int j = 0; j<M ; j++)
{
     y[j] = new int[N];
}

That line

int* x=new int(5) ;

you probably meant to be

int* x=new int[5]; // square brackets

Then in

int i = 0;
int** y = new int*[i];

you allocate for 0 elements. And in the lop you probably mean to iterate over j, but do incrementation on i twice per loop iteration:

for(int j = 0 ;i<5 ; i++)
{
     y[i++]=x;
}

Also in that loop you set each element to point to the same 5 element array. I'm pretty sure you're actually want something along this:

int const M = 5;
int const N = 5;

int** y = new int*[M];
for(int j = 0; j<M ; j++)
{
     y[j] = new int[N];
}
白鸥掠海 2024-11-20 23:23:52

y 是零长度,因为当你初始化它时 i==0
所以断点是在第二个循环中访问 y[1] 时。
但这为什么要增加两次 i 呢?

y is zero-length because when you initialize it i==0
so the breakpoint is when you access y[1] in the second loop.
but this why increment twice i?

找回味觉 2024-11-20 23:23:52

试试这个:

int len = 5;
int* x=new int[len] ;
int** y = new int*[len];
int i =0;
for(int j = 0 ;j<len ; j++)
{
     y[i++]=x;
}
delete[] y;

你不应该在代码中使用常量,除非为其声明别名变量。

Try out this:

int len = 5;
int* x=new int[len] ;
int** y = new int*[len];
int i =0;
for(int j = 0 ;j<len ; j++)
{
     y[i++]=x;
}
delete[] y;

you shouldn't use constants in your code, except when declaring alias-variables for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文