C ++ Creating SIGSEGV for Debugging Purposes

I am working on an open class of shared variables, and I want to be able to generate a SIGSEGV error to see if my implementation works as I planned. I tried to create a function that changes the pointer and reads it 100 times. Then I call this function on both threads, and the threads execute endlessly inside my program. This does not create the error I want. How should I do it?

edit I do not process segfaults at all, but they are generated in my program if I remove locks. I want to use a non-blocking construct, so I created a generic class of variables that uses CAS to stay locked. Is there a way that I can have a piece of code that will generate segfaults so that I can use my class to verify that it fixes the problem?

+4
source share
4 answers
#include <signal.h> raise(SIGSEGV); 

Will trigger an appropriate signal.

+9
source

malloc + mprotect + dereference pointer

There is an example in this mprotect page.

+5
source

A pointer to defragment to unallocated memory (at least on my system):

 int *a; *a = 0; 
+2
source

Split invalid pointer:

 *((int*)0x8100000000000000) = 5; 
+1
source

Source: https://habr.com/ru/post/1398509/


All Articles