我知道这些问题非常基本,但我仍然以某种方式发现它们令人困惑。 [英] I Know These Questions Are Very Basic, but I Still Somehow Find them Confusing.

查看:64
本文介绍了我知道这些问题非常基本,但我仍然以某种方式发现它们令人困惑。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.这是有效的;



int * p = new int [4] [5];



如果不是为什么;

(即允许动态分配多维数组?)



2.如何返回地址动态分配数组中的变量;

以下是否有效?



class MyClass

{

int * p;

MyClass();

int Func();

};



MyClass :: MyClass()

{

p = new int [3] [4];

}







int * MyClass :: Func()

{





return&p [3]; //这是怀疑的行

}

1.Is this valid;

int * p = new int [4][5];

if not why;
(i.e. is dynamically allocating multidimensional array allowed?)

2. How does one return the address of a variable in dynamically allocated array;
Is the following valid?

class MyClass
{
int *p;
MyClass();
int Func();
};

MyClass::MyClass()
{
p = new int [3][4];
}



int *MyClass::Func()
{


return &p[3];//This is the line in doubt
}

推荐答案

这看起来像是你的作业,但是......

1)试一试!将该行添加到您最喜欢的C / C ++编译器中,然后构建它。编译器是否抱怨?



2)嗯,编译器不会抱怨...但这并不意味着它是个好主意。请参见此处: http://en.wikipedia.org/wiki/Dangling_pointer [ ^ ]
This looks like your homework to me, but...
1) Try it! Add that line to your favorite C / C++ compiler, and build it. Does the compiler complain?

2) Well, the compiler won't complain...but that doesn't mean it's a good idea. See here: http://en.wikipedia.org/wiki/Dangling_pointer[^]


这篇关于我知道这些问题非常基本,但我仍然以某种方式发现它们令人困惑。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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