1

I am trying to execute following code which is giving me segmentation fault. What error I am doing or am I missing any part ? can this code be implemented in some other way?

Code:

#include <iostream>
#include "string.h"

using namespace std;

struct A {
    char* a;
};

int main()
{
    struct A *x;
    x->a = "soumya";
    char* str = "soumya";
    cout<<str<<endl<<(char*)x->a<<endl;
   // if(strcmp(x->a,str)!=0)
  //  {
   //     cout<<"not same"<<endl;
   // }

    return 0;
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
  • not a c++ guru but if I had to guess it looks like you're attempting to utilize a variable before initializing it (and I'm not sure that'd be the correct usage of the `struct` keyword inside of the code body) – Rogue Mar 25 '17 at 08:09
  • `struct A *x;` //uninitialized wild pointer `x->a = "soumya";` //invitation to disaster; accesses the address out of process space and ends up with segmentation fault issued by the OS `A *x = new A` and also delete it later using `delete A` to avoid memory leak. – vivekn Mar 25 '17 at 08:12
  • Possible duplicate of [Definitive List of Common Reasons for Segmentation Faults](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults) – Barmar Mar 25 '17 at 08:13
  • @soumya You can, and should, accept an answer it it provide you with the required guidance. – Jonas Apr 03 '17 at 08:36

3 Answers3

4

You are not initializing the x pointer to struct A so initially it points to some undefined area in memory which is not allocated.

So by doing x->a = "soumya"; you are trying to write this space which causes segmentation fault.

You can change

struct A *x;

to

struct A *x = malloc(sizeof(struct A));
// Don't forget to free this memory if your program is going to run for some time

or to

struct A x;

And replace every x->a with x.a in this case memory is allocated in stack so it will automatically be freed at the end of the method.

8bra1nz
  • 717
  • 7
  • 19
2

There are several issues we need to cover.

1) do not use raw pointers in C++ anymore, in general.

2) you do not actually allocate any memory for *x. This can be achieved by doing:

struct A *x = new A;

Remember to call delete x, to deallocate the memory when appropriate.

The third problem is that you do not allocate memory for the content of x->a. The best solution is to use std::string instead of a char pointer. Then you are allowed to do x->a = "soumya";.

You struct should then be

struct A {
    std::string a;
};
Jonas
  • 6,915
  • 8
  • 35
  • 53
1

Here is a corrected version :

Use of const char * because you assign constant string

Use of struct A x; to have the struct allocated and not only a pointer to a non allocated memory.

#include <iostream>
#include "string.h"
using namespace std;
struct A {
    const char* a;
};
int main()
{
    struct A x;
    x.a = "soumya";
    const char* str = "soumya";
    cout<<str<<endl<<x.a<<endl;

    return 0;
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
contremaitre
  • 106
  • 1
  • 2
  • 9