General Course Flashcards

1
Q

What is the main difference between C and C++

A

C++ is built on C. C doesn’t have classes, something which C++ does have

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the size of the void type?

A

Void returns no size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the sizes of:

  • Char
  • Short
  • Int
  • Long
A
  • Char (8bit)
  • Short (16bit)
  • Int (32bit)
  • Long (64bit)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What would “A” and “*A” Give respectively?
int i = 5;
int *A = &i;

A
A = the address of i
*A = The value at the address location A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

printf(“Address of i is %p, value of i is %d\n”, pointer_to_i, *pointer_to_i);

What is thr purpose of the ‘*’?

A

The ‘*’ dereferences the pointer getting the value at the location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
#define ARRAY_LENGTH 10
What does #define do?
What is ARRAY_LENGTH?
A

define is a preprocessor. This means that before compile time all uses of the keyword ARRAY_LENGTH are swapped for the value 10.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

You can add bytes to the pointer so:

*(ptr + 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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])));

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is this valid code?

int i;
int*pointer_to_i =&i;
int**pointer_to_pointer_to_i=&pointer_to_i;

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the two main file types and their function in C and C++?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the two phases in ‘Two-Phase Compilation’?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Will the code written on a Windows OS run on a Mac or Linux OS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the 5 regions of memore?

A
  • Text
  • Initilized Data Segment
  • Uninitilized Data Segment
  • Stack
  • Heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the Text Segment of memory used for? What is the size of this segment?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Initilized Data Segment used for? What is its size?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Uninitialized Data Segment used for? What is its size?

A

Origionally refered to as BSS “Block Started by Symbol”.
Global and static variables without default values known at compile time.
-This region of memory is set to all 0 bytes by the Operating System before the program begins execution.

Size of segment is the total size of all variables within the segment, known at compile time.

17
Q

What is Stack Memory? What is its size?

A

Variables are created and removed from the stack automatically as your program executes.
-A stack is a data structure with LIFO (Last In First Out) behavior.

Entering a function pushes a “Stack Frame” onto the stack.

Size of segment is fixed size known at execution time.

- Operating System specifies the amount of stack space when it launches the process for the program. 
 - Default size is OS and hardware dependent, usually 8MB
18
Q

Can the stack memore change in size? Does the OS give this all at once?

A

The OS does not have to give all of stack memory at once. if there was 8MB per process and 1000 processes that would be 8GB of RAM!

The OS can grow the stack as needed up to the know limit. Our program thinks all Stack memory is there for execution purposes.

19
Q

What are three ways we can run out of stack memory?

A

If we make large variables or a lot of variables within a function on the stack

If we recursively call a function or functions many many times deep each call adds a stack frame onto the stack
-There are only so many times we can do that before running out of space.

The combination of the previous two can amplify the individual effects greatly.

20
Q

Heap memory is allocated by the programmer.

True or False?

A

TRUE!
The programmer can, in theory allocate as much memors as is available on the system… we could…But why would we want to do that?