Visual Studio very strange "hello world" error

Pages: 12
Hi! I've just installed Visual Studio Community 2022 witch C++ package (not everything, just 4GB). Tried the welcome "Hello World" program and it worked then, to try the CPU and RAM profiler, i write this very simple code but it gives me the following error, what is happening??

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int TEST[1000000];

    for (int i = 0; i < 1000000; i++)
    {
        cout << "HELLO";
        TEST[i] = 0;
    }
}


ERROR
https://i.postimg.cc/F9t9kWfs/error-vs.jpg


SOLUTION:
The simple solution is to use "static int ARRAY [largeNumber];"
for more details read the thread.
Last edited on
> int TEST[1000000];
Don't put stupidly large arrays on the stack.

https://docs.microsoft.com/en-us/cpp/build/reference/stack-stack-allocations?view=msvc-170
Your default limit is 1MB.

Your array by itself is most likely 4MB (if int's are 4 bytes).

Either use a vector, or new[] to allocate large data structures from the heap.
You are exceeding the stack size.
Warning	C6262	Function uses '4000004' bytes of stack:  exceeds /analyze:stacksize '16384'.  Consider moving some data to heap.

Compile and run it in debug mode and *CRASH!* Compile and run in Release mode, runs fine.
Instantiate your array on the heap and nary a problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
   constexpr size_t SIZE { 1'000'000 };

   int* TEST { new int[SIZE] };

   for (int i { }; i < SIZE; ++i)
   {
      std::cout << "HELLO\t";
      TEST[i] = 0;
   }
}
First thank you all, ok i supposed it was this kind of problem but when i was using CodeBlocks i've never occurred in such an error, it was easier indeed, there is a way to use large arrays?

I think i'm not so bad at coding amateur things (i've coded a quite nice raycaster) but have no idea of some technicals: in Codeblocks i use MINGW.. maybe that compiler allows these types of thing? Is it more "elastic"?

PS sorry for my bad english
Last edited on
LakySimi1 wrote:
is there is a way to use large arrays?

1. Use a std::vector

OR

2. use the heap, new[] (see my example above)

I personally avoid regular arrays if at all possible. C++ code, use C++ containers.
Mmm... damn, you sure? I'm really used to use arrays.. why CodeBlocks allows me then??
Are you compiling code in C::B as 32-bit or 64-bit? Default for C::B is 32-bit. VS 2022 defaults to 64-bit.

The size of an int is different in 64-bit vs. 32-bit.

You should get used to using ALL of what C++ has to offer instead of relying on how C/early ANSI C++ does it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

int main()
{
   constexpr size_t SIZE { 1'000'000 };

   std::vector<int> TEST(SIZE);

   for (int i { }; i < SIZE; ++i)
   {
      std::cout << "HELLO\t";
      TEST[i] = 0;
   }
}

Other than a different way to instantiate a std::vector vs. a regular array, using a std::vector is very similar to using a regular array.

Passing a container to a function is a common task. Using a regular array requires passing its size as an additional parameter. Not required with a std::vector:
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>
#include <vector>

void arr_func(int[ ], int);
void vec_func(std::vector<int>&);

int main()
{
   constexpr size_t SIZE { 100 };

   // always initialize an array on creation
   int arr[SIZE] { };

   arr_func(arr, SIZE);

   // a vector is initialized automatically when size created
   std::vector<int> vec(SIZE);

   vec_func(vec);
}

void arr_func(int arr[ ], int SIZE)
{
   // an array passed to a function "forgets" its size,
   // so the size has to be passed as an additional parameter
   std::cout << "The size of the array is: " << SIZE << '\n';
}

void vec_func(std::vector<int>& vec)
{
   // no need to pass the size of a vector, it "remembers"
   std::cout << "The size of the vector is: " << vec.size() << '\n';
}
Yes, but in C::B, in one program, i used a single array of 400MB with no problem, i'm curious why i could do that in C::B.

Mm thanks for the advice, i don't know, i'm using C++ sure but i code little games just for fun, on my spare time, so it tooks a while for me to study C++ and honestly don't have time and patiance to learn new things, the one i know, which are pretty simple and straight farword, are more than enough for my projects :/

