Array size

P

Pavan

Hi ,
Suppose if I declare like this

int n = 0;

cin >> n;

int arr[n];


Suppose when we run this application, we enter value of n as 10, then
does it create an array for 10 integers or does it create only for 0?

First of all is it safe to do like that?
 
M

Michael DOUBEZ

Tim Love a écrit :
First of all, check that it's legal ANSI C++. No doubt it will be one
day, but is it yet?

It is valid C99 and it is true that now, most c++ compiler will accept
this code unless you specific strict compilation options (std=c89).
>>int n = 0;
>>cin >> n;
>>int arr[n];
>>Suppose when we run this application, we enter value of n as 10, then
>>does it create an array for 10 integers or does it create only for 0?

It does create a array of 10 elements. It is called VLA (Variable Length
Array).

I don't know; what happens if n<0 ? I guess C is silent about this case.

Do you really need it ? I guess not.
Why don't you use a plain vector ?

Michael
 
P

Pavan

Tim Love a écrit :
First of all, check that it's legal ANSI C++. No doubt it will be one
day, but is it yet?

It is valid C99 and it is true that now, most c++ compiler will accept
this code unless you specific strict compilation options (std=c89).

 >>int n = 0;
 >>cin >> n;
 >>int arr[n];

 >>Suppose when we run this application, we enter value of n as 10, then
 >>does it create an array for 10 integers or does it create only for 0?

It does create a array of 10 elements. It is called VLA (Variable Length
Array).

 >>First of all is it safe to do like that?

I don't know; what happens if n<0 ? I guess C is silent about this case.

Do you really need it ? I guess not.
Why don't you use a plain vector ?

Michael

I didnt need exactly that kind of scenario.

But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....
}

In the above I have a function that will create a array based on the
size of vector argument passed
to it. So I wanted to know is it ok to do like that. Also is it
allowed in VC++(windows) also?
 
M

Michael DOUBEZ

Pavan a écrit :
Tim Love a écrit :


I don't know; what happens if n<0 ? I guess C is silent about this case.

Do you really need it ? I guess not.
Why don't you use a plain vector ?

I didnt need exactly that kind of scenario.

But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....
}

In the above I have a function that will create a array based on the
size of vector argument passed
to it. So I wanted to know is it ok to do like that. Also is it
allowed in VC++(windows) also?

I don't know.
In this case you have a size >=0 so it seems safer.

Here again, before all experience, you could use vector and avoid
possible UB. Moreover, it is not guaranteed that compilers support
flawlessly the VLA.

Michael
 
S

Salt_Peter

Tim Love a écrit :
It is valid C99 and it is true that now, most c++ compiler will accept
this code unless you specific strict compilation options (std=c89).
int n = 0;
cin >> n;
int arr[n];
Suppose when we run this application, we enter value of n as 10, then
does it create an array for 10 integers or does it create only for 0?
It does create a array of 10 elements. It is called VLA (Variable Length
Array).
I don't know; what happens if n<0 ? I guess C is silent about this case.
Do you really need it ? I guess not.
Why don't you use a plain vector ?

I didnt need exactly that kind of scenario.

You mean you didn't need to program for *all* scenarios?
uh, wellcome to the real world. You can wake up now.
But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....

}

In the above I have a function that will create a array based on the
size of vector argument passed
to it. So I wanted to know is it ok to do like that. Also is it
allowed in VC++(windows) also?


Is it ok? not really. Its rather innefficient and dubious at best.
I would rather stick to the vector than have to complicate the code by
populating an array.
The vector stores its elements in contiguous memory, like an array.
The vector is a much more capable container than an array for many,
many reasons.

Note the reference parameter, otherwise you are passing by value:

bool f(const std::vector& vec) { ... }
 
C

ciccio

I didnt need exactly that kind of scenario.
But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....
}

Why don't you use then std::vector<int> arr(vec.size()) instead? you
anyway already work with the vector class.
 
S

Spitfire

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ciccio wrote:
|> I didnt need exactly that kind of scenario.
|>
|> But what I need is I have a function like this
|>
|> bool f(std::vector vec)
|> {
|>
|> int arr[vec.size()];
|> ......
|> ......
|> .....
|> }
|
| Why don't you use then std::vector<int> arr(vec.size()) instead? you
| anyway already work with the vector class.

Was wondering why no one is suggesting creating a pointer and
initializing *dynamically* its size, based on the vector size? Sorry, if
I'm overlooking something really important here!


- --
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
“I'm smart enough to know that I'm dumb.â€
~ - Richard P Feynman
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4 (GNU/Linux)

