문제

I am doing a visual c++ appliction and trying to allocate size to a buffer(that buffer is further used to store the contents of a stream). There is no problem if the size declared to buffer is less then

const int size= 319000; //here there is no problem

but for accessing some data of my desire from stream i need to declare the buffer of size like this-

const int size=4348928;//this size cause the problem
char buffer[size+1];
HRESULT hr = pStream->Read(buffer, size, &cbRead );

Although the last two line of the code has no role to play with my problem it is just to give you idea that what exactly i am doing with this buffer's size.

But when i declare this size it does nothing (i mean my visual application function like this: if you click a file it generate a stream and i am storing that stream in a buffer- and if i declare the size of the order of 319000 the programs run fine and when increase the size to 4348928 it even don't work- and of course there is no error )

도움이 되었습니까?

해결책

If buffer is a local variable, then you try to allocate the array on the stack. The stack is normally in the low megabyte range (as in one to four). You try to allocate over four megabytes, which will not work.

The easy way to solve this is to allocate it dynamically off the heap:

char* buffer = new char[size + 1];

// Do operations on `buffer`

delete[] buffer;

다른 팁

This happens to you because static data are stored in stack and its size is a few MB. If you allocate memory dynamically your data goes into heap and it is much bigger.

In your case I would use containers. Probably vector.

std::vector<char> buffer(size);

Containers are safer then pointers and much safer then dynamically allocations because containers auto delete stuff if you don't use it anymore. Also you can always safely increase size of container just pushing another value.

If you need to pass it to function you can pass reference (it is probably the best option), however you can pass pointer to first element by doing this: &buffer[0] (this works only with vector).

What is more you can iterate through all containers by getting pointer to data.

for (std::vector::iterator it = buffer.begin(); it != buffer.end(); ++it)
{/*some stuff*/}

Iterations looks the same with any type of container.

What is more, you allocate 4348928 chars, it is about 4MB. But if you need to allocate lets say 300MB allocation with new usually fails (the same with vector). For such a big allocations you should use std::deque.

Use Pointer as the local function memory is allocated from stack and it will not be able to get that much fetch.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top