Sunday, May 18, 2025

About Segmentation fault in Linux

 in linux segmentation fault is :-

-------------------------------------------------

 

accessing unauthorized memory for "reading" or "writing" is segmentation fault.

For example :-

let you have created a class 

class ABCD

{

    // here you have created a pointer variable as :-

    MYSQL *mysql;

}; 

Now in somewhere your project you used this pointer without initialized it, somethig like this :-

 void ABCD::myfunction-abcd()

{

....................

....................

mysql->...................... // or something like that 

/*

 then in above line you will get "segmentation fault" error in console and your program will be terminated immediately. Because when we, create object, program will give you a memory allocated for your object. And above pointer will be allocated a memory. But here program or compiler will not initialize it by default, it's your responsibility to initialize it. Now because of that allocated memory for above pointer will have garbage value. And in this stage pointer is called "dangling pointer". And because of that pointer will point to any location in memory, so "reading" or "writing" by this pointer is "segmentation fault", because this pointer is pointing to unauthorized memory. and that's all.................

*/

}

No comments:

Post a Comment