iD8DBQFHuavcKVWYDRRZoskRAuW3AJ9hydLE1Xb7HAMf+kF+SSfzhTTxWwCggKli
I4zqPBd64DWduSlDIXyE8CE=
=QrGv
-----END PGP SIGNATURE-----
 
D

Daniel T.

Pavan said:
But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....
}

You are mistaken, you don't need to have a function like that at all.
Tell us what you are really trying to do and we can probably help you.
 
S

Scott McPhillips [MVP]

But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....

}

In the above I have a function that will create a array based on the
size of vector argument passed
to it. So I wanted to know is it ok to do like that. Also is it
allowed in VC++(windows) also?


No, it is not valid C++ and will not compile in VC++ 2005. The best
solution is to define a new vector.
 
R

Richard Herring

Spitfire <[email protected]> said:
ciccio wrote:
|> I didnt need exactly that kind of scenario.
|>
|> But what I need is I have a function like this
|>
|> bool f(std::vector vec)
|> {
|>
|> int arr[vec.size()];
|> ......
|> ......
|> .....
|> }
|
| Why don't you use then std::vector<int> arr(vec.size()) instead? you
| anyway already work with the vector class.

Was wondering why no one is suggesting creating a pointer and
initializing *dynamically* its size, based on the vector size?

That's in effect what vector does, internally.
Sorry, if
I'm overlooking something really important here!

Cleanup on exit.
Exception safety.
 
J

Jim Langston

Pavan said:
Tim Love a écrit :
...
First of all is it safe to do like that?
First of all, check that it's legal ANSI C++. No doubt it will be
one day, but is it yet?

It is valid C99 and it is true that now, most c++ compiler will
accept this code unless you specific strict compilation options
(std=c89).
int n = 0;
cin >> n;
int arr[n];
Suppose when we run this application, we enter value of n as 10,
then does it create an array for 10 integers or does it create
only for 0?

It does create a array of 10 elements. It is called VLA (Variable
Length Array).
First of all is it safe to do like that?

I don't know; what happens if n<0 ? I guess C is silent about this
case.

Do you really need it ? I guess not.
Why don't you use a plain vector ?

Michael

I didnt need exactly that kind of scenario.

But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....
}

In the above I have a function that will create a array based on the
size of vector argument passed
to it. So I wanted to know is it ok to do like that. Also is it
allowed in VC++(windows) also?

As stated, it is not currently a part of the C++ standard. But my question
is why? Why do you want to create a variable lenght array as shown when you
can use a vector? What is wrong with:

std::vector<int> arr( vec.size() );

How does that not serve your purpose?

You can use that anywhere you can use an array of int although you need to
go an extra step when getting a pointer to the first element. With the
array you would use
arr
with the vector it would be
&arr[0]

&arr[0] should be an int* which should be usable where you would need either
an int * or int []
 
D

Daniel T.

Jim Langston said:
Pavan wrote:
But what I need is I have a function like this

bool f(std::vector vec)
{

int arr[vec.size()];
......
......
.....
}

In the above I have a function that will create a array based on the
size of vector argument passed
to it. So I wanted to know is it ok to do like that. Also is it
allowed in VC++(windows) also?

As stated, it is not currently a part of the C++ standard. But my question
is why? Why do you want to create a variable lenght array as shown when you
can use a vector? What is wrong with:

std::vector<int> arr( vec.size() );

How does that not serve your purpose?

You can use that anywhere you can use an array of int although you need to
go an extra step when getting a pointer to the first element. With the
array you would use
arr
with the vector it would be
&arr[0]

&arr[0] should be an int* which should be usable where you would need either
an int * or int []

And also note that since he is passing the vector in by value, he
probably doesn't need to make yet another copy anyway.
 
M

Michael DOUBEZ

Richard Herring a écrit :
Spitfire <[email protected]> said:
ciccio wrote:
|> I didnt need exactly that kind of scenario.
|>
|> But what I need is I have a function like this
|>
|> bool f(std::vector vec)
|> {
|>
|> int arr[vec.size()];
|> ......
|> ......
|> .....
|> }
|
| Why don't you use then std::vector<int> arr(vec.size()) instead? you
| anyway already work with the vector class.

Was wondering why no one is suggesting creating a pointer and
initializing *dynamically* its size, based on the vector size?

That's in effect what vector does, internally.
Sorry, if
I'm overlooking something really important here!

Cleanup on exit.
Exception safety.

For those issues, he could use boost::scoped_array<>.

It is more that he is pondering the usage of an unsafe, possibly UB
solution when there is an equivalent, simple, standard and safe
solution. Moreover, iterators and associated support are readily available.

std::vector<int> arr(vec.size());
is clear enough (except for the horrible name 'arr' :) )


Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top