Revision - Revision of Some C++ Concepts Arguments to C++...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Revision of Some C++ Concepts Arguments to C++ Programs #include <iostream> using namespace std; int main(int argc, char *argv) { cout << "Number of args " << argc << endl; for (int i=0; i<argc; i++) cout << argv[i] << endl; return 0; } 1 If program is stored in file testArg.C and compiled to the executable testArg , then ./testArg COS110 "is fun" produces the following output Number of args 3 ./testArg COS110 is fun 2 Global vs Local Variables Global variables • are declared outside a function, • and is visible in any other function • not good programming style • unexpected side-effects • difficult to debug Local variables • are declared inside a block, e.g. function • only visible inside the scope of the block • are destructed outside the scope the block Remember, in C/C++ a function is also a variable 3 #include <iostream> using namespace std; int foo(char, double); double aDouble = 1.0; int main(void) { int anInt = foo(’X’,aDouble); cout << anInt << endl << aDouble << endl; } int foo(char c, double d) { if (c == ’X’) aDouble = 0.0; else { double aDouble = 2.0; d = aDouble; } } What will be the output? What is the scope of each variable? 4 Stack vs Heap Static variables are allocated on the stack inside the defined scope int x; double *p; char s[10]; Memory occupied by static variables are automatically freed (removed from the stack), when they are out of scope Each function’s static parameters and local variables are placed in the stack, and removed at program termination Dynamic variables have memory allocated on the heap • use new operator char *s = new char [10]; int *x = new int; Memory of dynamic variables remains allocated until deallocated delete s; delete x; Remember: no garbage collector 5 Values stored at these memory locations still exist: char *s = new char[10]; strcpy(s,"hello"); //why not: s = "Hello"? delete s; cout << s << endl; Therefor, rather delete s; s = 0; Memory leaks: int *x = new int; int *y = new int; x = y; 6 Address Arithmetic Examples of address specifications using the address operator & • used to denote an address • assume char s[50]; then valid address references are: s &s[0] s+i //i is an integer &s[i] 7 The indirection operator * • used to denote the object at the given address • assume int a[10]; then *a <==> a[0] *(a+i) <==> a[i] • equivalent statements for (i=0;i<10;i++) cout<<a[i];...
View Full Document

This note was uploaded on 02/27/2012 for the course IT 101 taught by Professor Unsure during the Spring '11 term at University of South Africa.

Page1 / 29

Revision - Revision of Some C++ Concepts Arguments to C++...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online