Skip to Content
Safe C++
book

Safe C++

by Vladimir Kushnir
May 2012
Intermediate to advanced
142 pages
2h 54m
English
O'Reilly Media, Inc.
Content preview from Safe C++

Chapter 4. Index Out of Bounds

There are several ways in C++ to create an array of objects of some type T. Three common methods are:

#define N 10  // array size N is known at compile time
  T static_array[N];

  int n = 20; // array size n is calculated at runtime
  T* dynamic_array = new T[n];

  std::vector<T> vector_array; // array size can be changed at runtime

Of course, you can still use the calloc() and malloc() functions and your program will compile and run, but it’s not a good idea to mix C and C++ unless you have to because you’re relying on legacy C libraries. However you allocate the array, you can access an element in it using an unsigned integer index:

const T& element_of_static_array  = static_array[index];
const T& element_of_dynamic_array = dynamic_array[index];
const T& element_of_vector_array  = vector_array[index];

Let’s deal with dynamic arrays and vectors first, and return to the static array later in this chapter.

Dynamic Arrays

What would happen if we provide an index value that is larger than or equal to the array size? In all three of the preceding examples, the code will silently return garbage. (The exception to this rule for Microsoft Visual Studio 2010 is discussed later.) The situation is even worse if you decide to use the operator [] in the left-hand side of an assignment:

some_array[index] = x;

Depending on your luck (or lack of thereof) you might overwrite some other unrelated variable, an element of another array, or even a program instruction, and in the latter ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Optimized C++

Optimized C++

Kurt Guntheroth
C++ Reactive Programming

C++ Reactive Programming

Praseed Pai, Peter Abraham

Publisher Resources

ISBN: 9781449321338Catalog PageErrata