Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Problems with MultiSync - NetGetString

Author
Message
Hayer
19
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 1st Jun 2008 21:53

This just jumps off with the error


Could u explain how to get "strings"(char* 'es?) in Dark GDK Benjamin? ( Or anyone! )

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 1st Jun 2008 23:01


If NetGetString(Msg, 5) is supposed to put an acquired string at Msg, then that's an error. Msg either needs to point to allocated space

char *Msg = new char [25];

or needs to be declared as a buffer, not a pointer.

char Msg [25];

If you do the former, you need to remember to delete the allocated space when you're done with it, i.e., before your function ends.

The way you're doing it the function appears to be putting the string in some unidentified space, likely attempting to overwrite some other code or data.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Hayer
19
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 2nd Jun 2008 11:35 Edited at: 2nd Jun 2008 11:41
okeey, so how do I "delete" the Msg[25]?


And btw;
How do I do it when I dont know the size of the string?

Cause:
unsigned long Size = NetGetMessageSize();
char Msg[Size];
NetGetString(Msg,Size);

Makes this fat error
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'msg' : unknown size



dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 2nd Jun 2008 12:10 Edited at: 3rd Jun 2008 04:19
For a variable array size you need to dynamically allocate this char array, i.e. 'char* myString = new char[howeverMany];', To deallocate this you simply use 'delete [] myString', this isn't required for statically allocated arrays like you're doing, but for dynamically allocated arrays it is at some point, unless you want to waste RAM and potentially run out.

It may be best to just create a single string that's visible in all the network receiving code which is of a larger size than any strings you will be sent, this will cut out on lines of code at least, and you wouldn't have to allocate space all the time.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 2nd Jun 2008 18:55
Okay:

First, you have to declare a buffer with a known size at compile time. You cannot declare a buffer using a variable whose value is determined dynamically while the program is running. That's one of the reason for doing it as

char *Msg = new char [Size];

This basically says that Msg is a pointer to a character, although it will be to a series of characters once the new command is executed. If you just declare

char *Msg;

it's simply a pointer without an assigned address to point to. By using the new command you're asking the operating system to provide some amount of space for a number (Size) of data elements of the size of the type (char) you're specifying. The new command returns a pointer to the memory location of the space that the OS has provided which is then assigned to the pointer (Msg) you're using to reference it.

Several things to keep in mind. You don't ever want to change the pointer value stored in Msg until you've let the system reclaim the memory space allocated (when you're finished with it) using the delete command with

delete [] Msg;

If you're going to traverse the string with a pointer, assign another pointer the same address of the beginning of the string that's found in Msg (for example.)

Now, as for using a statically declared buffer, there's nothing that says that it has to be the same size as the string it's receiving. However, it does need to be at least as large as the string. So create it with a size larger than any known string you expect to find in it and just let it take up the space.

Also remember that character strings typically are terminated with a character of value zero. Any size of a string you request is going to have to allocate enough space for the string characters and the zero terminator.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-11-20 11:44:02
Your offset time is: 2024-11-20 11:44:02