Why is my C program crashing?
-
kakan wrote:
Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
That way even the dest has the terminating null byte. ( "" == '\0' ). Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me. I tried declaring *dest as "Halo" and it still crashes.
:rose:
-
You shouldn't do it this way. Try this instead:
main(){
char temp[30];
char *source = "Hi", *dest = temp;
while(*source++) *dest++ = *source;
}Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
kakan wrote:
You shouldn't do it this way. Try this instead:
Please tell me where exactly was I going wrong. Can you show me a sample to implement strcpy this way? I am trying like mad since half an hour.
:rose:
-
kakan wrote:
You shouldn't do it this way. Try this instead:
Please tell me where exactly was I going wrong. Can you show me a sample to implement strcpy this way? I am trying like mad since half an hour.
:rose:
This really is basics, I suggest you read a book that covers C. It would make things clearer to you. Your original code was this:
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}A declaration like this: char *source = "Hi" is (normally) used for constant values (that doesn't change during the execution of the program). If you want a buffer that can be modified, you'd better allocate it, like this: char temp[50]; Now you have a buffer that can hold up to 50 characters. Use it as you wish, i.e. like this (as long as your source text doesn't exceed 49 charaters plus the terminating null character): strcpy(temp, "Hello World");
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
kakan wrote:
You shouldn't do it this way. Try this instead:
Please tell me where exactly was I going wrong. Can you show me a sample to implement strcpy this way? I am trying like mad since half an hour.
:rose:
-
HI me also trying for it... its working release version not in debug version why is that?????
The original code stomps memory that it doesn't own. In release mode, the behaviour is undefined, in debug, the IDE is watching if you do this, and letting you know that you've done it. The release mode version will crash, sometimes.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
The original code stomps memory that it doesn't own. In release mode, the behaviour is undefined, in debug, the IDE is watching if you do this, and letting you know that you've done it. The release mode version will crash, sometimes.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
HI So why is char * source = "Hello" giving problem why am i able to just read memory not write to it????
You can write to it, I believe. The problem is, it's a fixed memory location, of fixed size. You can't reassign the string to a longer length. It's bad practice to define a string this way, unless you expect it to be a constant in your program.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
kakan wrote:
Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
That way even the dest has the terminating null byte. ( "" == '\0' ). Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me. I tried declaring *dest as "Halo" and it still crashes.
:rose:
Aljechin wrote:
Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me.
What are you not understanding? It's been explained several different ways. In your code snippet,
dest
is not pointing to a valid (i.e., allocated) memory location. Until it is, you cannot copy characters to it fromsource
. strcpy() would behave in the same fashion had you used:char *source = "Hello", *dest = "";
strcpy(dest, source);One solution is to use:
char *source = "Hello", dest[6] = "";
strcpy(dest, source);Another would be:
char *source = "Hello", *dest = new char[6];
strcpy(dest, source);
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Where am I going wrong? I am trying to implement strcpy manually.
main() { char *source = "Hi", *dest = ""; while(*source++) *dest++ = *source; }
Visual C++ 6.0 Compiler with service pack 6. WindowsXP 32 bit edition.
:rose:
There are two problems. 1. Your
char*
variables are initialized to point at string literals. You should never write to such memory, as the compiler may store the strings in read-only memory, causing a crash if you write to them. 2. Even if the memory were writable,dest
only has room for one char. When you copysource
into it, you're writing off the end of the array.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
There are two problems. 1. Your
char*
variables are initialized to point at string literals. You should never write to such memory, as the compiler may store the strings in read-only memory, causing a crash if you write to them. 2. Even if the memory were writable,dest
only has room for one char. When you copysource
into it, you're writing off the end of the array.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
Thank you very much, I understand it now. I did not give you that 2 vote. I am checking the answer today only.
:rose: