Skip to main content
gamedev.net
🔒 Locked

Array Sizing

Started by Penguinzchaseme Jul 1, 2004 at 1:45 PM 6 replies 750+ views
Original Post
Penguinzchaseme
Penguinzchaseme
For my program, I need to make a global array based on input - this is the basic idea: int x[N]; int main() { int N; cin >> N; } and I need to size an integer based on input: int main() { int N; cin >> N; if(N < 1000) long int x; else int x; } but of course that doesn't work, it just give me a "x undeclared" error
When Penguins are chasing you, resistance is futile, give into their demands and they may spare you
DigitalDelusion
DigitalDelusion
C++:
simple safe solution use std::vector and resize it.
use new to allocate memory when you know the size, don't forget to call delete [] (and dont forget the brackets)

C:
use malloc don't forget to call free when you're done

C99:
use a function having a VLA

C/C++ non portable but supported on most platforms:
_alloca does stack based allocation cleaned up on function exit

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
wild_pointer
wild_pointer
Quote:
Original post by Penguinzchaseme
and I need to size an integer based on input:

int main()
{
int N;
cin >> N;
if(N < 1000) long int x;
else int x;
}

but of course that doesn't work, it just give me a "x undeclared" error


There is no real way to do what you're trying to do at runtime. You know integers can hold values in the range [-2,147,483,648, 2,147,483,647]? If you expect the input would be larger than that, consider an unsigned type.

[EDIT] Fixed.

[Edited by - wild_pointer on July 1, 2004 2:51:36 PM]
[size=2]
Fruny
Fruny
For the array: See what DigitalDelusion said.

For the type switch: No can do at run time. And anyway, you do realize that on todays computers, you generally have sizeof(long) == sizeof(int), right?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Spoonbender
Spoonbender
Quote:
Original post by wild_pointer

There is no real way to do what you're trying to do at runtime. You know integers can hold values in the range [-32,767, 32,767]? If you expect the input would be larger than that, just use a long (or unsigned data type if you're unworried about negative values).


Errm, correct me if I'm wrong, but haven't most of us switched to 32 bit systems, with 32 bit ints a decade or so ago? ;)
wild_pointer
wild_pointer
Quote:
Original post by Spoonster
Quote:
Original post by wild_pointer

There is no real way to do what you're trying to do at runtime. You know integers can hold values in the range [-32,767, 32,767]? If you expect the input would be larger than that, just use a long (or unsigned data type if you're unworried about negative values).


Errm, correct me if I'm wrong, but haven't most of us switched to 32 bit systems, with 32 bit ints a decade or so ago? ;)


Yes, I'm retarded. And even shorts are [-32,768, 32,767].
[size=2]
Penguinzchaseme
Penguinzchaseme
Hey thanks everyone I think I'm going to try something with unsigned and if that doens't work than I'll switch to a vector.
When Penguins are chasing you, resistance is futile, give into their demands and they may spare you
Miserable
Miserable
Quote:
Original post by Fruny
For the array: See what DigitalDelusion said.

For the type switch: No can do at run time. And anyway, you do realize that on todays computers, you generally have sizeof(long) == sizeof(int), right?

On the other hand, with 64-bit CPU's slowly gaining a foothold, this may not hold true much longer. On the AMD64 platform, sizeof(int) == 4 while sizeof(long) == 8; I don't recall what, say, PPC64 does, but I would not be surprised to find something extremely similar.

Of course, why one would do such a thing as the OP suggests in the first place is a bit unclear to me.

Topic Locked

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

Sign in to reply to this topic.