General Course Flashcards
What is the main difference between C and C++
C++ is built on C. C doesn’t have classes, something which C++ does have
What is the size of the void type?
Void returns no size.
What are the sizes of:
- Char
- Short
- Int
- Long
- Char (8bit)
- Short (16bit)
- Int (32bit)
- Long (64bit)
What would “A” and “*A” Give respectively?
int i = 5;
int *A = &i;
A = the address of i *A = The value at the address location A
printf(“Address of i is %p, value of i is %d\n”, pointer_to_i, *pointer_to_i);
What is thr purpose of the ‘*’?
The ‘*’ dereferences the pointer getting the value at the location.
#define ARRAY_LENGTH 10 What does #define do? What is ARRAY_LENGTH?
define is a preprocessor. This means that before compile time all uses of the keyword ARRAY_LENGTH are swapped for the value 10.
intptr =(int) some_array;
printf(“value at index 5 is %d\n”, ptr[5]);
printf(“value at index 5 is %d\n”,________);
What is another way of accessing the index 5 using a pointer.
You can add bytes to the pointer so:
*(ptr + 5)
What is dangerous about this code?
int some_array[10]; for(int i =0; i <10; i++){ some_array[i]= i *2; } printf("value at index 5 is %d\n", some_array[5]);
char ptr =(char) some_array;
printf(“value at index 2 is %d\n”, (int) *(ptr +2));
printf(“value at index 2 is %d\n”, (int) ptr[2]);
printf(“value at index 2 is %d\n”, ((int) (&ptr[2])));
The line ‘char ptr =(char) some_array; ‘. As the array is defined as an int accessing it as a char could return a nonsense value making it unreliable.
NOTE: This will probably not crash the program but its not advised.
Is this valid code?
int i;
int*pointer_to_i =&i;
int**pointer_to_pointer_to_i=&pointer_to_i;
Yes! you can have pointers to pointers. Remember that pointers also live in memory!
pointer_to_pointer_to_i == pointer_to_i**pointer_to_pointer_to_i ==pointer_to_i == i
What are the two main file types and their function in C and C++?
Header files:
-Header Files-.h .hpp-Defines function signatures upfront.
-Other files (both .h and .c) #include the header files so they know what functions and types will be available.
Implementation Files
-.c .cc .cxx .cpp-A single .c file within the whole multi-file program will provide an implementation of the functions that were promised.
-Everything must be implemented exactly once across all the implementation files.
-Implementing in the header file would inherently duplicate the implementation to everywhere it is included.
What are the two phases in ‘Two-Phase Compilation’?
Compiling:
- Compile each C/C++ file into an object file
Linking:
- Link multiple .o (object files) into a single executable.
- Everything in a header file that has been used must be given an implimentation in exactly one .o file.
Will the code written on a Windows OS run on a Mac or Linux OS?
Not necesserily. Differences in memory management and standard libraries mean the program may compile and run but not as intended.
It is good practice to compile on the OS you intend to run it on using that native compiler.
What are the 5 regions of memore?
- Text
- Initilized Data Segment
- Uninitilized Data Segment
- Stack
- Heap
What is the Text Segment of memory used for? What is the size of this segment?
It is an array of ‘Machine words’ one for each instruction in the program. The size of this segment is the number of the instructions * 32/64 depending on the architecture of the machine. (The size is know at compile time)
What is the Initilized Data Segment used for? What is its size?
Global and Static variables with an initilized value , know at compile time.
The size of this segment is the total size of all initilized variables. It is know at compile time.