I am trying to initialise these two character arrays inside a constructor. The error is with the character arrays buf and clientip, the others are fine. I have added the code of the entire constructor below:
class server : public udp {
public:
server() {
clientLength = 0;
buf[1024] = {};
bytesIn = 0;
count = 0;
clientIp[256] = {};
portnumber = 0;
}
private:
int clientLength = sizeof(client);
char buf[1024];
int bytesIn;
int count = 0;
char clientIp[256];
unsigned short portnumber = 70000;
The warning is that:
*(error) Array 'buf[1024]' accessed at index 1024, which is out of bounds. [arrayIndexOutOfBounds]
(error) Array 'clientIp[256]' accessed at index 256, which is out of bounds. [arrayIndexOutOfBounds]*
How can I solve this problem?
buf[1024] = {};in your constructor is accessing element 1024, which is out of bounds. Remove the [1024]. Better yet, move all of that to the initialization section of the constructor, or assign the default values in the private section.