First half of the final exam expectations

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/105

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

106 Terms

1
New cards

How do pointers work and behave in C?

Pointers are variables that hold memory addresses. When you declare a pointer, you're saying, "I want a variable that stores the location of another variable.

A pointer is a special variable that stores the memory address of another variable.

Imagine your house has an address.You can write your name on a paper (like a regular variable),or you can write your house's address (like a pointer).

So if:

int x = 10; → you’re holding the value 10.

int* ptr = &x; → you're holding the address of the variable x.

2
New cards

How does a pointer differ from a regular variable?

A regular variable stores a value (like the number 10), but a pointer stores the address of where that value is located in memory.

3
New cards

What is the dereference operator (*) used for?

* (dereference operator): Lets you access or change the value stored at the memory address.

4
New cards

What does the address-of operator (&) do?

& (address-of operator): Gives the memory address of a variable.

5
New cards

What types of data can pointers point to?

Pointers can point to different data types, like int, char, float, etc.

6
New cards

In what contexts are pointers commonly used?

Pointers are heavily used with arrays, strings, dynamic memory, and function parameters.

7
New cards

What is memory management in C?

Memory management means controlling how memory is used in your program. In C, you must do this manually.

Think of it like checking books out of a library. You must return them when finished. If not, the library (computer memory) runs out of space.

Memory management is how your C program:

1. Gets memory from the computer to use,

2. Uses that memory correctly,

3. And then gives it back when it's no longer needed.

Since C doesn’t have a garbage collector like Python or JavaScript, you are responsible for memory management.

8
New cards

What is static memory?

Static Memory: Memory allocated (setting aside or reserving) before the program runs (global or static variables).

Static memory refers to memory that is allocated once and stays for the entire life of the program.

You don’t need to free it, and it doesn’t disappear when a function ends.

Imagine you have a tool that you always need in your toolbox, no matter what job you're doing.Even if you switch jobs (functions), that tool stays in your toolbox the whole time.

That’s what static memory is — a permanent space in memory while the program is running.

9
New cards

What is stack memory?

Stack Memory: Managed automatically when functions are called. Variables are removed when functions end.

In C programming, stack memory is a special area of memory that stores:

1. Local variables (like int x = 5;)

2. Function parameters

3. Function call information (like return addresses)

It’s called a stack because it works like a stack of plates — Last In, First Out (LIFO):

1. The last function you call is the first one to finish and get removed from memory.

2.It refers to a region of memory where statically allocated variables are stored.

3. These variables persist for the entire lifetime of the program.

10
New cards

What is heap memory?

Heap Memory: Allocated at runtime using malloc, calloc, etc. You must free it with free().

Heap memory is a large pool of memory used for dynamic allocation.

Imagine you’re at a storage facility.

The stack is like a backpack: small, fast, automatic.

The heap is like renting a storage unit: big, slow, but flexible — and you’re in charge of locking/unlocking it.

11
New cards

What is fragmentation in memory?

Fragmentation: When free memory is broken into pieces that are too small to use efficiently.

12
New cards

What is a null pointer?

A pointer that points to nothing. It's used to avoid accidents.

13
New cards

What does malloc(size) do?

Allocates a block of memory.

14
New cards

What does calloc(num, size) do?

calloc(num, size) - Allocates and sets memory to zero.

15
New cards

What does realloc(ptr, newSize) do?

realloc(ptr, newSize) - Resizes a previously allocated block.

16
New cards

What does free(ptr) do?

free(ptr) - Releases allocated memory.

it gives the memory back to the system so other parts of your program (or other programs) can use it.

17
New cards

What are examples of correct memory usage?

1. Always check if malloc or calloc returned NULL before using the pointer.

2. Always free() memory you allocate when you're done with it.

3. Set pointers to NULL after freeing them to avoid accidental use.

4. Avoid memory leaks by freeing all allocated memory.

18
New cards

What are examples of incorrect memory usage?

1. Forgetting to call free() — causes memory leaks.

2. Using free() more than once on the same pointer — double free error.

3. Using memory after it's been freed — use-after-free bug.

4. Using an uninitialized pointer — might point to garbage memory.

4. Dereferencing a NULL pointer — causes a crash.

19
New cards

What is static allocation?

Static allocation means that memory is reserved (allocated) at compile time, and that memory:

1. Stays the same size throughout the program

2. Doesn’t move or change

3. Exists for the entire program run

When you use static allocation, you are telling the computer:

“I know exactly how much memory I need, and I want it ready from the start — and don’t ever take it away until the program ends.”

20
New cards

What is stack allocation?

Stack allocation is when memory is automatically set aside for a variable when a function is called, and automatically freed when the function ends.

It uses a part of memory called the stack.

Think of the stack like a stack of plates:

1. You add (push) a plate when a function starts.

2. You remove (pop) a plate when the function ends.

Each function call gets its own section of memory on the stack to store:

Local variables

Function parameters

Return addresses

21
New cards

