Advertisement

Easy memory question

Started by July 24, 2000 08:40 PM
5 comments, last by gimp 24 years, 5 months ago
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
Chris Brodie
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" );    
Advertisement
I dont get why you set message[0]=NULL

??



ByteMe95::~ByteMe95()
ByteMe95::~ByteMe95()My S(h)ite
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
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

------------------------------- Goblineye Entertainment------------------------------

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
Advertisement
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
Chris Brodie

This topic is closed to new replies.

Advertisement