0
Point V[rows];

Is this allowed in C++? rows is a variable whose value is given at runtime and Point is my class.

1
  • strictly speaking, this would be a compile time error, not a run-time error. Commented Dec 6, 2010 at 7:08

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

7 Comments

but then can i acces it as V[i] or not ?
Yes. A pointer works essentially like an array. You just have to delete the array when you're done.
Eh, Point *V = new Point[rows];, obviously.
Glad to help! By the way, if you accept this answer, we both get rep points. Good luck with your program!
Thanks I just did that...i was tring it before thanking u but it said ur suppose to wait
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.