What is heap allocation?

Heap allocation means your program manually asks the system for memory at runtime, and that memory is taken from a region called the heap.

You control both the request for memory and the release of it.

Unlike stack allocation, the system won’t clean it up for you — you must use free() when you're done.

22
New cards

What is a memory leak?

A memory leak happens when your program:

Allocates memory (using malloc(), calloc(), or realloc())

But never frees it (using free())

That memory stays reserved forever during the program’s run — even if you don’t use it anymore.

23
New cards

What is use-after-free?

A use-after-free happens when your program:

Frees memory with free(), But still tries to use that memory — like reading from it or writing to it.

That memory no longer belongs to you — using it is dangerous and causes undefined behavior, which can lead to:

1. Crashes

2. Corrupted data

3. Security vulnerabilities

24
New cards

What is a double free?

A double free error happens when you call free() on the same pointer more than once.

In simple terms:

You already gave memory back to the system — and now you’re trying to give it back again, which doesn’t make sense and can crash your program.

25
New cards

What is NULL dereference?

NULL Dereference: You tried to get data from a memory address that doesn't point to anything.

26
New cards

What is a dangling pointer?

A dangling pointer is a pointer that used to point to valid memory, but that memory has since been freed, deleted, or gone out of scope — yet the pointer still holds the old (now invalid) address.

You’re holding onto a dead memory address, and trying to use it is dangerous.

27
New cards

What are the characteristics of stack memory?

1. Memory is automatically managed.

2. Very fast.

3. Used for short-term things like function calls.

28
New cards

What are the characteristics of heap memory?

1. Manually managed using malloc, free, etc.

2. More flexible.

3. Used for long-term or large data.

29
New cards

What is Linux?

Linux is an operating system — just like Windows or macOS.

But more specifically:

