2

My code:

void RandomBuffer(ByteVector& out, size_t size)
{
    try 
    {
        out.resize(size);
        memcpy(&out[0], (void*)memcpy, size);
    }
    catch (...)
    {
        return;
    }   
}

I want to generate some kind of random buffer (in fast way and like a random buffer). So I used the code provided. For small buffers it works fine, but I had some big buffer 334692352 bytes and it failed for that.

After that I tried to use try-catch but anyway I get an exception and that is Access violation reading location

I want to ask, why this exception is not caught by the catch.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
ST3
  • 8,826
  • 3
  • 68
  • 92

2 Answers2

1

It's not an exception, it's memory access fault catched by OS or protected mode in CPU.

Are you sure you can directly access the memory of ByteVector? Check this link:

Memcpy of native array to managed array in C++ CLI

Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35
1

Depending on compiler and OS, access violation can be raised as an exception, caught by the OS or the CPU. If the OS is catching it and killing the process, there is not much you can do (but prevent access violation by checking your boundaries and memcpy)

Bruce
  • 7,094
  • 1
  • 25
  • 42