ECS 30 Lecture Notes - Lecture 12: Memory Address, Segmentation Fault

18 views4 pages
Friday, 27 April 2018
ECS 030 - Lecture 12
Arrays Continued
Indexing
-Any expression that produces an
integer can be used as subscript, as
long as subscript stays within bounds
useful for loops
-increase the increment of i in the for
loop each time it reaches the end of
the loop, and work with each element
in the array one by one, starting at the
lowest index you want to work with
Loops
-Very often, arrays are meant to be
sequentially accessed
for loop is the perfect repetition
structure
-Use of a constant for the array size is
always recommended
In cause you ever need to work with
the array, you refer to the global
constant for its size; makes the code
easier to understand and less complex
1
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Any expression that produces an integer can be used as subscript, as long as subscript stays within bounds: useful for loops. Very often, arrays are meant to be sequentially accessed: for loop is the perfect repetition structure. The behaviour of out-of-bound accesses is dif cult to predict: illegal accesses might not cause any error, and just generate random variables for uninitialised indexes, or might cause a segmentation fault error. Each element in an array has its own memory address. Addresses are 4-byte apart (can check with sizeof(int)) value associated to the array name is an address: it"s a pointer, but cannot be reassigned to another address. Pointer is constant: indexes are distances from this pointer (in this case, each following index is 4 bytes away from the previous, since the array consists of int variables) Address of the array and its rst variable is the same.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents