0

Line 9 of the below code generates undefined behavior. Is this due to the fact that title1[] is outside of main() and is global? Or is it because something else that I'm missing?

1. char title1[]="The Name of the Rose";
2. Book book1={title1,900,0};
3. int main(){
4.   Book book2={"Foucault's Pendulum",1000,0};
5.   Book* book3=(Book*)malloc(sizeof(Book));
6.   *book3=book2;
7.   book1.next=&book2;
8.   book2.next=book3;
9.   book1.title[0]='B';
10.  book2.title[0]='A';
11.  {
12.    Book list[2];
13.    list[0]=book1;
14.    list[1]=book2;
15.    list[1].next->next=&book2;
16.    {
17.      Book* p=&list[0];
18.      while (p!=0) {
19.        p=p->next;
20.    }
21.  }
22. return 0;
23.}

EDIT:

Added the Book definition:

‫;‪struct Book‬‬
‪typedef struct Book‬ {
‫;‪  char* title‬‬
  int pages;‬‬
‫;‪  struct Book* next‬‬
‫;‪} Book‬‬
Tom
  • 9,275
  • 25
  • 89
  • 147
  • 1
    Could you add the definition of the `Book` class? – Philip Kendall Feb 01 '13 at 16:04
  • 5
    It's undefined because you're modifying a string literal constant (not a copy of it). – John Dvorak Feb 01 '13 at 16:04
  • is the first element in the book structure is a pointer ? – MOHAMED Feb 01 '13 at 16:05
  • @PhilipKendall Apparently, its first element is a pointer to char and that's all you need to know about it. – Alexey Frunze Feb 01 '13 at 16:05
  • 2
    @JanDvorak Are you sure? `book1.title` is a reference to the character array `title1`, so unless the `title` element of the `Book` class is const-qualified (in which case, the code should not compile), there shouldn't be a problem. Now, line 10 and assigning to `book2.title` which is a reference to a string literal; that would be undefined behaviour. And this is C; C does not have 'constructors'. – Jonathan Leffler Feb 01 '13 at 16:06
  • Duplicate hundreds of times over. [Modifying String Literal](http://stackoverflow.com/questions/5464183/modifying-string-literal) is just one example. – Carl Norum Feb 01 '13 at 16:09
  • Please try to create an [SSCCE](http://sscce.org) before posting. It'll help you find and fix your bugs without having to ask duplicate questions here. – Carl Norum Feb 01 '13 at 16:12
  • @PhilipKendall added definition. – Tom Feb 01 '13 at 16:15
  • 1
    I'm fairly sure that the line with the UB is actually line 10, which modifies the string in the second book, not the first entry - which is perfectly allowed to do. Probably just a counting error on line numbers, I expect. – Mats Petersson Feb 01 '13 at 16:17
  • 1
    How do you know that line 9 gives undefined behavior? – David Grayson Feb 01 '13 at 16:19
  • ...For that matter, what UB are you observing? How can you call it UB without knowing that what you're doing is wrong, or else observing a variety of strange behaviors that you're attributing to UB? – phonetagger Feb 01 '13 at 16:20
  • @DavidGrayson it was the TA answer to this question. phonetagger sometimes i get seg fault, other times it runs for ever – Tom Feb 01 '13 at 16:22

3 Answers3

5

No, line 9 is not undefined behavior. It writes to this array:

char title1[]="The Name of the Rose";

which is not a string literal (but initialized by one). Such a plain array can be modified to your liking. It would have been different if you would have declared it like this:

char *title1="The Name of the Rose";

The undefined behavior is in line 10, here you are writing into a string literal, which is not allowed.

BTW, when asking a question here, please cook it down to a minimal example that shows your point. Most of the code you posted is completely useless for your question.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

You are trying to modify a string literal in line 10.

sr01853
  • 6,043
  • 1
  • 19
  • 39
0

The error occurs on line 10, because a string literal is a constant and therefore read only. Any write on it will cause an error.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356