Hello everyone. I'm trying to write a socket class for fun and I'm just wondering what the best solution to my problem would be...
Basically I'm trying to write the socket classes Read() function, but i want the user to be able to specify how many bytes to read (which will be parsed the recv()'s len parameter). So, to make it efficient, I want to dynamically allocate memory for an array the same size as the user specifies to read.
The problem is (I'm assuming) since its dynamically allocated (therefore the memory is allocated at run-time) I cannot compile it because the compiler says "Undeclared Identifier", etc.
Here's my code snippet:
Code:
char * Socket::Read(int sockfd, int len)
{
// Default read and buffer length/size if
// user does not specify read amount.
int bufSize = 2048;
if (len)
char * buf = new char[len+1];
else
char * buf = new char[bufSize];
if (!sockfd)
{
if (!len)
recv(this->sockfd, buf, bufSize, 0);
else
recv(this->sockfd, buf, len, 0);
}
else
{
if (!len)
recv(sockfd, buf, bufSize, 0);
else
recv(sockfd, buf, len, 0);
}
return buf;
}
Thank you for any help you can provide