Something I never learned?

This is a discussion on Something I never learned? within the C++ Programming forums, part of the General Programming Boards category; Would somebody please tell me why the code below works, but the code below that gives an illegal access message. ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    Something I never learned?

    Would somebody please tell me why the code below works, but the code below that gives an illegal access message.
    Code:
    char MyString[] = "465.9900";
    MyString[0] = '0';
    Code:
    char* MyString = "465.9900";
    MyString[0] = '0';
    Doesn't the pointer method do the same thing?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Re: Something I never learned?

    Code:
     char* MyString = "465.9900";
    MyString[0] = '0';
    You can assign any array size to mystring however it hasn't been
    done yet, so if you add that it should work.

    Code:
     char* MyString = "465.9900";
     MyString=new char[10];
    MyString[0] = '0';

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Something I never learned?

    Originally posted by CodeMonkey
    Would somebody please tell me why the code below works, but the code below that gives an illegal access message.
    Code:
    char MyString[] = "465.9900";
    MyString[0] = '0';
    Code:
    char* MyString = "465.9900";
    MyString[0] = '0';
    Doesn't the pointer method do the same thing?
    The first one creates an array of sufficient size and copies that string into it. The array is modifyable.

    The second one is just a pointer to that string which resides somewhere in the memory. Trying to write to it will result in an access violation error.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Re: Something I never learned?

    Originally posted by Travis Dane
    Code:
    char* MyString = "465.9900";
    MyString=new char[10];
    MyString[0] = '0';
    That will not keep the old data since the pointer is repointed.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    No, no. < = new char[10] > just sets it to a blank string of 10. Putting that before the < = "blahblah" > yields the same error. (in response to the first reply)
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Something I never learned?

    They are 2 very different things

    Code:
    char MyString[] = "465.9900";
    MyString[0] = '0';
    This means allocate space on the stack and copy "465.9900" to it. The compiler sees "465.9900" as a pointer to constant data and copies it byte for byte to memory on the stack that you can alter.

    Code:
    char* MyString = "465.9900";
    MyString[0] = '0';
    Here you are simply copying the pointer to the cont data......when you try change the array, you trying to change const data and the OS will likely raise an exception.

    <edit>Magos was too quick for me</edit>

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Thank you. Now how do I make this all work? I need to change individual values of char*'s.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Now how do I make this all work? I need to change individual values of char*'s.
    If you have a char * like so:
    Code:
    char *p = "some string";
    Then the only way to make it work is to either copy the string's contents into an array or dynamically allocate the memory. String literals may be placed in read-only memory and are off limits for assignment. Besides, using a char pointer in this manner is bad form for C++, a better way would be to say:
    Code:
    char p[] = "some string";
    Or just use a std::string.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >the OS will likely raise an ?>exception.

    you mean only windows will raise an exception. accessing values from a pointer using array positions works fine under every other operating system ive used (linux, unix, solaris, bsd), just not windows.

    >Besides, using a char pointer in this manner is bad form for C++

    this is not true, the sentance should read.....

    "Besides, using a char pointer in this manner is bad form for windows"

    codemonkey, its not you. its windows. although accessing memory this way can be dangerous, it is alowed in other OS'
    im not sure how to get around it in windows other than to do as suggested before and just use a char array instead of pointer.

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Perspective
    >the OS will likely raise an ?>exception.

    you mean only windows will raise an exception. accessing values from a pointer using array positions works fine under every other operating system ive used (linux, unix, solaris, bsd), just not windows.

    >Besides, using a char pointer in this manner is bad form for C++

    this is not true, the sentance should read.....

    "Besides, using a char pointer in this manner is bad form for windows"

    codemonkey, its not you. its windows. although accessing memory this way can be dangerous, it is alowed in other OS'
    im not sure how to get around it in windows other than to do as suggested before and just use a char array instead of pointer.
    No, it's not just windows. You're VERY confused.
    String literals can be placed into real-only memory on any implementation. Attempting to write to read only memory will result in undefined behavior.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >this is not true, the sentance should read.....
    >"Besides, using a char pointer in this manner is bad form for windows"
    On the contrary, it is bad form for C++ because assigning a string literal to a non-const char * is a deprecated feature supported only for compatibility with C. Whether string literals themselves are in read-only memory or not depends on your implementation, but both the C and C++ standards explicitly state that string literals shall be treated as immutable.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    ack, woke up still half drunk and responded to this thread which i totally missunderstood. sorry to get so $$$$$y about it (it was a long night...) anywho, what i meant was access rights crashing windows programs but not linux ones. you guys are right about not being able to write to the read only memory allocated.

    >Fordy said modifying a constant string literal
    i totally missed the entire concept of this post.... i bow my head in shame....

    example of what i was trying to say:

    Code:
    char* p = "string";
    cout << *(p+2);     // legit ways of accessing data
    cout << p[2];         // access memory that is not yours, windoze will crash
    srry, bout that.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cout << *(p+2); // legit ways of accessing data
    >cout << p[2]; // access memory that is not yours, windoze will crash
    The two methods are equivalent and correct, regardless of the operating system.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    char* p = "string";
    cout << *(p+2); // legit ways of accessing data
    cout << p[2]; // access memory that is not yours, windoze will crash
    They're exactly same thing, when you compile it, the compiler changes from p[2] to *(p+2).

  15. #15
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >The two methods are equivalent and correct, regardless of the operating system.

    right, where the difference in operating systems comes into play is when you try to access memory that is beyond the scope of your c-string. it is not correct to or advisable, but different OS' will respond to it differently. windows will just terminate the program while some other systems will allow you read access to this memory.

    the sample of code was, as i already said, just to clarify what i had thought the origional question was in this post. if you didnt notice, i already admited to missunderstanding the intent of the question.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to go once you've learned C++?
    By thetinman in forum Tech Board
    Replies: 25
    Last Post: 12-06-2006, 06:54 AM
  2. Learned the basics, SO ?
    By TuxPayne in forum C Programming
    Replies: 4
    Last Post: 09-11-2005, 12:32 PM
  3. Love - learned or inherent?
    By axon in forum A Brief History of Cprogramming.com
    Replies: 42
    Last Post: 01-24-2005, 11:09 AM
  4. Learned optimism
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-25-2003, 03:06 PM
  5. What have you learned?
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-01-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21