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. An Easy Question

An Easy Question

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformancequestionannouncement
5 Posts 3 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.
  • R Offline
    R Offline
    rahul kulshreshtha
    wrote on last edited by
    #1

    In Release mode with VS2008, Why "var" prints "Whats up" instead of "newval" It crashes in debug mode. I guess the reason might be the auto-memory allocation is "read-only". If I use char *var = new char[15] then it works fine.

    #include <stdio.h>
    #include <conio.h>
    #include <string>

    int main()
    {
    char *var = "\nWhats Up";
    strcpy(var, "newval");
    printf(var);
    getch();
    return 0;
    }

    L 1 Reply Last reply
    0
    • R rahul kulshreshtha

      In Release mode with VS2008, Why "var" prints "Whats up" instead of "newval" It crashes in debug mode. I guess the reason might be the auto-memory allocation is "read-only". If I use char *var = new char[15] then it works fine.

      #include <stdio.h>
      #include <conio.h>
      #include <string>

      int main()
      {
      char *var = "\nWhats Up";
      strcpy(var, "newval");
      printf(var);
      getch();
      return 0;
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      When you create var in this way it is pointing to a constant string so your strcpy() command is not allowed. What you should code is:

      char \*var = "\\nWhats Up";
      var = "newval";
      

      // or
      char var[] = "\nWhats Up";
      strcpy(var, "newval");

      although in the second case you could easily overwrite the buffer if you are not careful.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      R CPalliniC 2 Replies Last reply
      0
      • L Lost User

        When you create var in this way it is pointing to a constant string so your strcpy() command is not allowed. What you should code is:

        char \*var = "\\nWhats Up";
        var = "newval";
        

        // or
        char var[] = "\nWhats Up";
        strcpy(var, "newval");

        although in the second case you could easily overwrite the buffer if you are not careful.

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        R Offline
        R Offline
        rahul kulshreshtha
        wrote on last edited by
        #3

        Thanks, I got it

        1 Reply Last reply
        0
        • L Lost User

          When you create var in this way it is pointing to a constant string so your strcpy() command is not allowed. What you should code is:

          char \*var = "\\nWhats Up";
          var = "newval";
          

          // or
          char var[] = "\nWhats Up";
          strcpy(var, "newval");

          although in the second case you could easily overwrite the buffer if you are not careful.

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Richard MacCutchan wrote:

          although in the second case you could easily overwrite the buffer if you are not careful.

          Did you mean overrun, didn't you? BTW: Good answer, my 5.

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          L 1 Reply Last reply
          0
          • CPalliniC CPallini

            Richard MacCutchan wrote:

            although in the second case you could easily overwrite the buffer if you are not careful.

            Did you mean overrun, didn't you? BTW: Good answer, my 5.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Well spotted! Odd how it looked quite sensible when I wrote it.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            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