array resizing

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
   #include <iostream>

using namespace std;

int resizeA(int y[],int enterSize);

int main()
{
    int rSize;
    cin >> rSize;
    int ray[rSize];
    for(int x = 0;x<rSize;x++){

        cout << ray[x] << endl;
    }

    resizeA(ray,10);

    for(int x = 0;x<rSize;x++){

        cout << ray[x] << endl;
    }

}
int resizeA(int y[],int enterSize){

   for(int x = 0;x<enterSize;x++){

         y[x] = x;

   }
}


on a side not if anyone can help if I change the function to resizeA(ray,5);

it only prints out two ints instead of 5?

also when I use resizeA(ray,10); it only prints out 9 ints

and when I use resizeA(ray,20); the program crashes???

how come this is happening ?
Last edited on
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.
closed account (E0p9LyTq)
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.

Arrays - http://en.cppreference.com/w/cpp/language/array
Thanks Anon and FurryGuy



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?
Last edited on
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.


also how come if it goes over 10 it will be out of bounds? just say if I type 2 in for rsize wouldn't it be out of bounds if its greater than 2?
If you declare an array as [2] you get 2 elements - at [0] and [1]
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.



Last edited on
Thanks anon but I'm still not too sure about it

I mean how come if what the user entered for rsize was < 10 it would crash(or out of bounds)?

I mean like if I enter 2 as rsize it will print two elements but when I change the function to this

resizeA(ray,11);

it crashes while anytime the second parameter is 10 or below it does not crash even when I specify rsize to be only 2 or 3 elements ?
Last edited on
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
Last edited on
I also don't get why

when I enter 0,1,2,3 for rsize I get the output

0
1
2
3
4
5
6
7
8

(just does not make sense!!!)

yet when I type 4 I get the output
0
1
2
3

or 5

0
1
2
3
4

You might want to revisit this thread:
http://www.cplusplus.com/forum/beginner/192489/
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

0
1
2
3
4
5
6
7
8

yet when I type 4 or 5
it prints
0
1
2
3
4
and

0
1
2
3
4
5

respectively
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.
thanks guys
Topic archived. No new replies allowed.