I'm trying to write ARGB color data into a uint32_t variable through a for loop. I get an error:
invalid write to m_imagedata[i][1],wriable range is 0 to 0
if (!render){
uint32_t* m_imagedata = nullptr;
}
else{
m_imagedata = new uint32_t(m_viewportheight * m_viewportwidth);
}
for (uint32_t i = 0; i < m_viewportheight * m_viewportwidth; i++) {
m_imagedata[i] = 0xff00ffff;
How can I fix this?
You have the syntax for dynamically allocating an array wrong. It should be
and then you'd have to delete it using the
delete[]form ofdeletei.e.However, as others have noted in comments if what you want is an array that can be dynamically sized at runtime, you should use the one that is included in the C++ standard library:
std::vector<T>. This way you do not need a loop to initialize each item to some value and you don't have to worry about deleting it yourself. If yourm_imagedatamember variable is astd::vector<uint32_t>you can initialize it in the constructor of your class like: