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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why it fails?

Why it fails?

Scheduled Pinned Locked Moved C / C++ / MFC
phpcomperformancequestion
7 Posts 5 Posters 1 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.
  • S Offline
    S Offline
    Sarath C
    wrote on last edited by
    #1

    char *p= "test string";
    p[5] = '_';

    This code will fail. why? My Assumption: "test string" remaining in memory as a constant pointer, hence it modifying teh same fails. it would be helpful if you let me know how compiler handling such definitions and how it resides in memory

    SaRath.
    _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


    My Blog | Understanding State Patte_

    S D 2 Replies Last reply
    0
    • S Sarath C

      char *p= "test string";
      p[5] = '_';

      This code will fail. why? My Assumption: "test string" remaining in memory as a constant pointer, hence it modifying teh same fails. it would be helpful if you let me know how compiler handling such definitions and how it resides in memory

      SaRath.
      _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


      My Blog | Understanding State Patte_

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      String literals are constant. For historical reasons you can use char * (when you really should only be able to use const char *) in this case. See here[^] to see what Microsoft has to say about this.

      1 Reply Last reply
      0
      • S Sarath C

        char *p= "test string";
        p[5] = '_';

        This code will fail. why? My Assumption: "test string" remaining in memory as a constant pointer, hence it modifying teh same fails. it would be helpful if you let me know how compiler handling such definitions and how it resides in memory

        SaRath.
        _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


        My Blog | Understanding State Patte_

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

        Sarath.This code will fail. why?

        It's the difference between arrays and pointers. Had you used the following, it would have worked:

        char p[] = "test string";
        p[5] = '_';

        Z 1 Reply Last reply
        0
        • D David Crow

          Sarath.This code will fail. why?

          It's the difference between arrays and pointers. Had you used the following, it would have worked:

          char p[] = "test string";
          p[5] = '_';

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          Just an FYI: It is VERY bad to declare an array this way and then modify it. In this case, it won't hurt anything, but lets say you tried something like: char p[] = "hello"; p[5] = ','; p[6] = ' '; p[7] = 'W'; p[8] = 'o'; p[9] = 'r'; p[10] = 'l'; p[11] = 'd'; Now you have overwritten parts in memory that were not allocated for that string. With few exceptions, you should always specify a size for your arrays: char p[30] = "hello, world!";

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          S 1 Reply Last reply
          0
          • Z Zac Howland

            Just an FYI: It is VERY bad to declare an array this way and then modify it. In this case, it won't hurt anything, but lets say you tried something like: char p[] = "hello"; p[5] = ','; p[6] = ' '; p[7] = 'W'; p[8] = 'o'; p[9] = 'r'; p[10] = 'l'; p[11] = 'd'; Now you have overwritten parts in memory that were not allocated for that string. With few exceptions, you should always specify a size for your arrays: char p[30] = "hello, world!";

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            S Offline
            S Offline
            Saravanan Sundaresan
            wrote on last edited by
            #5

            I think, Null char to be added to p[12].

            D Z 2 Replies Last reply
            0
            • S Saravanan Sundaresan

              I think, Null char to be added to p[12].

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

              You can think it, but it'd still be wrong.


              "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

              "Judge not by the eye but by the heart." - Native American Proverb

              1 Reply Last reply
              0
              • S Saravanan Sundaresan

                I think, Null char to be added to p[12].

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                Adding NULL to p[12] would still cause a segmentation fault (aka crash). Since the array was not declared with any bounds, it defaults to the size of the initailized string (which is 6 for "hello"). Any character that is written past that size is overwriting something in memory that may or may not be important. If it overwrites, for example, the next instruction on the stack ... you get the idea. The point is, you should always declare arrays with a size.

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                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