• 0

C++ strcpy [SIMPLE]


Question

#define EXIT_PASSPHRASE "exit"

...

//This would work instead of the next to lines but its depreciated
//char *exit_passphrase_ = EXIT_PASSPHRASE; //Works

char *exit_passphrase_ = new char(bufsize);
strcpy(exit_passphrase_, EXIT_PASSPHRASE); // THE PROBLEM IS HERE

...

While(strncmp(buffer, exit_passphrase_, bufsize) != 0){ //working

}

Why is my strcpy not setting the exit_passphrase_ to the EXIT_PASSPHRASE? What am i doing wrong?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

It should be noted that the code you're writing isn't really C++ except calling 'new'. You could just as easily replace that with malloc, and you'd be writing pure C.

char *phrase = malloc ( bufsize ); /* allocate memory on the heap */
char phrase [bufsize]; /* allocate on stack */
Both of those are all C. No C++ involved.
  • Like 1
Link to comment
Share on other sites

  • 0
char *exit_passphrase_ = new char(bufsize);
strcpy(exit_passphrase_, EXIT_PASSPHRASE); // THE PROBLEM IS HERE
Because you're not creating an array on the heap as far as I can tell.

char phrase [bufsize];
or

char *phrase = new char [bufsize];
Will probably do the trick.
  • Like 1
Link to comment
Share on other sites

  • 0

It should be noted that the code you're writing isn't really C++ except calling 'new'. You could just as easily replace that with malloc, and you'd be writing pure C.

 

char *phrase = malloc ( bufsize ); /* allocate memory on the heap */
char phrase [bufsize]; /* allocate on stack */
Both of those are all C. No C++ involved.

 

 

OMG... I F*** love you man. NO HOMO. Iv'e been with PHP for SOOO long i forgot the diffrences.

This also fixed my memset fuction.

THANK YOU MAN. THANK YOU!

Such an easy fix. Soooooo much thanks.

Link to comment
Share on other sites

This topic is now closed to further replies.