Regular C/C++ Knowledge Flashcards

1
Q

When should you use a pointer in C/C++ program?

A
  1. Any time you share, acces or modify data other than local variables in C/C++, you are going to need to use a pointer.
  2. In scenarios where you wish to pass a large chunk of memory through a function argument.
  3. You do not know the address of a memory while compiling the program.
  4. You would like to change a variable across files and one good practice is to send that variable as a pointer and get modified in called function.
  5. When you have to write a specific address like in embedded controllers such as 8051 or ARM Micro Controllers.
  6. Callback Functions
  7. Linked lists, where the devices are getting attached and removed at run time (USB Devices to a Laptop/Desktop).
  8. You want to access one data type in another data type format. Example: You have 256 Bytes of memory, but would like to process 4 Bytes at a time while computation time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between Static and Dynamic Memory Allocation in C?

A

Static Memory Allocation is a space in memory already created by the compiler in advance for variables that will be used, and Dynamic Memory Allocation is a way where memory can be allocated for variables during the run-time inside of the program.

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

What is Recursion?

A

The process in which a function calls itself directly or indirectly.

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

What is Stack Overflow error?

A

It is when the program tries to utilize more space than available when running a program and typically results in crashes.

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

What are some disadvantages of recursive programming as oppose to iterative programming?

A

Recursive programming has greater space and time requirements than iterative programming since all of the functions will remain in the stack until the base case is reached and the time it takes for the function calls and returns.

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

What are some advantages of recursive programming as oppose to iterative programming?

A

Recursion provides a clean and simple way to write code. Some problems are inherently recursive like tree traversals, which are preferred to write recursive code.

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

How many bytes does an “int” take up?

A

4 bytes of memory space and ranges from “-2147483648 to 2147483647”

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

How many bytes does a “char” take up?

A

1 byte of memory space and ranges from “-128 to 127” or “0 to 255”

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

How many bytes does a “boolean” take up?

A

1 byte of memory space

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

How many bytes does a “float” take up?

A

4 bytes of memory space

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

How many bytes does a “double float” take up?

A

8 bytes of memory space

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

How many bytes does a “short int” take up?

A

2 bytes of memory and ranges from “-32,768 to 32,767”

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

How many bytes does a “unsigned short int” take up?

A

2 bytes of memory and ranges from “0 to 65,535”

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

How many bytes does a “unsigned int” take up?

A

4 bytes of memory space and ranges from “0 to 4,294,967,295”

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

How many bytes does a “long int” take up?

A

8 bytes of memory space and ranges from “-2,147,483,648 to 2,147,483,647”

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

How many bytes does a “unsigned long int” take up?

A

8 bytes of memory space and ranges from “0 to 4,294,967,295”

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

How many bytes does a “long long int” take up?

A

8 bytes of memory space and ranges from “-(2^63) to (2^63)-1”

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

How many bytes does a “unsigned long long int” take up?

A

8 bytes of memory space and ranges from “0 to 18,446,744,073,709,551,615”

19
Q

How many bytes does a “signed char” take up?

A

1 byte of memory space and ranges from “-128 to 127”

20
Q

How many bytes does a “unsigned char” take up?

A

1 byte of memory space and ranges from “0 to 255”

21
Q

How many bytes does a “long double” take up?

A

12 bytes of memory space

22
Q

How many ways can you pass data into a function and what are those ways?

A

There are 2 different ways, “pass by value” and “pass by reference”.

23
Q

What is “Pass by Value”?

A

It is when the function copies the actual value of the argument into the parameter of the function and changes made to the parameter inside the function have no effect on the argument.

24
Q

What is “Pass by Reference”?

A

It copies the address of an argument into the parameter of a function and the address is used to access the actual argument used in the call, meaning the changes made to the parameter affect the passed argument and this is how C simulates “Pass by Reference”.

25
Q

What is the difference between
“int * const x” and
“const int * x”?

A

The difference is that “int * const x” is saying that the pointer itself is constant and cannot change, but you can change the int of which it is pointing to .

“const int * x” means that the data “int” cannot change, but you can always change what the pointer is pointing to.

26
Q

What is malloc?

A

Simplest standard library function that allocates memory at runtime. You specify the number of bytes of memory that you want allocated as the argument and returns the address of the first byte of memory it is allocated to.

27
Q

Scanf vs. Getf

A

Scanf only reads strings up to a space, and gets can read a multitude of strings separated by spaces.

28
Q

Scope of a variable

A

Where the variable can be referenced in a program.

29
Q

Visibility/Linkage of a variable

A

Determines for a multiple-source-file program whether the identifier is known only in the current source file or in any source file with proper declarations

30
Q

Auto Storage Class

A

Is used to declare variables of automatic storage duration. Created when block(function) in which they are defined is entered, exist while block is active and destoryed when block is exited.

31
Q

Why use Auto?

A

A means of conserving memory

32
Q

External Storage Class

A

An extern storage class tells us that a variable is defined elsewhere and not within the same block

33
Q

External Variables

A

A global variable initialized with a legal value where it is declared in order to be used elsewhere

34
Q

Why use Extern?

A

Etern variables can be access between two different files which are part of a large program, like functions contained in separate files can communicate through extern variables.

35
Q

What rule applies to Extern variables?

A

You must first declare the variable outside of any module first and then declare an extern variable in another file.

36
Q

Static Storage Class for variables, functions and global variables.

A

When applied to global variables, the static modifier causes that variable’s scope to be restricted to the file in which it is declared.

When applied to functions, the static function can be called only from within the same files as the function appears.

When applied to local variables, it’s value is kept throughout the entire life-time of the program. Can be used beyond the scope it was initially in.

37
Q

What is a Register?

A

A processor register (CPU register) is one of a small set of data holding places that are part of the computer processor.

38
Q

What can a register hold?

A

It can hold an instruction, a storage address, or any kind of data.

39
Q

What is register storage class used for?

A

The register storage class is used to define local variables that should be stored in a register instead of RAM (memory).

40
Q

What is the pro of using register storage class?

A

Makes use of register variables to be much faster than that of the variables stored in memory during runtime of the program.

41
Q

What should the register storage class be used for?

A
  1. The variables which are most frequently
42
Q

Can you access the address of a variable that is registered?

A

No you can’t, because the value is registered and it doesn’t have an address. UNLESS you have the variable, itself, left as a regular value and the pointer, itself, is a register.

43
Q

Can you use different storage classes together?

A

No, you can’t

44
Q

Can you use register outside of block/method/function?

A

No, must be used inside of them.