Array Basics

Learn the basics of arrays from a memory point of view.

Introduction

Up until this point, we used standalone variables. In this lesson, we’ll apply the information presented in the first four chapters to arrays.

Scope

The scope of arrays follows the same rules presented for variables.

There’s a global scope and a local scope:

  • Arrays in the global scope are visible in the entire program.
  • Arrays in the local scope are only visible inside the function where they are declared.
  • Global arrays are stored inside the .data or .bss sections if they are initialized or not.
  • Local, non-static arrays are stored on the stack and cleaned before the function returns during the stack cleanup process.
  • Local, static arrays are stored inside the .data or .bss and are only visible in the function where they are declared (globals with limited scope).

See the following code, where we define a bunch of arrays. We will explore the sections in which they are stored. There are comments on lines 4, 5, and 9 in the code. We’ll uncomment these lines one by one to see the change in the output.

Note: We ignore the hex and filename columns as they aren’t important.

Get hands-on with 1200+ tech skills courses.