Advertisement

pointer question

Started by December 23, 2002 11:17 AM
12 comments, last by zackriggle 21 years, 10 months ago
[EDIT ALL]
I need it this way because I do not know ahead of time the amount of memory needed to receive one whole message. I cannot receive just a part of one message, because that will require me to totally re-do just about everything...

[edited by - zackriggle on December 23, 2002 8:43:09 PM]
quote: Original post by micepick
char* ReadFromWinsock(...);  


What''s the problem with something like that? You can get around not knowing the length of the buffer by terminating it with a NULL character like a C-style string.

EDIT: I believe that strlen() returns the number of characters in a buffer until the first NULL character. So the length of a string in a wild pointer is undefined.


There could be a NULL character anywhere if you send anything but strings. If you''d receive this:
11010011 00000000 10110111 01011001
strlen would return 1. But you would have received 4 bytes, no only two (one char + zero character). So you''d have a data loss (Imagine receiving 100MB and having a null character at the beginning...)
[My Lousy Page | Kings Of Chaos | Vampires | [email=lordlethis@hotmail.com]email.me[/email]]
Advertisement
True. I suppose the only way around the buffer size problem is to have an argument that specifies the amount of data to read.
The reason I needed the char*& function (I REMEMBER NOW) is that I need to put an entire command in one char* . I could go about doing in any of 3 ways:<br><br>– 1. The way suggested [not preferable]<br>char * x = new char [GetSizeofNextMessage()];<br>x = ReadWinsock(socket);<br><br>— 2. i can do it the way I intended: —<br>char * x;<br>ReadWinsock(socket,&x);<br><br>— 3. i can do it through a struct, as previously mentioned (now sounding very good) —<br>struct PACKET<br>{<br> char* data;<br>}<br>// function prototype<br>PACKET ReadWSock(SOCKET sck);<br><br>–&gt; NOTE: <–<br>I think that I am going to use the struct approach. It gives me exactly what I need. I'll just not have the packet.data pointer initialized in the constructor, leaving me to KNOW that there is no memory allocated for it. The PACKETs will most likely &#111;nly be filled in by the ReadWSock() function.<br>–&gt; END NOTE <–<br><br><br>=== Note about how ~my~ messages are read in Winsock: ===<br>– There are 3 parts to a message<br>– 1 - A hex value (converted to ascii) containing the length of <br> the length of [you'll see] the data string<br>– 2 - An ?-hex long number (converted to ascii with itoa). <br> Length is specified by value #1<br>– 3 - The actual data string, X chars long [length specified by <br> #2]. This [#3] can be a char string, or a struct <br> converted to a char*<br><br>So, if I send "Hello World! This is Zach!"<br>it ends up being changed to:<br>"21AHello World! This is Zach!"<br>=========================================================<br><br><SPAN CLASS=editedby>[edited by - zackriggle &#111;n December 23, 2002 8:41:46 PM]</SPAN>

This topic is closed to new replies.

Advertisement