Arrays in C++ - Reading Assignment

  1. It’s an indexed sequence of values, which all share the same type and are referred via single identifier.

  2. The size of an array is equal to the number of elements(values) it holds. The full size though is calculated by multiplying the size and the space required for 1 element (length * space saved in memory for single value).

  3. arrayType arrayName[arrayLength] {particular elements, separated by comma(,)};
    arrayType and arrayName are always mandatory. arrayLength is required, but if left empty will be calculated by the number of elements added upon initialization. If the length is set, it’s not mandatory to add elements when initializing.

  4. sizeof(arrayName).

  5. You get the full size with it and divide by the size of single element (usually 0 to be sure that there is such) - sizeof(arrayName) / sizeof(arrayName[0]).

  6. You get undefined behavior.

1 Like

Okay. Sometimes the questions are quite unclear to me. I often end up referring to other students’ answers to get an idea. But thank you!

1. What is an array? // A variable used to store values of the same type.

2. Which index would you use to access the 2nd variable in an array? // Index 1…

3. What can you say about the size of an array? // Fixed Arrays are allocated by the programmer using [ ] brackets, and when allocated outsized this fixed length they are open to security breaches. A second type of array is called dynamic which can be declared after the program is compiled.

============================================================

  1. How can you initialize an array? // You use int array[]={2, 5, 7}; to assign values.

  2. How can you get the size of an array in bytes? // Use the function sizeof(array).

  3. How can you use the function sizeof in order to calculate the number of elements in an array? // sizeof(array)/sizeof(array[0]);

  4. What happens when you try to write outside the bounds of the array? // An unpredictable behaviour that could make the code vulnerable to security breaches and hacking.

1 Like

Part I

  1. An array is a an aggregate data type used to hold data elements of one type which can be accessed with a single identifier.

  2. The 2nd variable in an array resides on index 1 and can be accessed by name_of_array[1]

  3. The size of an array in bytes is the dimensions of the array multiplied by the size (in bytes) of the data type it’s holding. The size of a 3 x 5 array holding integers would be 3 x 5 x 8 bytes = 120 bytes.

Part II

  1. Arrays can be initialized with an initializer list. If the array is declared to hold more elements than given by the initializer list, the remaining elements will be set to 0 for integers, 0.0 for doubles, empty for strings… etc. It’s best practice to explicitly initialize arrays.

  2. You can use std::size(array) to get the size of the array in bytes.

  3. To get the length of an array, divide the size of the array by the size of an element. For example: sizeof(array) / sizeof(array[0])

  4. Writing outside of the bounds of an array will cause undefined behavior, specifically, it may overwrite values stored by variables elsewhere in your program. Therefore, it is important to always be cautious when indexing arrays as to not write outside the bounds.

1 Like

size will give you the length of the array, sizeof gives you the size in bytes. :slight_smile:

1 Like
1. What is an array? -> It is an aggregate data type that lets us access many variables of the same type through a single identifier.
  1. Which index would you use to access the 2nd variable in an array?
    -> 1

  2. What can you say about the size of an array?
    -> The size of the array is set when initialized and could not be changed unless its dynamic.

1. How can you initialize an array? -> By using the initializer. data type[number of elements]{element 1, 2,3,}; ex. int prime[5]{ 2, 3, 5, 7, 11 };
  1. How can you get the size of an array in bytes?
    -> sizeof() function

  2. How can you use the function sizeof in order to calculate the number of elements in an array?
    ->sizeof(int)

  3. What happens when you try to write outside the bounds of the array?
    -> undefined behavior will occur

1 Like

You have use the array type and divide it with the full size of the array. So if you have an array of int you can do sizeof(array)/sizeof(int) :slight_smile:

2 Likes
  1. An array is a data type that allow us to store and access multiple variables of the same type via single identifier.
  2. 2nd variable would be at index 1.
  3. Arrays have fixed size specified during definition or initialization of the array. Dynamic arrays are special kind of arrays which length can be set at runtime.

Second part:

  1. Arrays can be initialize via use of an initializer list like this int testArray[4] {4, 3, 2, 1} or int testArray[] {4, 3, 2, 1}. If we do not specify all values they will be set to default. We can also assign values one by one.
  2. To determine the size of an array in bytes we can use function sizeof(testArray) from std namespace.
  3. By dividing size of an array by one element, preferably by first one.
  4. If we try to write outside the bounds of an array, we may overwrite some other variables and we will get undefined behaviour as a result.
1 Like
  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.

  2. I would use index 1

  3. In fixed array, the array length must be a pre-compile constant number.

  4. One can initialize an array in C++ with following statements: int array[5]{1,2,3,4}; Array is initialized when using curly brackets.

  5. withe sizeof

  6. We can we can determine the length of a fixed array by dividing the size of the entire array by the size of an array element (sizeof(array) / sizeof(array[0]))

  7. The program identifies this as undefined behaviour.

1 Like

