2

Given the following code:

#include<iostream>
int main(){
  char container[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
  for(char* cptr = container; *cptr != 0; cptr++)
    std::cout << *cptr << std::endl;
  return 0;
}

It prints these characters in sequence each time I execute it. I cannot understand why the loop would terminate since I have not explicitly specified any null terminator at the end of the container array. Please help.

10
  • It should result in a segmentation fault Commented Oct 14, 2013 at 16:42
  • 3
    Depending on the environment, the memory after your container may be initialized with zeros, causing a null terminator to happen to be there. However this isn't guaranteed, so it could happen that it fails on a different machine/different compiler settings/different anything really. Commented Oct 14, 2013 at 16:42
  • Undefined behaviour, you can'r predict that how code behavior at runtime. Commented Oct 14, 2013 at 16:43
  • 1
    @VladLazarenko There are dynamic analysis tools like valgrind and electric fence that are meant to find these sort of issues. Also, debug versions of heap allocators typically write patterns into memory to find problems with referencing the heap poorly. In this case it is stack memory so you probably need something else. Commented Oct 14, 2013 at 16:50
  • 2
    thats why its called undefined behavior. It could format your hard drive, light up the white house christmas tree, or work, .... Commented Oct 14, 2013 at 17:03

4 Answers 4

8

It's just luck, really.

It happens that the area of memory corresponding to container[7] is 0, so you're getting lucky.

Exceeding the bounds of your array is undefined behavior. In your case, it just happens to be the behavior you were hoping for, but you can't rely on that.

Sign up to request clarification or add additional context in comments.

2 Comments

+ but lucky should be unlucky ;) it is worth case not best.
@GrijeshChauhan Heh. I didn't say "good luck" or "bad luck". Sidharth - it's good luck in that "your code works just as you'd like it to!" It's bad luck in that if/when you move to any other system, you'll get a bug that doesn't appear on your system, so will be hard to track down.
3

First, the code you posted has a horrible bug: cptr != 0 should be *cptr != 0. You should be checking to see if the character at the given address is null, not if the pointer itself is null.

The other answers and comments are correct. The only reason you're getting the correct output is because there happens to be some zeroed memory right at the end of your container array. Putting the container array inside a struct will help to eliminate the extra padding the compiler might otherwise insert:

#include<iostream>
int main(){
  struct {
    char container[7];
    char alphabet[27];
  } x = {
    {'a', 'b', 'c', 'd', 'e', 'f', 'g'},
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  };
  for(char* cptr = x.container; *cptr != 0; cptr++)
    std::cout << *cptr << std::endl;
  return 0;
}

If you run this version you'll see your array 'a''g' printed, then it will run over into the second array and print 'A''Z' before hitting the null at the end of that string.

1 Comment

@sidharthsharma - In the future you can test the code you're posting to avoid such typos. That confused at least one other commenter trying to run the code you posted.
2

You're running off the end of the array which invokes undefined behavior. One possible undefined behavior is that it works in a seemingly reasonable way. In this case probably what's happening is that the undefined behavior is padding your array with zeros.

Comments

1

You are evoking Undefined Behavior. Undefined Behavior means, "anything can happen," which includes, sometimes, exactly what you want to happen. That's what's happening here.

The reason why you're invoking Undefined Behavior is because you are reading from uninitialized memory, when you access the element one-past-the-end of container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.