简体   繁体   中英

Catch c++ exception by allocating c memory error

I allocate a char pointer for receiving data. When the data is too large, I'll receive the Segmentation fault (core dumped). I try to use a try catch block to catch the exception to avoid this kind of error, but the same error still shows. How do I catch this kind of exception?

#include<iostream>
using namespace std;

void setmemory(char* p, int num)
{
    p=(char*)malloc(num);
}

void test(void)
{
    char* str = NULL;
    setmemory(str,1);
    try{
        strcpy(str,"hello");
        cout << "str:" << str << endl;
    } catch (...) {
        str = NULL;
        cout << "Exception occured!" << endl;
    }
 }

 int main()
 {
    test();
    return 0;
 }

You cannot catch segmentation fault through exception... Exceptions are not designed to catch signals. For these cases you need special handling like calling signal handler etc...

Although there is a way to convert these signals into exceptions in C++. Following link would illustrate on this topic:-

https://code.google.com/p/segvcatch/

Example given in this:-

try
{
    *(int*) 0 = 0;
}
catch (std::exception& e)
{
    std::cerr << "Exception catched : " << e.what() << std::endl;
}

Anyway after getting segmentation fault it's better to go for debugging rather than continue with current execution and later be transported to realm of undefined behavior.

Generally catching of Segmentation Fault is bad idea because you have no guaranty that any data in your program still valid. So you cannot just intercept SIGSEGV signal and do you work as usual.

See more details here: How to catch segmentation fault in Linux?

You cannot catch a Segmentation fault in C++. A seg fault is a result of memory corruption due to a bad operation run by your program. At that point, the operating system has taken control of your program. You don't want to keep a program running after a seg fault because the state is unstable. You as the programmer need to avoid seg faults.

If the code you posted is your actual code, even if you allocated enough space, your code will not work correctly.

 void setmemory(char *p, int num)
 {
     p=(char*)malloc(num);
 }

  int main()
  {
    char* str = NULL;
    setmemory(str,1);
    //...
    strcpy(str,"hello");

We can stop right there. You are attempting to copy "hello" to a NULL pointer. You are not setting str to the allocated space, even though the setmemory function was called.

The proper way to do this (I will use the malloc , even though I don't recommend it) would be:

 void setmemory(char *&p, int num)
 {
     p=(char*)malloc(num);
 }

You need to pass the pointer by reference, otherwise p acts as a temporary variable, so setting p within the function will not propagate back to the caller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM