Struct type initialization

Hi,

I am trying to understand structures and their initialization. I have a piece of code that looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct connection_t
{
public:
	bool		valid;		//< true if this structure is valid
	bool		server;		//< true if is server, otherwise is client
	int			socket;		//< socket in use
	int			socketType;	//< one of SOCK_STREAM or SOCK_DGRAM

	struct sockaddr_in	sock_from;
	int 				sock_fromlen;

	connection_t() :
			valid(false),
			server(false),
			socket(0),
			socketType(0),
			sock_fromlen(0)
	{
		memset(&sock_from, 0, sizeof(sock_from));
	}
};


This is in a .h file. I have a corresponding .cpp file that does has a function:
1
2
3
4
5
int 
server_init(int 			in_port,
			bool			in_useTCP,
			bool			in_nonBlocking,
			connection_t&	out_connection)


which calls

out_connection.sock_fromlen = sizeof(out_connection.sock_from);

at which point I get a seg fault.

I have had this happen with another struct of the same format. If I remove the structure variable on the left side, it is functional. It just seems to be setting equal to those structure elements. Is there something I am doing wrong? I am initialising the variable going into the function.

Thanks in advance.
sounds like you're passing a bad reference to server_init.

Be sure that 'out_connection' is valid. Specifically, maybe &out_connection is null.
I am doing (in a class function)

connection_t sockSSV;

and then:

if (0 > server_init(s_commsConfig.portSSV, s_commsConfig.useTCP,s_commsConfig.nonBlocking, sockSSV))

Do I need to do any more initialisation than that? This was where I thought the initialisation inside the structure would take care of it.

No that's right.

Okay so if that's not the problem.... are you sure sockaddr_in is a POD struct? Does it contain any complex types like string or vector or any other classes?
solved. there was a wrapping class above this one that was not initialized. Initialized the contained class but not the wrapping class.
Topic archived. No new replies allowed.