PART ONE

  1. What is an array?
    An array is an aggregate data type. Through an array you can access many variables of the same data type, but using a single identifier. There are two types, a fixed array and a dynamic array.

  2. Which index would you use to access the 2nd variable in an array?
    Index 1.

  3. What can you say about the size of an array?
    A fixed array, is where the length is known at compile time (it is a number), and the memory is then allocated. It is a fixed size that cannot be changed.
    A dynamic array is where the length is unknown at compile time and is actually set at runtime, hence the size can be changed.

PART TWO

  1. How can you initialize an array?
    You can initialise an array element by element, but this can be very repetitive.
    eg.
int even[6];
even[0] =0;
even[1] =2;
even[2] =4;
even[3] =6;
even[4] =8;
even[5] =10;

Or you can use an initializer list, to do the entire array in one line:
int even[6]{0,2,4,6,8,10};

C++ uses zero initialization, so that if there are not as many less initialisers in the list than the array can hold, it will set the remaining elements to 0.
If you try to put in more than the array can hold it will give an error at compilation.

  1. How can you get the size of an array in bytes?
    From the std namespace, use the function sizeof(array) where array is the identifier of the array.

  2. How can you use the function sizeof in order to calculate the number of elements in an array?
    sizeof(array) / sizeof(array[0]) so using the first element of the array to divide the calculated bytes of the array.

  3. What happens when you try to write outside the bounds of the array?
    We would get undefined behaviour. We could overwrite other values associated with other variables, or the value we have assigned could be overwritten by another variable that is using the same memory location.

1 Like

Part 1

What is an array?

It is a technique to create a index which stores value.

Which index would you use to access the 2nd variable in an array?

The answer is “1”

What can you say about the size of an array?

It is the part of code which declares how many element the array index will have.

Part 2

How can you initialize an array?

When the array are declared it is initialized just as any other variable.

How can you get the size of an array in bytes?

By adding a compile-time operator “sizeof” in a variable and initialize the variable.

How can you use the function sizeof in order to calculate the number of elements in an array?

Create a variable and storage the value sizeof(array)/sizeof(array[number of elements]);

What happens when you try to write outside the bounds of the array?

What happens is completely undefined.

1 Like

Not sure this is the correct term. An array is a list of elements of the same type which you can access with an index. :slight_smile:

I don’t think this would work, because using the number of elements as an index would actually be out of bounds of the array. You could use the last element (number of elements - 1), but it would be easier to use the first element in the array (index 0) or the type of elements in array :slight_smile:

1 Like

1- An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
2 - Index 1
3 -There are two arrays. A fixed and dynamic. A fixed array is decided when creating the code and a dynamic array is when it is not determined at the time of creating the code.

1- Example from the article: int prime[5]{ 2, 3, 5, 7, 11 };
Int array[5]{ 7, 4, 5 }; // only initialize first 3 elements
2 - sizeof(array);
3 - int a[17];
size_t n = sizeof(a) / sizeof(int);
4 - Undefined behaviour

1 Like

It is an indexed list of objects of the same type.

1 (you start counting from 0).

The size of an array can be declared as a fixed length or a dynamic length.

Either manually line by line or by declaring the entire array at once using an initializer list.

By using the sizeof() operator

You can do this by dividing the size of the entire array by the size of an array element. sizeof(array)/sizeof(array[1]).

When you write outside the bounds of the array C++ will write to memory. This leads to unknown behavior and can cause lots of trouble. :slight_smile:

1 Like
  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.

  2. To access the second variable in an array, you have to use index 1 (starts with 0)

  3. The size of an array is the sum of all its elements multiplied by the total bytes the type is.

  4. element by element OR a more convenient way to initialize entire arrays is via use of an initializer list.

  5. sizeof(array)

  6. sizeof(array) / sizeof(array[0]) so the size of the array devided by the bytes looked up using the first element gives the number of elements.

  7. When this happens, you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.

1 Like

This will only work if the array is larger than 1 element :wink: I would rather use the first element in the array because you can be sure it exists (array[0])

2 Likes

Thank you for the clarification! :slight_smile:

1 Array = a list of similar objects
2 The second element is 1
3 The size is the number of listed objects.

1 An array can be initialized as a list of constants/variables, or as a list of elements…
2 Use sizeof(name)
3 sizeof(array) / sizeof(array[0])
4 Too many objects in an array will create the overflow error undefined behavior.

1 Like

They are objects of the same type :slight_smile:

Since there aren’t any more objects in the array you could potentially write out of bounds of the array. :slight_smile:

2 Likes

1 Array is an aggregate data type that let us access many variables of the same type through the single identifier
2 Index 1 because the first variable in the array is always index 0
3 The size of an array is the array length multiplied by element size. You can specify the array length during the declaration between brackets, otherwise, the complier can do it for you if you initialize the array.

1 Many ways.
Example:
int array [3] = { 7 }; //only initialize the first element
int array [3] = ={0, 1, 2}; //explicitly define length of array
int array [] = { 0, 1, 2};// let initializer list set length of the array

  1. size of an array is commonly base the number of total integers multiplied by 4 because on a 32 bit program every integer represents 4 bytes and every character represent 2 bytes or you can use the following example:
    sizeof(array);

3
sizeof(array)/sizeof( );
Example array of int: size of(array)/sizeof(int);

4 you will get an undefined behavior. you can overwrite a value in memory but the program can crash

1 Like