-1

I have a char pointer & have used malloc like

 char *message;
 message=(char *)malloc(4000*sizeof(char));

later I'm receiving data from socket in message what happens if data exceeds 4000 bytes ?

Asna
  • 108
  • 8
  • 4
    May I ask why you are using `malloc` in C++? Also, `sizeof(char)` is specified to always be `1`. – Some programmer dude Sep 18 '15 at 05:45
  • what alternate do I have should I use it like char* message = new char[4000]; – Asna Sep 18 '15 at 05:48
  • but my question what if data assigned to this char* exceeds 4000 ? would the size be readjusted like in arrays – Asna Sep 18 '15 at 05:51
  • 3
    Oh the answer below answers your question, I'm just trying to knock some bad habits out of you. ;) A good hint that you're doing something you should not be doing is using a C-style cast (or even a `reinterpret_cast`). And remember that `malloc` just allocated memory, it doesn't construct objects and so can't be used to allocate other objects. And lastly, if you have a fixed-size buffer, you should not allocate it dynamically at all, instead use e.g. [`std::array`](http://en.cppreference.com/w/cpp/container/array), in modern C++ there is seldom any use of pointer outside of C-compatibility. – Some programmer dude Sep 18 '15 at 05:55
  • Thanks this is very helpful – Asna Sep 18 '15 at 05:58
  • Are you talking about TCP, UDP, or some other protocol? – David Schwartz Sep 18 '15 at 06:38

4 Answers4

1

I'll assume you are asking what will happen if you do something like this:

recv(socket,message,5000,0);

and the amount of data read is greater than 4000.

This will be undefined behavior, so you need to make sure that it can't happen. Whenever you read from a socket, you should be able to specify the maximum number of characters to read.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • What would be undefined behavior? There's nothing wrong with receiving a message that happens to be larger than some buffer. – David Schwartz Sep 18 '15 at 06:38
  • @DavidSchwartz - Just a guess, in such case we'll have to do multiple read on the socket until there is nothing to be read any more. Am I correct. ? – anurag-jain Sep 18 '15 at 06:40
  • @a4anurag That might be true for stream protocols like TCP. It's definitely not true for datagram protocols like UDP. But that's not what the question asked. – David Schwartz Sep 18 '15 at 06:41
  • @DavidSchwartz: Thanks. I made an assumption, which I originally didn't have in my answer, so I've added the assumption. I think you may have misread the question though. It states "I'm receiving data from socket in message", not "I'm receiving a message". The variable name is "message". – Vaughn Cato Sep 18 '15 at 12:58
1

Your question leaves out many details about the network protocol, see the answer by @DavidSchwartz.

But focussing on the buffer in which you store it: if you try to write more than 4K chars into the memory allocated by message, your program could crash.

If you test for the size of the message being received, you could do realloc:

int buf_len = 4000;
char *message;
message = static_cast<char*>(malloc(buf_len));

/* read message, and after you have read 4000 chars, do */
buf_len *= 2;
message = static_cast<char*>(realloc(message, buf_len));

/* rinse and repeat if buffer is still too small */

free(message); // don't forget to clean-up

But this is very labor-intensive. Just use a std::string

int buf_len = 4000;
std::string message;
message.reserve(buf_len); // allocate 4K to save on repeated allocations
/* read message, std::string will automatically expand, no worries! */
// destructor will automatically clean-up! 
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • @DavidSchwartz If he tries to write more than 4K into the message, he will have a memory violation, won't he? – TemplateRex Sep 18 '15 at 06:39
  • The question he asked is what happens if he receives a large message. If, for example, he's talking about TCP, none of your answer makes any sense. – David Schwartz Sep 18 '15 at 06:40
  • @DavidSchwartz I'm sure you are right that it depends on the network protocol. But the only code given is about a fixed-size `char` buffer and what happens if more data is being received. It could be stdin that produces this data. I interpreted the question as on how to handle memory issues when trying to store it into `message` – TemplateRex Sep 18 '15 at 06:47
  • But he didn't ask that. He asked what happens if the data on the socket exceeds the buffer size. In the case of TCP, nothing unusual at all -- you can't even tell that more data was received until the next time you call the receive function. And UDP typically truncates. – David Schwartz Sep 18 '15 at 06:48
1

It depends on a few factors. Assuming there's no bug in your code, it will depend on the protocol you're using.

If TCP, you will never get more bytes than you asked for. You'll get more of the data the next time you call the receive function.

If UDP, you may get truncation, you may get an error (like MSG_TRUNC). This depends on the specifics of your platform and how you're invoking a receive function. I know of no platform that will save part of a datagram for your next invocation of a receive function.

Of course, if there's a bug in your code and you actually overflow the buffer, very bad things can happen. So make sure you pass only sane values to whatever receive function you're using.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

For the best result,you get a segmentation fault error

see What is a segmentation fault? dangers of heap overflows?

Community
  • 1
  • 1
chenjun
  • 301
  • 3
  • 10