-
Notifications
You must be signed in to change notification settings - Fork 1
Pointers
Can't copy an array is usually converted to a pointer.
This functions are equivalent
void function(const int *arr);
void function(const int arr[]); // shows the intent that the function takes an array
void function(const int arr[10]); // dimension for documentation purposes (at best)
Use unary operators.
void PrintArray(int array[20])
{
std::cout << "sizeof(array) = " << sizeof(array) << std::endl; // it will print only 4 bytes
}
int main()
{
srand( time(0) );
int randomArray[20];
for (int i = 0; i < 20; ++i)
{
randomArray[i] = rand() % 101;
}
std::cout << "sizeof(randomArray) = " << sizeof(randomArray) << std::endl; // it will print 80 = 20 elementes each of size 4 bytes
PrintArray(randomArray);
}
-
Character arrays, end marker (NULL)
-
Integer arrays
void function(const int* beg, const int *end) { while(beg != end) std::cout<< *beg++<
-
Indicate the size of the array in a parameter.
It is possible to specify the dimension of the array using references, but the caller will be bounded to the size on the arguments
void function(int (&arr)[10]); // only arrays of size 10 accepted
- Global
- Class
- Namespace
- Block
Delimited by curly braces
Parameter initialization works the same way as variable initialization.
When an argument value is copied, the parameter and the argument are independent objects.
Changes made to the variable have no effect on the initializer.
Using references to avoid copies
TODO
Essentially an alias to a variable.
You can return multiple values from a function.
// Initialize to null
int* intPtr = 0;
float* floatPtr = 0;
int intVar = 50;
float floatVar = 3.1415f;
// Initialize pointers to an address
int* intPtr = &intVar;
float* floatPtr = &floatVar;
Derreferencing
Constant pointers
float* const constFloatPtr; // the pointer is constant, the variable pointed to CAN change
float const* constFloat0; // the pointer is NOT constant, the variable pointed CAN't change
const float* constFloat1;
const float* const constFloatConstFloatPtr; // pointer and the variable pointed CAN'T change
TODO
void f1(const int i){ ... }
void f1(int i) { ... } // WRONG!
Can't overload a function: parameter list are not sufficiently different.
Refer to example in p 283 & 283
The complete main function
int argc, char ** argv
Use command line arguments
Defined in cstdlib when exiting main (not std:: scope needed)
EXIT_FAILURE
EXIT_SUCCESS
Error if they only differ in their return types
Avoid function call overhead.
Is a request to the compiler, it might be ignored
assert only works if NDEBUG is not defined
To turn off asserts, NDEBUG must be defined.
FILE
LINE
TIME
DATE
- Allocation
- Deallocation
- Memory Leaks
- dynamic allocation for variable, array, deallocate dynamic variable
- runtime, compile time memory allocation
- a function exists in memory and therefore has a memory address?
- About references, can we initialize a reference to NULL?
- about pointers, passing an array to a function parameter
- Pointers and symbols & # ++ -1
- comparing vectors and dynamicly allocated arrays
- example of memeory address and variable
- example pointer arithmetic
- About functions returning more than one result using references or pointers