Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why is my C program crashing?

Why is my C program crashing?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
15 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C color Aljechin

    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:

    K Offline
    K Offline
    kakan
    wrote on last edited by
    #6

    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

    C 1 Reply Last reply
    0
    • K kakan

      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

      C Offline
      C Offline
      color Aljechin
      wrote on last edited by
      #7

      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:

      K C 2 Replies Last reply
      0
      • C color Aljechin

        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:

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #8

        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

        1 Reply Last reply
        0
        • C color Aljechin

          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:

          C Offline
          C Offline
          CodeMak
          wrote on last edited by
          #9

          HI me also trying for it... its working release version not in debug version why is that?????

          C 1 Reply Last reply
          0
          • C CodeMak

            HI me also trying for it... its working release version not in debug version why is that?????

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #10

            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

            C 1 Reply Last reply
            0
            • C Christian Graus

              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

              C Offline
              C Offline
              CodeMak
              wrote on last edited by
              #11

              HI So why is char * source = "Hello" giving problem why am i able to just read memory not write to it????

              C 1 Reply Last reply
              0
              • C CodeMak

                HI So why is char * source = "Hello" giving problem why am i able to just read memory not write to it????

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #12

                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

                1 Reply Last reply
                0
                • C color Aljechin

                  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:

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #13

                  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 from source. 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

                  1 Reply Last reply
                  0
                  • C color Aljechin

                    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:

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #14

                    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 copy source 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

                    C 1 Reply Last reply
                    0
                    • M Michael Dunn

                      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 copy source 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

                      C Offline
                      C Offline
                      color Aljechin
                      wrote on last edited by
                      #15

                      Thank you very much, I understand it now. I did not give you that 2 vote. I am checking the answer today only.

                      :rose:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups