This is invalid C++. And since it is allocated on the stack, which has a limited size, it starts to crash after a certain size, as you noticed. Instead, you should use std::vector.sf::Uint8 pixels[W*H*4];
Also, I've experimented an isolated sf::Uint8 array, and memory is not the problem, since I've tested declarating such an array with 2000*2000*4 elements, and it works fine.Depends where you declare it.
btw, that statement is not illegal since i've used it in the app i'm developing and it worked fine up to 716*716*4.It is illegal. Some compilers allow it (as a non-standard extension), mostly because it is valid in C. Compile with the -pedantic flag (gcc) and you'll see the difference.
btw, that statement is not illegal since i've used it
...
This is why you should always build your code with multiple compilers on multiple platforms with high warning levels enabled (and run other tools (like clang tidy, cppcheck - and others)) to be (reasonably) sure that your code is standard compliant and portable.
I recommend always building all commits with gcc, clang, VS (and sometimes icc) on (at least) Linux and Windows, with (at least) "-Wall -Wextra" (or equivalent "/W4"). More is better... It catches real bugs and keeps your code working broadly :-) and it can be completely automated to happen in the background every time you do a "git commit" :-)
This is what I do myself - and it's worth it in the long run spending a few hours to set up a few virtual machines and a CI system integrated with you DVCS ...
So, basically, it is always best to use a std::vector instead of array, right?in most cases the answer is yes vector is prefered over array. so if the image data is fairly small like couple of KB, yeah it is safe to use array and you are already know the limit in your machine if you go with array. but for C++ standard, actually i couldn't find reference that support it is illegal. but after searching, it turns out the standard doesn't guarantee the array will be created in stack. because, it is dependant on implementation, and according to wikipedia (https://en.wikipedia.org/wiki/Data_segment) there are few cases here to implement array with big size like so:
I guess that could be translated to reality as "No-one uses arrays of pixels, just vectors". it is very important to know what are the current standard ways to do stuff. For instance, i first met arrays and then vectors, but as soon as i met vectors i abandoned arrays, as vectors would do the same as arrays and they are more dynamic. so now even for fixed arrays, i replace arrays for vectors.
Another question... As some funny pic states, programmers say "It doesnt work. Why? / It works. Why?"well, i think we need a computer hardware engineer to answer it :-\
So why doesnt an array of pixels work?
So, basically, it is always best to use a std::vector instead of array, right?No. you must first identify what you need, and then choose the best data structure accordingly. Here, you need heap allocation (because the stack is too small to hold your big array). Heap allocation is done with the new/delete keywords in C++, but this is too unsafe and low-level, so the standard library offers a std::vector wrapper that is safe and easy to use.
So why doesnt an array of pixels work?As already stated, the stack has a limited size, you can't allocated tons of MB on it. If this is not clear for you, you can read more about stack/heap storage. This is an important concept, especially in C++ where you directly have to deal with these two kind of storage.
what do u mean by 'safe'? i guess you mean std::vector is still stored in the stack, and it is still safer than an array stored in the heap.An std::vector is stored on the stack but the actual data is stored on the heap. Using an std::vector is much safer when compared to using the heap manually.
1 more question: most of the time, a function that takes an array or an array pointer can also take a vector or a vector pointer?No, because std::vector, std::array and c-style arrays are all different types. As with before, though, you can pass the data part of an std::vector or std::array as if it was an array.