| Aramil of Elixia (772) | |
| I have read on lots of tutorials that you once an array is set, it stays that size. this is false. today i got bored in school and suddenly realized that the bare base of an array swap function i wrote can resize the array. i havent figured out how to make it smaller yet, but i can make it bigger and it will be the start of my whole array library | |
|
|
|
| maeriden (244) | |
| Isn't that std::vector? | |
|
|
|
| Aramil of Elixia (772) | |
| you can do it with vector, but for the people who cant get vectors because they are new to c++ can use this | |
|
|
|
| ResidentBiscuit (2215) | |
| Are you actually making it larger, or are you copying it to a larger array? Because the former is going to rather implementation defined. When you create an array, it's either given a set amount of space in the stack, or a set amount of space on the heap. Each of which have to be contiguous. This means you could have an array sitting right next to some other random data, in which case making it larger would be impossible as you would end up intruding into occupied memory (which the OS likely won't allow anyway). | |
|
|
|
| Aramil of Elixia (772) | |
| good guess but im not doind either. i am pretty sure this will work on normal arrays. | |
|
|
|
| cire (1850) | |
|
Perhaps you should supply some some code so it can be | |
|
|
|
| maeriden (244) | |
| Are you going to tell us how? I'm rather curious. I can't imagine a way that is not copying or using a linked list | |
|
|
|
| helios (10126) | |
| An array is fixed size by definition. Period. | |
|
Last edited on
|
|
| Aramil of Elixia (772) | |||
why would it be shot down cire? im doing it just in ints right now because i havent templated it yet
i tested it too and it worked | |||
|
|
|||
| cire (1850) | |
| I'm sorry, but that doesn't resize an array. It doesn't even calculate the current size of an array correctly. | |
|
|
|
| helios (10126) | |||
| |||
|
|
|||
| Aramil of Elixia (772) | |
| it resized it for me | |
|
|
|
| firedraco (5413) | |
| No, no it didn't. You just overwrote some random memory. | |
|
|
|
| Aramil of Elixia (772) | |
| it still resized it. do you want me to take a snap shot of the code with the output? | |
|
|
|
| helios (10126) | |
|
There's no need. It didn't. The fact that the program didn't crash isn't proof of anything. The semantics of C and C++ don't allow the resizing of arrays. There's nothing you can do to prove that an array has been resized because that's simply impossible. | |
|
|
|
| Aramil of Elixia (772) | |
| its not. i printed out the array going to element fifteen and it gave the output it was supposed too for each of them | |
|
|
|
| helios (10126) | ||||
Alright, then.
Output:
It's also interesting to note that b[12]==12. This is likely because (&b[12]) == &i on the second for loop in main(). Try calling resize() with growth == 1000. Tell us what you see. | ||||
|
Last edited on
|
||||
| Catfish3 (117) | |
When you use an array's name, what you get is the memory address of the first element. So array[20] translates to *(array + 20). (Also try 20[array] for fun.)This is basically, pointer arithmetic. The language expects you to know what you're doing, so if this ever blows up in your face, it'll be at the operating system's discretion (segmentation fault). Conclusion: your code is bad, even if you're not given an error or warning. | |
|
|
|
| darkestfright (1091) | |
|
@Aramil of Elixia I suggest you head what everyone is saying here, because they are right and you are wrong -- despite the appearance of you being correct. This is undefined behavior as far as the C++ standard goes: rainbow unicorns could should lasers through your windows because of your code and it would still be technically a "correct" program. Let me ask you, which is more likely: That everyone here who is explaining to you why your code is incorrect, are in fact wrong, despite the fact that they have referenced the C++ standard as well as given you examples showcasing why you are incorrect (this is a textbook Buffer Overflow error, http://en.wikipedia.org/wiki/Buffer_overflow) OR you have discovered the magic secret 4 lines of code of mythic legend that entire libraries have been written for like the STL, Boost and others? | |
|
|
|
| ResidentBiscuit (2215) | |
|
@Aramil, None of us are trying to "shut you down" or kill off any creativity you might have. We're just simply saying that what you claimed to have accomplished is, first of all, impossible in modern computer architecture, and second of all, you just didn't accomplish what you think you did. @Helios, That is some interesting results. How did a even get touched in all that? And did you happen to find out why b[12] is 12? | |
|
|
|