Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”

Similar presentations


Presentation on theme: "CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”"— Presentation transcript:

1 CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range” of items) –Allows some useful tricks with addressing –Can make certain algorithms more efficient Array indexes are another form of aliasing –Can interchange with pointer dereferencing –Can do index arithmetic, like pointer arithmetic Arrays can be indexed by enumerated types –Allows labels to be used instead of numbers

2 CSE 232: C++ memory management Pointers and Arrays int main (int, char **) { int arr[3] = {0, 1, 2}; int * p = &arr[0]; int * q = arr; // p, q both point to arr[0] ++q; // now q points to arr[1] } Array variable itself behaves like a const pointer int * const arr; Set pointers to the start of the array –Using address of 0 th element –Or, just using array name Pointer arithmetic –Adding number n to a pointer Changes pointer’s value By n times the size of the type to which it points Moves pointer n array positions –E.g., value in q is increased by sizeof(int) by ++ q 0xefffdad0 2 int arr[3] int *p 1 0xefffdad4 int *q 0 Notice alternative declaration

3 CSE 232: C++ memory management Arrays of (and Pointers to) Pointers int main(int argc, char * * argv) { for (int i = 0; i < argc; ++i) { cout << argv[i] << endl; } return 0; } Can have pointers to pointers Can also have an array of pointers (like a const pointer to a pointer type) E.g., argv parameter in main –Array of pointers to character strings –Could also declare as a pointer to the first pointer –Array dimension is not specified –Instead a special argument ( argc ) holds array size By convention, character strings are zero terminated –Special char is ‘\0’ not ‘0’ 0xefffbab0 char * * argv int argc 2 0xefffa0d0 hellotest\0

4 CSE 232: C++ memory management Exercise Write a simple main function –or download array_exercise0.cc Declare two arrays of integers, both of length 3 –Initialize one of them directly with the values 9, 7, 5 –Leave one of the arrays un-initialized Write a print function for these arrays –Parameters should be an array and a size variable –The function should loop through the passed array … … and print out the value at each position in it … … stopping after it has printed all positions in the array. –Call the function twice from the main function to print both arrays –Note that you can either put the definition of the function before main… –…or you can put a “forward” declaration of the function before main Use a loop to copy values from initialized array into the other –Call print function again on each of the arrays and see what prints out


Download ppt "CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”"

Similar presentations


Ads by Google