Skip to main content
GameDev.net gamedev.net
🔒 Locked

Redeclaring an array

Started by Iccarus Jan 20, 2006 at 10:09 AM 15 replies 2.1k views
Original Post
Iccarus
Iccarus
Is there anyway in C++ for me to redeclare an array? I want the array to have (x*y)+1 elements but the thing is being declared in a class file privately and so won't let me declare it like that. So is there anyway to declare the array as 10 and then change it at a later point to (x*y)+1?
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
SiCrane
SiCrane
In C++ array types have a fixed size. You want to consider using a std::vector instead of a normal array.
Iccarus
Iccarus
any header files needed for that? isn't it?
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
rip-off
rip-off
Quote:
Original post by Iccarus
any header files needed for that? isn't it?


#include

note the lack of a file extension

the vector is a template type in namespace std.

std::vector myVector;
Iccarus
Iccarus
std::vector maparray[length];

is that the correct initialisation (sorry I haven't used vectors before)
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
SiCrane
SiCrane
If you declare it as a local variable:
std::vector<int> maparray(length);

As a member variable it's just
std::vector<int> maparray;

You can change the size of the vector with the .resize() member function.
rip-off
rip-off
Quote:
Original post by Iccarus
std::vector maparray[length];



is that the correct initialisation (sorry I haven't used vectors before)


almost...
std::vector maparray(length);

this will contain length ints.

use this if you want to start with them zeroised

std::vector maparray(length,0);


note that the vector is a dynamic array. it will ensure it has enough room for anything you try to do.

eg.

vector<int> vec;// note that even though we dont specify the length, the vector will ensure there is enoughfor( int i = 0 ; i < 10 ; ++i )   vec.push_back(i);// adds the number to the back of the vectorfor( int i = 0 ; i < vec.size() ; ++i )   std::cout << vec << '\n';
Iccarus
Iccarus
Cheers, you guys.

Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
SiCrane
SiCrane
Quote:
Original post by rip-off
use this if you want to start with them zeroised

std::vector maparray(length,0);

The 0 is unncessary. When you specify an initial size with the constructor, it automatically zeros the vector elements when the vector is of int type.
rip-off
rip-off
Quote:
Original post by SiCrane
Quote:
Original post by rip-off
use this if you want to start with them zeroised

std::vector maparray(length,0);

The 0 is unncessary. When you specify an initial size with the constructor, it automatically zeros the vector elements when the vector is of int type.


sweet.
specialised templates i assume.
didnt know that.
SiCrane
SiCrane
Quote:
Original post by rip-off
sweet.
specialised templates i assume.
didnt know that.


No. The sized constructor for std::vector<> looks like:

explict vector(size_type n, const T & value = T(), const Allocator & allocator = Allocator());

The vector is constructed with n elements copy constructed from value. When no value is specified, the default argument used is T(), which in the case of T being int, turns into 0.
rip-off
rip-off
Quote:
Original post by SiCrane
No. The sized constructor for std::vector<> looks like:

explict vector(size_type n, const T & value = T(), const Allocator & allocator = Allocator());

The vector is constructed with n elements copy constructed from value. When no value is specified, the default argument used is T(), which in the case of T being int, turns into 0.


suppose it is my fault for assuming stuff.

i assumed that because:

int i;

doesn't guarentee that i is any value, that int() wouldn't either( not that i've often thought about how "int()" would work )

thanks for the info
Zahlman
Zahlman
Yeah. Weird, isn't it?

int main() {  int i; // not initialized to anything  int j = 0; // initialized to 0  int k(0); // initialized to 0  int l = int(); // initialized to 0  int m(); // not an int, but instead a function declaration :)  m();}int m() {  return 42;}
Sharlin
Sharlin
And it gets better:

float f;int i(f);      // Warning: converting from float to intint j(int(f)); // Let's try an explicit cast... gah, a function declaration! (*int k((int)f); // Okint m(int());  // No bonus, again a function declaration :P


*) I wonder why the grammar allows parentheses around the variable name in a variable declaration in the first place...
Zahlman
Zahlman
Quote:
Original post by Sharlin
And it gets better:

float f;int i(f);      // Warning: converting from float to intint j(int(f)); // Let's try an explicit cast... gah, a function declaration! (*int k((int)f); // Okint m(int());  // No bonus, again a function declaration :P


*) I wonder why the grammar allows parentheses around the variable name in a variable declaration in the first place...


That's not... oh, you mean parentheses around the parameter name in a function declaration. I guess that's indeed what's going on there - it couldn't be declaring a function that accepts a function pointer, because that would be int j(int(*)(f)). :S Yeah, a serious annoyance, it is. Any word on whether 0x will clean this mess up?
Sharlin
Sharlin
Quote:
Original post by Zahlman
That's not... oh, you mean parentheses around the parameter name in a function declaration.


It seems to work in any variable declaration, not just within function argument lists.

Quote:
Any word on whether 0x will clean this mess up?


There certainly exist proposals to that end, the last word in the matter is this paper by Stroustrup and Dos Reis. Basically, they seek to generalize the use of {} initializers and also enable constructing user-defined types via initializer lists. The proposal seems very reasonable and probably has quite a good chance to be included at least in some form.

[Edited by - Sharlin on January 20, 2006 8:38:28 PM]
M2tM
M2tM
The C++ way has been mentioned.

In C (or if you really want to just use a plain array) you can do it like so:

to initialize
char *array = (char*)malloc(sizeof(char)*x);

to make bigger
x++;
array = (char*)realloc(sizeof(char)*x);


Let me say right now that if I catch you doing this in C++ (without a good reason) I'll break your kneecaps, but it is important to know how to do it.
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.