I get segmentation fault when a structure member points to another structure. In details, my new structure is
typedef struct _sock_t{
ir_socket_t *_socket;
bool flag;
extern_class obj;
double *vect;
};
while the structure I want to point to is ir_socket_t socket;
. In main()
, if I do something like
_sock_t *_sock ;
_sock->_socket = &socket;
I then get segmentation fault. However,
_sock_t _sock ;
_sock._socket = &socket;
works fine. What is wrong about the first approach.
P.S. Note that ir_socket_t
is C not C++ structure.
UPDATE: If I also consider a C++ class object as new a structure member, then I still get segmentation fault at
int n(4);
_sock->obj.resize(4); // here segmentation fault
while there is no problem with the latter approach (no pointer). This happens even if I do _sock_t *_sock = (_sock_t *)malloc(sizeof(_sock_t));
; using @Bathshebba answer.
What could be the issue please?
Note that _sock->vect= new double[n]
works fine.
Note also that _sock = new _sock_t;
works instead fine.