Is this undefined behaviour?

This is a discussion on Is this undefined behaviour? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { char v[] = "blablabla"; v[0] = 'b'; return 0; } I know this is, ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Is this undefined behaviour?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char v[] = "blablabla";
    
        v[0] = 'b';
    
        return 0;
    }
    I know this is, but what about this?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char *v = malloc(strlen("blablabla") + 1);
        if (v)
            strcpy(v, "blablabla");
    
        v[0] = 'b';
    
        free(v);
    
        return 0;
    }
    I think it is ok but I just want to make sure. Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Neither is UB. You are thinking of the UB of writing to a string literal.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    if char *v = "blablabla";
    this one is undefined behaviour.
    Code:
    The C99 Draft (N869, 18 January, 1999)
    J.2 Undefined behavior
    #1
    The behavior is undefined in the following circumstances: ...
    An attempt is made to modify a string literal of either form (6.4.5).









    EDIT=edited.thanks xeddiex and eerok.
    Last edited by cbastard; 01-15-2006 at 01:04 PM.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    Code:
      char *s1 = "string literal";
      char s2[] = "not a string literal";

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by eerok
    Code:
      char *s1 = "string literal";
      char s2[] = "not a string literal";
    immutable: char *s1
    They're both string literals but the difference is that in the first one, s1 is being assigned a literal that is located in the data segment (always?) where all your programs execution instructions are stored along with other life time program duration stufff and you cannot and shouldn't try to change anything there.

    mutable: char s2[]
    The latter one has two stages; first the memory for s2 is allocated on the stack and then the string literal (which is located in the data segment) on the right is copied character for character to each block in memory in s2. And the values in s2 are now different from the string literal because they're both different copies, so, it's safe to write to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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