Thread: modifying string literal, identifying whether char* points to malloced

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    modifying string literal, identifying whether char* points to malloced

    Code:
        char* my_string = "oello world";
        my_string[0] = 'h';
        
        printf("%s", my_string);
    This snippet gives me a "Segmentation fault" in onlinegdb.com's C compiler. Why does it give a segmentation fault and not an error caused by trying to modify 'char const'?

    Also, is it possible to identify whether a char* passed into a function is a string literal or malloced? Intention is that I want to make sure a string literal is not passed into a function which modifies what the char* points to.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Making strings constant was something added way back in C90.
    Back then, there was still a lot of code which didn't use const properly, so you didn't get a warning.

    So as a special case, you don't get a warning when you lose the const-ness of a char pointer.

    Compare with this.
    Code:
      const int a[] = { 1 };
      int *b = a;
    
    $ gcc main.c
    main.c: In function ‘main’:
    main.c:5:12: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
       int *b = a;
    However, you can beef up your gcc like so, and get a warning when you do silly things with your string constants.
    Code:
    $ gcc -Wwrite-strings main.c
    main.c: In function ‘main’:
    main.c:4:21: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
       char* my_string = "oello world";
                         ^
    > Why does it give a segmentation fault and not an error caused by trying to modify 'char const'?
    Because regardless of what your compiler says (or not), or your decision to ignore warnings (or not), the OS always has the last word on whether a particular memory access is valid.

    > Also, is it possible to identify whether a char* passed into a function is a string literal or malloced?
    No.
    Sure, there are a multitude of hacks on a per case basis, but nothing clean simple and portable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You asked for a pointer that points to an assumed-to-be-constant string. The string ends up in "read only" memory. You will be able to change the value of the pointer, but not the data it points to.

    If you use

    Code:
    char my_string[] = "whatever";
    You are asking for an array of characters to be allocated and initialised. It ends up in either the stack or global data, which are both "read/write" memory.
    Last edited by hamster_nz; 10-08-2020 at 11:22 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nwb
    This snippet gives me a "Segmentation fault" in onlinegdb.com's C compiler. Why does it give a segmentation fault and not an error caused by trying to modify 'char const'?
    You did declare my_string to be a char* rather than a const char*. If you had declared it to be a const char*, I would expect a compile error.

    Quote Originally Posted by Nwb
    Also, is it possible to identify whether a char* passed into a function is a string literal or malloced?
    No, at least not in standard C.

    Quote Originally Posted by Nwb
    Intention is that I want to make sure a string literal is not passed into a function which modifies what the char* points to.
    The problem is that for historical reasons the type of a string literal is char[N] rather than const char[N], even though the content of the string literal is not to be modified without resulting in undefined behaviour. So you end up with a char* argument that does match a char* parameter. Naming your string literals (and hence giving them a const char* type) could help, but admittedly can be awkward and you may still need peer review to ensure it is done. It is possible that some compilers on high warning levels as well as other static analysis tools may be able to warn you about such potential undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Thank Salem, hamster_nz and laser light.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof on string literal defined in #define VS char*
    By Absurd in forum C Programming
    Replies: 4
    Last Post: 02-11-2019, 11:09 AM
  2. Most proper way to convert string literal to char*
    By Epy in forum C++ Programming
    Replies: 15
    Last Post: 09-18-2013, 07:11 AM
  3. Modifying a string literal
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 12:26 AM
  4. Modifying a string literal.
    By Eman in forum C++ Programming
    Replies: 45
    Last Post: 12-30-2010, 06:37 PM
  5. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM

Tags for this Thread