Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gimp

Easy memory question

This topic is 6565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m pretty sure I''ve lot the plot. I wrote this code expecting to get a NULL then four character string, I''m thinking however that i''ve got it wrong...
    
	byte *message = new byte[5];
	message[0]=NULL;
	memcpy (message+1,"QUIT",4);
    
Anyone wish to slap me with a dead fish? thanks gimp

Share this post


Link to post
Share on other sites
Advertisement
Actually, you have a NULL and just four characters.
If you want a string, you need one byte more - for the terminating NULL character.
    
byte *message = new byte[6];
message[0]=NULL;
message[5]=NULL;
memcpy( message+1, "QUIT", 4 );

// or another version:


byte *message = new byte[6];
message[0]=NULL;
strcpy( message+1, "QUIT" );

Share this post


Link to post
Share on other sites
Neither do I, lets just settle for the "because he can" response.

I''ll back Serge K''s response, although he didn''t release his memory after he''d done his copying! what a bad person he is

Share this post


Link to post
Share on other sites
faster way:

byte *message = new byte[6];
Zeromemory(message,sizeof(message));
strcpy(message+1,"QUIT");



Edited by - Xeno on July 25, 2000 5:45:13 PM

Share this post


Link to post
Share on other sites
quote:
Original post by gimp

I'm pretty sure I've lot the plot. I wrote this code expecting to get a NULL then four character string, I'm thinking however that i've got it wrong...

byte *message = new byte[5];
message[0]=NULL;
memcpy (message+1,"QUIT",4);

Anyone wish to slap me with a dead fish?

thanks

gimp



I'm really not sure what you're trying to get to but you are getting a null the four characters... however you do not have a string, as It has been pointed out, you're missing the null terminator; anyway, if you pass that pointer to any function that's supposed to take a string, it will only read an empty string...
Oh, by the way, you can do something like this (3rd version)
        
byte *message = new byte[6];/*null, four chars, then null again*/
message[0]=NULL;
memcpy (message+1,"QUIT",5);




And if I have to re-edit this post again, I'm gonna punch the monitor! (dang quote/source confusion thingie)

Edited by - alexmoura on July 25, 2000 6:12:37 PM

Share this post


Link to post
Share on other sites
Sorry, I should have explained myself. I''m forming messages to pass through my pluggable factory. The factory will be able to accept both enumerated messages and string messages. To accept string messages I make the first value NULL (Where the message ID would normally be).

The only reason I asked this question is that my map.find() seemed to be failing and I was thinking that it was because I was getting the data assignment wrong.

I dont bother with a trailing null as it''s really not needed (the find can compare structures of binary data so should be able to do unterminated strings as well...

As it turned out I was incorrectly creating the registration token by not prepending it with the leading NULL.

byte szTokenSHUTDOWN[] = "\0QUIT";

Problem solved. Thanks and sorry to confuse you all...

gimp

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!