0

I have a very simple program that I don't know why a crash occurs on exit. My main function is a simple cout<<"Hello world";

But I've added another cpp file:

system_information.cpp:

system_information::system_information() { threadid_processid_map[22]=23; processor_processid_map[128] = { 0L }; processor_threadid_map[128] = { 0L }; } 

And the header file looks like:

system_information.h

class system_information{ public: DWORD processor_processid_map[128]; DWORD processor_threadid_map[128]; unordered_map<DWORD, DWORD> threadid_processid_map; system_information(); }; 

And I simply have this file to declare in instance of my class:

parse.cpp:

#include "system_information.h" system_information sys_info; 

My program crashes on exit on crt0at.c at this line:

onexitbegin_new = (_PVFV *) DecodePointer(__onexitbegin); 

What am I doing wrong?

6
  • 6
    processor_processid_map[128] = { 0L }; invokes undefined behavior by writing past the bounds of the array Commented Mar 21, 2018 at 22:38
  • So why does it cause a crash? Does any memory leak cause a crash? Commented Mar 21, 2018 at 22:40
  • 1
    undefined behavior means anything can happen - including crashes (also you don't have a memory leak in this code) Commented Mar 21, 2018 at 22:42
  • This might be a good time to learn to use your debugger. A debug build in VS might have warned you that you'd just corrupted the stack by writing past the end of your arrays. Commented Mar 21, 2018 at 22:48
  • 1
    It might be difficult to pinpoint the problem without a debugger. My guess is that the out of bounds assignment has changed the __onexitbegin pointer. __onexitbegin and _onexitend stores the range of functions needed to be called while exiting, and the system would have crashed on calling the exitbegin. Commented Mar 22, 2018 at 6:34

1 Answer 1

3

I'd say you're slightly confused about what:

processor_processid_map[128] = { 0L }; 

is actually doing. The fact that you have braces in there seems to indicate you think it will set all values in the array to zero but that is not the case. You can initialise the array that way but assigning to it is a different matter.

What that code is doing is trying to set element number 128 to zero and, since elements are restricted to the range 0..127, what you have there is undefined behaviour.

If you do wish to zero out the entire array, there are a number of ways to do this, including:

// Simple loop. for (int i = 0; i < sizeof(id_map) / sizeof(*id_map); ++i) id_map[i] = 0; // Setting memory. #include <cstring> memset(id_map, 0, sizeof(id_map)); // Fill algorithm. #include <algorithm> std::fill(id_map, id_map + sizeof(id_map) / sizeof(*id_map), 0); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.