Author |
Message |
Flikerator
|
Posted: Sat Oct 29, 2005 12:32 am Post subject: Array Question |
|
|
code: | int numbers[5];
std::cout << ??
numbers [??] = 100 |
If I want to declare the 5th number without calling five how would I do that?
Example (In Turing);
code: | var numbers : array 1..5 of int
put upper (numbers)
numbers (upper(numbers)) := 100 |
So basically I want to know how I would use an upper function in C++;
Also if its possible to call the different uppers in say, a 2Dimensional array, or a 6th dimiensional array that would be very helpful ^^
Thanks. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sat Oct 29, 2005 3:10 am Post subject: (No subject) |
|
|
You don't. If you need to figure out the size of a collection, use a vector instead of an array. |
|
|
|
|
|
1of42
|
Posted: Sun Oct 30, 2005 12:25 am Post subject: (No subject) |
|
|
To expand on what wtd said: arrays in C++ are nothing more than glorified pointers. An array's name is a pointer to the first value in that array. So really,
is just
Now, with that digression in mind, you see that it is really quite impossible to tell the length of an array. So, use a vector, which keeps track of its own size. Call the size() method to find out the length of the vector. |
|
|
|
|
|
rizzix
|
Posted: Sun Oct 30, 2005 11:43 am Post subject: (No subject) |
|
|
1of42 wrote:
actually..
is really |
|
|
|
|
|
Flikerator
|
Posted: Mon Oct 31, 2005 9:08 am Post subject: (No subject) |
|
|
Alright thanks. Ill have to check into this vector thing, so far the tutorials I have havn't mentioned them. Anyone notice how crappy they are? I try to use a variety of them and take out some good things about them...Thats what you get for studying it in highschool :/ |
|
|
|
|
|
wtd
|
|
|
|
|
beard0
|
Posted: Mon Oct 31, 2005 1:25 pm Post subject: (No subject) |
|
|
wtd: in the tutorial you linked to, you have a bit of an argument over ++i vs i++ with rizzix that never really finished. I was going to PM rizzix with my take on it, but wanted to make sure I'm understanding things properly first:
++i:
increments the value
returns new value
i++:
copies the value to another memory location
increments the value
returns the copied value
rizzix, about i++ wrote: what the compiler could do is return the current value and then increment the value in register. no need to return copys..
And this is impossible because you must return a value as the last thing a command does. |
|
|
|
|
|
rizzix
|
Posted: Mon Oct 31, 2005 3:05 pm Post subject: (No subject) |
|
|
beard0 wrote: And this is impossible because you must return a value as the last thing a command does. That's not how it works in Java...
its a foolish c++ thing.. cuz of the way c++ implements operator overloading.. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
beard0
|
Posted: Mon Oct 31, 2005 3:50 pm Post subject: (No subject) |
|
|
rizzix wrote: That's not how it works in Java...
Are you sure that it's not just that the JVM is doing some compensating in the background, causing extra instruction cycles? I would think that the value to be returned would have to be somewhere in memory at the time the method ends. Unless something really weird happens, and you return a value, and then run the remainder of the method concurrently with the calling method, which I seriously doubt happens. |
|
|
|
|
|
rizzix
|
Posted: Mon Oct 31, 2005 5:20 pm Post subject: (No subject) |
|
|
beard0 wrote: rizzix wrote: That's not how it works in Java...
Are you sure that it's not just that the JVM is doing some compensating in the background, causing extra instruction cycles? I would think that the value to be returned would have to be somewhere in memory at the time the method ends. Unless something really weird happens, and you return a value, and then run the remainder of the method concurrently with the calling method, which I seriously doubt happens. I know my Java trust me
To start with: you have blured the line between the mechanics of the machine vs the syntax rules of the language.
The JVM is not the Java language... the JVM is a machine... being a machine it can do anything and everything a machine can possibly do.. Good knowledge on ASM would be very helpfull to understand how everything works under the hood... but this example would demonstrate the specifics relating to the postfix operator in java..
prefix code: Java: | int i = 0, j = 0;
j = ++i;
|
bytecode: | 0: iconst_0
1: istore_1
2: iconst_0
3: istore_2
4: iinc 1, 1
7: iload_1
8: istore_2
9: return
|
postfix code: Java: | int i = 0, j = 0;
j = i++;
|
bytecode: | 0: iconst_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_1
5: iinc 1, 1
8: istore_2
9: return
|
notice that in prefix code the bytecode generated ( 4 & 7 ) first increment, then store..
while in the postfix code the bytecode generated ( 5 & 8 ) first store then increment..
oh yea... efficient! |
|
|
|
|
|
|