Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I use a large buffer as a vector but not with new on Windows?

I'm using 64 bit Windows 7 Pro and Visual Studio 2010 Pro.

I'm trying to allocate and use a buffer that is bigger than 4 GB (for high data rate data capture).

Allocating and writing the buffer as a vector of bytes works fine. Allocating the buffer as an array of bytes works fine, but writing to that array crashes quickly. (The last message printed is "buffer allocated".)

Commenting out the vector section does not fix the problem.

The following is my test program:

#include <iostream>
#include <vector>
#include <BaseTsd.h>

using namespace std;

int main() {
  const ULONG64 BUF_SIZE = 4 * 1024ULL * 1024ULL * 1024ULL;

  {
    vector<unsigned __int8> v(BUF_SIZE);
    cout << "vector allocated" << endl;
    for (ULONG64 i = 0; i < BUF_SIZE; ++i) {
      v[i] = 0xff;
    }
    cout << "vector written" << endl;
  }

  {
    unsigned __int8* buffer = new unsigned __int8[BUF_SIZE];
    cout << "buffer allocated" << endl;
    for (ULONG64 i = 0; i < BUF_SIZE; ++i) {
      buffer[i] = 0xff;
    }
    cout << "buffer written" << endl;
    delete[] buffer;
  }

  return 0;
}

UPDATE: I believe this is a compiler bug. See here: http://connect.microsoft.com/VisualStudio/feedback/details/553756/invalid-check-for-maximum-array-size-in-x64-compiler-c2148

like image 710
Jim Hunziker Avatar asked Dec 20 '11 18:12

Jim Hunziker


People also ask

How do you know what size buffer to use?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.

Why is buffer overflow still a problem?

Buffer overflows can affect all types of software. They typically result from malformed inputs or failure to allocate enough space for the buffer. If the transaction overwrites executable code, it can cause the program to behave unpredictably and generate incorrect results, memory access errors, or crashes.

What types of programming languages are vulnerable to buffer overflows?

Assembly and C/C++ are popular programming languages that are vulnerable to buffer overflow, in part because they allow direct access to memory and are not strongly typed. C provides no built-in protection against accessing or overwriting data in any part of memory; more specifically, it does not check that data ...

What typically happens when a buffer is overflowed?

A buffer overflow, or buffer overrun, occurs when more data is put into a fixed-length buffer than the buffer can handle. The extra information, which has to go somewhere, can overflow into adjacent memory space, corrupting or overwriting the data held in that space.


1 Answers

I just tried compiling the given code with VS2010 Pro (64-bit version), and the compiler produced a C2148 error for the new call:

error C2148: total size of array must not exceed 0x7fffffff bytes

I compiled it from the command line after running vcvarsx86_amd64.bat. It seems that the limit given here is maybe somehow coming into play. Changing the new to [BUF_SIZE-1] allowed it to compile and run (although that is still larger than the 0x7fffffff number discussed in those links).

like image 127
Mark Wilkins Avatar answered Oct 04 '22 04:10

Mark Wilkins