开发者

C++ runtime error?

Point V[rows];

Is this allowed in C++? rows is a开发者_JS百科 variable whose value is given at runtime and Point is my class.


In C++ the comparable idiom is:

std::vector<Point> V(rows);

It's not 100% identical, because it still calls new Point[] (c99 can use the stack), but it still gives you the vector without performing multiple allocs.


Only in C99 - it's a new feature called "variable length arrays". Normally, no.

I would strongly recommend against using this feature. If you have to do it, either use alloca, or allocate them properly, i.e. Point *V = new Point V[rows];.

BTW: Many people discourage Alloca as well. See here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