1

I have the following code:

char* str = "01248";
printf("%x \n", str[str[1] + str[3]]);

As str[1]+str[3] = 101, the code looks for str [101].

Will this code get (always) a segmentation fault error? Or there's a change that we have something at this address and we'll print something?

Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
user25063
  • 21
  • 1
  • 1
    possible duplicate of [Why do I get a segmentation fault when writing to a string?](http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string) – Michael Foukarakis Jun 23 '14 at 07:18
  • 1
    I don't get the -1s. Care to explain them? It is a question which shows lack of knowledge, bit that's what a question is supposed to do. It is not a bad question per se... – glglgl Jun 23 '14 at 07:30

4 Answers4

9

This will always invoke undefined behavior.

You're indexing out of bounds, you can't do that without getting undefined behavior.

Exactly what happens is, wait for it, undefined. It can cause a segfault, there might be no problem at all and some value might get printed, you just can't know.

If even nothing "bad" happens and some value is printed, the program is still faulty and broken.

Also, of course there's no difference between this and just

printf("%x\n", str[101]);  /* BAD CODE! */

The fact that you compute the invalid array index by adding two valid dereferences doesn't matter, that's just an extra layer of confusion. Also, you can't "know" that 1 + 4 is any particular numerical value, that's up to the target machine's character encoding. In ASCII, it will be 49 + 52, i.e. 101. You can't even know that '1' + '4' is positive.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

Will this code get (always) a segmentation fault error?

No.

The string literal lies in a special rodata section with all other string literals and maybe other readonly data (though I don't know what these could be).

Here it depends how the data is arranged in that segment. If this string is followed by others with a length of at least 100 bytes, it will probably succeed and you'll access another string. But if it is the last one in that segment, you'll hit an address outside of anything allocated and get a segfault.

Nevertheless, it is undefined behaviour and you should not rely on it. In doubt cases, it may or may not succeed depending on the ordering and length of other strings.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

This happens because you're adding two chars.

str[1] = '1' = 49

str[3] = '4' = 52

so str[1] + str[3] is 101.

What you're looking for is to convert your char into int:

char* str = "01248";
int index1 = str[1] - 48;
int index2 = str[3] - 48;
printf("%x \n", str[index1 + index2]);
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
  • 2
    I'm not sure they look for this. They are aware that the result is 101, and the question was if that always leads to a segfault. – glglgl Jun 23 '14 at 07:18
0

There is a change that it print something. IF the OS reserve memory for the array it will print something but if the accessed index is beyond the reserved memory it will be segmentation fault

user3309301
  • 301
  • 1
  • 4