Are you sure is not fault of the compiler? In C::B i remember an option to NOT follow strictly the C++ rules, if i'm not wrong with some tricks i could even modify array size on the run!

..the real problem to me is that C::B doesn't have a dark mode! But i want to feel free of selling my games and honeslty don't know any other FREE for commercial use IDE (i know VS is for just one developer, my case)..



EDIT:
I will read it with more attention later but it looks more complicate than.. :(
 
int ARRAYOFNUMBERS [1000000];
Last edited on
M'ok, you want to do things your way even if they are not standard C++, and potentially dangerous. I won't stop you.
Yyyyes, i would say you are right, but, in my defense, the things i do are altogheder so simple and straight farward that the possibilities of some problem are really low, to be honest in my poorely optimized 5000 lines of the raycasting game i've never occurred is some strange behavior which was not fault of my algorithm or distraction (maybe luck you would say? maybe ahaha!).. just to be clear and let you know me: i don't use pointers, to say that the "bricks" i use are very simple indeed!

Btw thank you for responding!
> Yes, but in C::B, in one program, i used a single array of 400MB with no problem,
But is it on the stack?

And this is NOTHING to do with C::B vs Visual Studio.

What really matters is the compiler you're using and the OS that the resulting code runs on.

Your glossy editor is irrelevant.
In fact i supposed that! i suppose that is the compiler that builds the binary, it has the rules, it decide what create or not (accordedly with OS).

My C::B use MINGW, maybe i should try to set it up in VS..

EDIT:
Sorry don't get what a stack is: my array was like
 
int ARRAY [100000000];
Last edited on
the things i do are altogheder [sic] so simple and straight farward [sic] that the possibilities of some problem are really low

That is so wrong. Even simple, short bits of code can be potential bombs waiting to go off. Your code shows that.

PROPERLY using C++ will reduce the errors, not eliminate them. C++ has THOUSANDS of people testing and retesting the C++ libraries. People who are EXPERTS. You act as if you know better than they do.

A good programmer tries to write code that isn't buggy, a sloppy programmer doesn't care.

You should be HAPPY VS 2022 crashed your code. The VS compiler is very strict, unlike the default bundled C::B compiler.

I use VS 2019/2022, MSYS2 for command line editing/compiling and C:B. The C:B IDE was modified to use the MSYS2 compiler. That way I can use C++20 or C++latest when testing code, the bundled compiler with C:B is before C++20, the same as I can with VS or MSYS2.

You should strive to create the best damned code you can, that is portable and C++ standard compliant. To do otherwise is criminal.
I really understand you point of view, but coding is not my job, i don't study coding, is really just a passion that i coltivate time to time so have an array bigger than the one that is imposed by the standards is good to me; my real passion is game logic and algorithms.

I think that all those standards exist to help make a code really safe and stable in every condition, hardware, software, OS version, online applications, enviroment.. that's not my case, i simply don't need all those safe rules, mate, i do some 320x200 retro fps or turnbased rpg in 32bit and 40MB of ram which 35 of them are just for SFML's (for display a window and draw in it) libraries..
Last edited on
static int TEST[1000000];
> is a way to use large arrays?

Large arrays on the stack? Increase the stack size of the executable to a suitably high value.

from the command line: linker option /STACK:16777216 (for stack size up to 16 MB)
or from the IDE: Configuration Properties -> Linker -> System - Stack Reserve Size

More information: https://docs.microsoft.com/en-us/cpp/build/reference/stack-stack-allocations?view=msvc-170
To lastchance: it works! Thank you! A question: are not arrays static by "standard"?

To JLBorges: mm thanks! I found using "static" more flexible, experimenting can lead me to 2GB for a single array.. and i like experimenting ahaha!


PS is there a way to cite or tag someone in a post? :)
Last edited on
I found using "static" more flexible

Mmm, I think std::vector is rather more "flexible" - it can expand in size and copy as an object.


Dunno if this is of any interest:
https://craftofcoding.wordpress.com/2015/12/07/memory-in-c-the-stack-the-heap-and-static/
Last edited on
Pages: 12