hey guys I read somewhere either this site or a c++ that you cannot resize an array once you created it but in this code below I was able to resize the array with a function,what do they mean by you can't resize an array even though I'm able to with this function?
Line 12 is not legal C++, although some compilers do allow it as a non-standard extension.
In standard compliant C++, array dimensions must be constants known at compile time.
What you are doing in resizeA is undefined behavior. If what the user entered for rsize is >= 10, it will work. If what the user entered for rsize is < 10, you're going to be writing out of bounds.
arrays can not be variable sized, the C++ standard doesn't allow that.
TDM-GCC 4.9.2 does compile your code, but with the warning levels I have set the compiler does complain:
9 17 D:\Programming\Projects\Test\main-cpp.cpp [Warning] ISO C++ forbids variable length array 'ray' [-Wvla]
MSVC++ 2015 Community refuses to compile your code, throwing up two related errors:
Error (active) expression must have a constant value ConsoleApplication1 c:\Programming\My Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 9
Error C2131 expression did not evaluate to a constant ConsoleApplication1 c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp 9
You didn't actually resize the array, you simply accessed "out of bounds" elements that weren't a part of your array. That is why you had the program crash with large inputs to your function.
On a side note: accessing your array without specifying any values after creating your array, as you do with lines-13-16, pulls up garbage values.
If what the user entered for rsize is >= 10, it will work. If what the user entered for rsize is < 10, you're going to be writing out of bounds.
how come this happens? but also how come when I enter 2 and I choose the size of the parameter as 10 it will work ?BUT it only prints 9 values instead of 10 :s
rsizeA(ray,10);
also why would 10 be the limit? before it goes out of bounds I thought it would be 2 since thats what I typed in?
how come if it goes over 10 it will be out of bounds?
That's not what I said. What I said was if what the user entered for rsize was >= 10, then reasizeA will work because you have at least 10 elements in the array.
just say if I type 2 in for rsize wouldn't it be out of bounds if its greater than 2?
Correct. As DrZoidberg said, if the user enters 2, you have two elements [0] and [1]. Any reference to any other element is out of bounds.
Let's say the user entered 2 for rsize. How much memory is allocated? Two ints, right?
ray[0] and ray[1].
Now you call resizeA with a count of 10. resizeA can write into ray[0] and ray[1], no problem.
But your loop counter is 10, so your loop now tries to write into ray[2]. Where is ray[2]? It was never allocated, so you get an out of bounds trap or a program crash.
Simply because you reference an out of bounds element does not change the size of the array.
Thanks Anon I really appreciate the help sorry if I'm been frustraiting or anything It just seems like such a hard concept to grasp
I understand that when I entered 2 for rsize the compiler will set aside enough memory for 2 ints.
but the thing is I thought when I go over 2 in the function parameter(enterSize) I would get an error yet even if I put three or four even five anything 10 or below the function will work
I thought that when I did this just say I put 10 as the second parameter it would crash because the for loop continues until it reaches the size of my second parameter(enterSize) right?
(yet when I put 10 in the program runs and prints out 9 0-8 not 10 ints)
I hope I'm making sense sorry if i'm not,thanks for helping me with this really appreciate it
so cire your saying it's happening because of undefined behavior? but there has to be an explanation why each time I run the program and I type 0,1,2 or 3 in for rsize it prints
It sounds like you're trying to derive rules which define what happens when the program produces undefined behaviour. There's a contradiction there. If you could generate such rules, it wouldn't be undefined.
Of course you could examine the assembler instructions generated, and examine the contents of memory and explain precisely what is going on in a particular instance. But that doesn't give you anything which you can depend upon. Use a different compiler or a different computer and something completely different might happen.