C++ Tutorial: The Do's and Don'ts of Accessing One Element Past the End of an Array | CodeGuru

C++ Tutorial: The Do’s and Don’ts of Accessing One Element Past the End of an Array

Introduction Seemingly, the only safe option is to never access elements that are outside the valid boundaries of an array. However, there are cases when you need to access a memory address pointing to one element past the end of an array: when traversing an array, or in algorithms that manipulate a sequence of elements. […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 11, 2011
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

Seemingly, the only safe option is to never access elements that are outside the valid boundaries of an array. However, there are cases when you need to access a memory address pointing to one element past the end of an array: when traversing an array, or in algorithms that manipulate a sequence of elements. As opposed to a common belief, C++ programming does permit access to the address of one element past the end of an array. However, you have to do that very carefully, paying attention to several important restrictions.

Sorting an Array

Accessing the address of one element past the end of an array is required more often than you think. Suppose you have an array of integers that you want to sort using the std::sort() algorithm:

  int arr[5]= {3,4,89,7,0};
  std::sort(arr, arr+5);

Sort() requires two forward iterators (recall that pointers are perfectly valid iterators): an iterator indicating the first element of a sequence, and another iterator indicating the end of the sequence. Notice that the end of a sequence is not the last valid element of the array, i.e., arr[4]. Rather, it’s the address of one element past the last valid element, namely arr+5. Any attempt to dereference arr+5 would result in undefined behavior. However, the address arr+5 itself is valid for certain purposes.

Advertisement

Valid and Invalid Operations

Taking the address of one element past the end of an array is safe and permitted, so long as you’re not using that address to read or write to the data to which the address is pointing. Additionally, you’re not allowed to increment that pointer any further. However, you may decrement that pointer. Additionally, you can use that address in pointer comparison expressions, as in the following example:

   for (int *p=arr; p<arr+5; p++)
     *p=0; // clear the array

   Or even like this:

   int n=5;
   while(n) //a more cumbersome method of clearing the array
   {
    *(arr+5-n)=0;
    n--;
   }

   In contrast, dereferencing the expression arr+5 is undefined:

   if (*(arr+5)) //undefined behavior
    x++;
 

Generally speaking, the Standard allows you to use arr+5 only as a pointer, never as the value to which it’s pointing.

  vector <int> vi;
  vi.push_back(1);
  vector<int>::iterator it= vi.end();
  *(--it)=8; //OK, assigns 8 to vi[0]
  ++it; //advance to one past the last valid element
  if (it==vi.end()) //OK, comparison
   cout<<you have reached the end of the vector"<<endl;
  cout<<*it<<endl; //undefined behavior; dereferencing

Summary

Accessing the address of one past the last element of an array is a valid operation under certain conditions. You can use that address only in pointer arithmetic expressions that access valid elements of the array, and in comparisons. You’re not allowed to dereference the result nor can you increment the pointer any further (say reaching the third element past the array’s end). Notice that STL containers follow this idiom. The end() member function returns an iterator pointing to one element past the last element of the container. You may use the iterator returned from end() only in comparisons and in expressions that access valid elements of the container:

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.