Linux is a family of free, open-source operating systems based on the Linux kernel (the core part that talks to your computer's hardware).

30
New cards

What is the root directory in Linux?

/ — Root directory: the starting point for everything.

31
New cards

What is the purpose of /home in Linux?

/home — Your personal folder where your files live.

Think of /home as the “Users” folder — it holds your private files, settings, and documents.

32
New cards

What is stored in /bin or /usr/bin?

/bin and /usr/bin store command programs.

33
New cards

What does /dev represent?

/dev is short for devices and contains files that act like hardware (e.g., keyboards, USB).

34
New cards

What is found in /proc?

/proc contains information about your system and processes.

35
New cards

What are the read permissions in Linux file permissions?

Read (r) allows a user to open and view a file.

36
New cards

What are the write permissions in Linux file permissions?

Write (w) allows a user to modify a file.

37
New cards

What are the execute permissions in Linux file permissions?

Execute (x) allows a user to run a file like a program.

38
New cards

What does -rw-r--r-- mean?

The owner can read/write; others can only read.

39
New cards

What is a process in Linux?

A running program. Each one has a PID (Process ID).

40
New cards

What is stdin?

What you type (keyboard).

41
New cards

What is stdout?

What prints out (screen).

42
New cards

What is stderr?

Error messages.

43
New cards

What does the ls command do?

ls stands for “list”, and it’s used to list the contents of a directory — like viewing files and folders.

44
New cards

What does 'cd folder' do?

Go into that folder.

Change your current working directory to the folder named folder

45
New cards

What does 'pwd' do?

pwd stands for "print working directory".

It tells you where you are in the filesystem — your current directory in the terminal.

46
New cards

What does 'chmod' do?

chmod stands for change mode.

It’s used to change the permissions of a file or directory — that is, who can read, write, or execute it.

47
New cards

What does 'chown' do?

Change who owns a file.

48
New cards

What does 'man command' do?

Get help about a command.

49
New cards

What does mkdir do?

mkdir stands for “make directory”.

It’s used to create a new folder (directory) from the terminal.

50
New cards

What does rm do?

rm: Delete files.

51
New cards

What does > do in Linux?

Take output and write it to a file (replace old content)

> takes what would normally show up in the terminal and puts it into a file instead. If that file already exists its contenets will be overwritten.

52
New cards

What does >> do in Linux?

The >> operator is used to append output to a file.

It adds new content to the end of a file without deleting what’s already there.

53
New cards

What does < do in Linux?

The < operator is used for input redirection.

It tells a command to read input from a file instead of from the keyboard or terminal.

54
New cards

What does | do in Linux?

The pipe (|) takes the output of one command and sends it as input to another command.

It lets you chain commands together, so you can process data step-by-step.

55
New cards

Why are redirection and pipes useful?

These tools let you chain commands and build powerful workflows right in your terminal.

1. Redirection lets you send output to a file, read input from a file, or append to a file.

2. Pipes let you connect commands, so the output of one becomes the input of another.

56
New cards

What is File I/O in C?

File I/O (File Input/Output) in C allows programs to interact with files on disk—reading information from them or saving output to them. Files are essential in almost all real-world applications for storing data persistently.

57
New cards

What determines File I/O behavior?

1. The mode in which the file is opened (read, write, append, etc.)

2. Whether you're using low-level system calls or high-level standard library functions

3. The current position of the file pointer

4.The operating system's file permissions and structure

58
New cards

What does a read operation do in File I/O?

A read operation pulls bytes from the current position forward.

59
New cards

What does a write operation do in File I/O?

A write operation places bytes at the current position and advances the pointer.

60
New cards

What happens when a file is closed?

Any remaining buffered data is flushed and the system releases the file handle.

61
New cards

What is buffering in the context of File I/O?

Buffering is the process of temporarily storing data in memory (a buffer) before reading from or writing to a file or device.

A buffer is just a small chunk of memory (like a container) used to hold data temporarily to make input/output (I/O) operations faster and more efficient.

62
New cards

How does buffering improve read/write operations?

For reads, it loads large chunks of data into memory at once and serves small pieces from fast RAM.

For writes, it collects data in memory and writes it to disk in fewer, larger operations. This makes I/O faster, more efficient, and reduces system calls.

63
New cards

What is EOF in File I/O?

It is a special condition that indicates no more data can be read from a file.

When you’re reading a file, functions like fgets(), fscanf(), or read() will keep going until they hit EOF — meaning the end of the file has been reached.

64
New cards

Why is it important to check for EOF in File I/O?

Because EOF (End Of File) tells your program:

“There’s no more data to read.”

If you don’t check for EOF, your program might:

1. Try to read past the end of the file

2. Get invalid or garbage data

3. Enter an infinite loop

4. Cause errors or crashes

65
New cards

What should you do after performing file operations?

Always check the return value to ensure it succeeded.

You should always close the file using fclose().

66
New cards

What should you check if a file operation fails?

You should check whether the file pointer or file descriptor is NULL or -1, and also examine the errno variable for detailed error info.

67
New cards

What does the open() function do?

open() opens a file and gives you a file descriptor.

68
New cards

What does open() return on success?

Returns a file descriptor (non-negative integer).

69
New cards

What does open() return on error?

Returns -1.

70
New cards

What does read() do?

The read() system call is used to read raw bytes of data from a file or device into your program’s memory.

71
New cards

What does read() return on success?

Returns number of bytes read.

72
New cards

What does read() return on error?

Returns -1.

73
New cards

What does write() do?

The write() system call is used to write raw bytes from your program’s memory to a file or output device.

74
New cards

What does write() return on success?

Returns number of bytes written.

75
New cards

What does write() return on error?

Returns -1.

76
New cards

What does close() do?

Closes the file.

77
New cards

What does close() return on success?

Returns 0.

78
New cards

What does close() return on error?

Returns -1.

79
New cards

What does lseek() do?

lseek() is used to change the current read/write position (also called the file offset) inside an open file.

It tells the operating system:“Move to a different spot in the file before reading or writing.”

80
New cards

What does lseek() return on success?

Returns the new offset (position in the file).

81
New cards

What does lseek() return on error?

Returns -1.

82
New cards

What does file mode "r" mean?

"r": Read-only. File must already exist.

83
New cards

What does file mode "w" mean?

"w": Write-only. Deletes old content. Creates file if needed.

84
New cards

What does file mode "a" mean?

"a": Append. Adds to end. Creates if needed.

85
New cards

What does file mode "r+" mean?

"r+": Read and write. File must exist.

86
New cards

What does file mode "w+" mean?

"w+": Read and write. Clears file if it exists, else creates new.

87
New cards

What does file mode "a+" mean?

"a+": Read and write. Always adds to end. Creates if needed.

88
New cards

What are best practices for File I/O?

1. Always check if the file opened successfully (check for NULL).

2. Close your files using fclose().

3. Handle edge cases like missing files or permission errors.

4. Don't forget to flush output if needed.

5. Read and write in chunks to be efficient.

89
New cards

What does fopen() do?

fopen(): Opens a file and gives you a FILE pointer

90
New cards

What value does EOF return in C?

EOF returns -1. It indicates the end of a file or an input error.

91
New cards

What does fputs() do?

fputs(): Write a line to a file.

92
New cards

fopen return on success

Success: Returns a file pointer

93
New cards

fopen return on error

Error: Returns EOF or null (cant be opened)

94
New cards

fseek return on success

Success: Returns 0

95
New cards

fseek return on error

Error: Returns non-zero or -1

96
New cards

What does fseek() do?

it is used to "go somewhere" in the file, like a cersur

97
New cards

fclose return on success

Returns 0

98
New cards

fclose return on failure

Returns EOF or -1

99
New cards

What does fclose() do?

closes the file

100
New cards

What is a file pointer?

A file pointer is a special pointer of type FILE* that is used to access and control a file in C.

It represents an open file and keeps track of where you are in the file (the current read/write position

A file pointer is like a bookmark in a file — it tells the system which file you’re using and where you are inside it.