0

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;
 }
user3738211
  • 49
  • 2
  • 7
  • 3
    If you are indeed using `C++` and not `C`, stop using `malloc` and `char*` and start using `string`. – Cory Kramer Nov 12 '14 at 15:32
  • 1
    Your set memory function does NOT update the pointer you pass to it in main. You need to pass `char*& p` – Neil Kirk Nov 12 '14 at 15:33
  • Use `char *strncpy( char *dest, const char *src, std::size_t count );` to define how many caracters you want to copy (based on the malloc you made) – NSimon Nov 12 '14 at 15:35
  • 2
    If you need to change what a pointer points to in a function, you need a pointer to a pointer or a reference to a pointer. Otherwise, your assignment to `p` in `setmemory` will not affect the caller. – crashmstr Nov 12 '14 at 15:36
  • 1
    You can't really hope to recover from this. Give up at this point and terminate the process. – David Heffernan Nov 12 '14 at 15:37
  • There are multiple things wrong with your code. If you debugged it, you would see right away that your pointer `str` is not set after the call to `setmemory`. The try/catch is another issue, but this occurs after the obvious error with `str`. So are you debugging your code? – PaulMcKenzie Nov 12 '14 at 15:38
  • In theory, if this worked, upon catching your exception you would still need to free the memory pointed to by str, you can't just leak it. – Neil Kirk Nov 12 '14 at 15:41

4 Answers4

2

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.

ravi
  • 10,994
  • 1
  • 18
  • 36
  • Would you please give me an example like calling signal handler? – user3738211 Nov 12 '14 at 15:36
  • 2
    @user3738211 This is the kind of error which shouldn't occur during a run of your program. There is no point recovering from it. You need to fix your code so that it doesn't happen in the first place. – Neil Kirk Nov 12 '14 at 15:40
2

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?

Community
  • 1
  • 1
1

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.

stewartbracken
  • 124
  • 1
  • 1
  • 7
0

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.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45