Can I use poll() when pollfd is a vector instead of array?

Is it okay for me to use poll() with a pollfd vector?
Yes. To get a pollfd pointer you can do &vec[0] on a non-empty vector or vec.data() in C++11.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void* connectionThread (void *){
	displayMsg("SERVER", "Now accepting connections...");
	vector<pollfd> poll_sockets;
	pollfd * poll_sockets_ptr;
	poll_sockets[0].fd = master_socket;
	poll_sockets[0].events = POLLIN;
	poll_sockets_ptr = &poll_sockets[0];
	while(server_online){
		int poll_activity;
		int num_in_poll_sockets = poll_sockets.size();
		poll_activity = poll(poll_sockets_ptr, num_in_poll_sockets, 0);

		if (poll_activity < 0){
			errorMsg("Could not poll!");
		}
		for(int i = 0; i<num_in_poll_sockets;i++){
			if (poll_sockets[i].revents & POLLIN) {
				displayMsg("CONNECTION_THREAD", "Someone connected!");
			}
		}

	}
	displayMsg("SERVER", "Closed: Connection Thread!");
return 0;
}


Like so?

It builds with no error, but hits a run-time error and terminates the program when hitting poll().
poll_sockets is an empty vector upon initialization. I should've used push_back!
Topic archived. No new replies allowed.