Why is my char* writable and sometimes read only in C++

后端 未结 4 1501
天涯浪人
天涯浪人 2020-12-21 09:28

I have had really big problems understand the char* lately. Let\'s say I made a recursive function to revert a char* but depending on how I initial

相关标签:
4条回答
  • 2020-12-21 09:50
    char * bob = "hello"; 
    

    This actually translated to:

    const char __hello[] = "hello";
    char * bob = (char*) __hello;
    

    You can't change it, because if you'd written:

    char * bob = "hello"; 
    char * sam = "hello"; 
    

    It could be translated to:

    const char __hello[] = "hello";
    char * bob = (char*) __hello;
    char * sam = (char*) __hello;
    

    now, when you write:

    char * bob = new char[6];    
    bob = "hello\0";
    

    First you assign one value to bob, then you assign a new value to it. What you really want to do here is:

    char * bob = new char[6];    
    strcpy(bob, "hello");
    
    0 讨论(0)
  • 2020-12-21 10:01

    You should always use char const* for pointers to string literals (stuff in double quotes). Even though the standard allows char* as well, it does not allow writing to the string literal. GCC gives a compile warning for assigning a literal address into char*, but apparently some other compilers don't.

    0 讨论(0)
  • 2020-12-21 10:06

    Edit: The question was retagged as C++ instead of C which was originally there but re-tagged....

    Ok. You have got a couple of things mixed up... new is used by C++, not C.

    • Case #1. That is declaring a pointer to char. You should be able to manipulate the string...can you show the code in what you did to do swapping characters.
    • Case #2/#3. That you got random crap, and discovered that a nul terminator i.e. '\0'...occupies every single string you'll encounter for the duration of C/C++, possibly for the rest of your life...
    +-+-+-+-+-+--+
    |H|e|l|l|o|\0|
    +-+-+-+-+-+--+
                ^
                |
             Nul Terminator
    
    • Case #4 did not work as you need to use a strcpy to do that job, you cannot simply assign a string like that after calling new, when you declare a string char *s = "foo"; that is initialized at compile time. But when you do it this way, char *s = new char[6]; strcpy(s, "hello"); that gets copied into the pointer variable s.

    You will eventually discover that this pointer to a memory block occupied by s will easily get over-written which will induce a fit of conniptions as you realize that you have to be careful to prevent buffer overflows...Remember Case #3 in relation to nul terminator...don't forget that, really, that string's length is 6, not 5 as we're taking into account of the nul terminator.

    • Case #5. That is declaring a pointer to array of type char, i.e. a multi-dimensional array, think of it like this
    *(bob + 0) = "foo";
    *(bob + 1) = "bar";
    

    I know there is a lot to digest...but feel free to post any further thoughts... :) And best of luck in learning...

    0 讨论(0)
  • 2020-12-21 10:11

    The key is that some of these pointers are pointing at allocated memory (which is read/write) and some of them are pointing at string constants. String constants are stored in a different location than the allocated memory, and can't be changed. Well most of the time. Often vulnerabilities in systems are the result of code or constants being changed, but that is another story.

    In any case, the key is the use of the new keyword, this is allocating space in read/write memory and thus you can change that memory.

    This statement is wrong

    char * bob = new char[6];
    bob = "hello\0";
    

    because you are changing the pointer not copying the data. What you want is this:

    char * bob = new char[6];
    strcpy(bob,"hello");
    

    or

    strncpy(bob,"hello",6);
    

    You don't need the nul here because a string constant "hello" will have the null placed by the compiler.

    0 讨论(0)
提交回复
热议问题