-
June 13th, 2002, 08:52 AM
#1
why am I crashing when I try to strcpy into a char* ?
I have:
char* filename;
filename[0] = '\0';
filename = "untitled.cis";
char filename2[256];
filename2[0] = '\0';
strcpy(filename2, dlg.GetFileTitle());
filename = ""; // to clear it out?
strcpy(filename, filename2);
it crashes on this last line.... why?
Thanks!
-
June 13th, 2002, 09:01 AM
#2
Hi,
char* filename;
is a pointer u need to allocate in order to use it.
you have generated an access violation.
to make it work
char filename[MAX_PATH]="\0";
strcpy( filename, "untitled.cis" );
char filename2[MAX_PATH]="\0";
strcpy( filename2, dlg.GetFileTitle() );
// to clear it out?
memset( filename , 0, MAX_PATH*sizeof(char) );
strcpy( filename, filename2 );
-
June 13th, 2002, 09:16 AM
#3
Aaaarrrggghhhh.
How can ANYONE code like that!
I reckon it is someone who learnt C++ first, and then tried to code strings the old fashioned C way
-
June 13th, 2002, 10:34 AM
#4
What do u mean?!?
If U do not ask u cann't know can u
-
June 13th, 2002, 11:12 AM
#5
Re: why am I crashing when I try to strcpy into a char* ?
Originally posted by lab1
I have:
char* filename;
filename[0] = '\0';
filename = "untitled.cis";
char filename2[256];
filename2[0] = '\0';
strcpy(filename2, dlg.GetFileTitle());
filename = ""; // to clear it out?
strcpy(filename, filename2);
it crashes on this last line.... why?
Thanks!
If this is 'C', then the solution provided by Avi is satisfactory. If this is a C++ program, you could do a lot better than this. But first, here are the things wrong with your program:
1) How do you know that the file title will be less than 256 characters? The first strcpy() will blow up if you happen to get a file > 256 characters.
2) The filename = "untitled.cis" and filename = "" line: These assign pointers to a string literal to filename. A string literal is implied to be a const char * (i.e. the buffer being pointed to shouldn't be modified). It is undefined behavior when you assign to a constant, which is what you are doing in the last strcpy(). This is why it blows up. Yes, "filename" may have originally been declared as a "char *", but when you assign a string literal to it, you cannot (or more accurately, shouldn't) write to it.
Note that if you ran this program on another compiler (or maybe even another version of VC++), the strcpy() may not have blown up. This is the sign of undefined behavior, so you never write constructs that invoke it. Writing to a string literal is one such way of invoking undefined behavior.
Here is an alternative, C++ solution, without the errors that you are getting now:
Code:
#include <string>
std::string filename;
filename = "untitled.cis";
std::string filename2;
filename2 = dlg.GetFileTitle();
filename = filename2;
a) There is no danger of filename2 being too small for the return of dlg.GetFileTitle(). The std::string class is a dynamic string, and can accommodate strings of varying sizes.
b) You assign to a string with a simple "=". There is no need for strcpy() or messy char * pointers to deal with.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|