1/105
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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.
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.
What is the dereference operator (*) used for?
* (dereference operator): Lets you access or change the value stored at the memory address.
What does the address-of operator (&) do?
& (address-of operator): Gives the memory address of a variable.
What types of data can pointers point to?
Pointers can point to different data types, like int, char, float, etc.
In what contexts are pointers commonly used?
Pointers are heavily used with arrays, strings, dynamic memory, and function parameters.
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.
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.
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.
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.
What is fragmentation in memory?
Fragmentation: When free memory is broken into pieces that are too small to use efficiently.
What is a null pointer?
A pointer that points to nothing. It's used to avoid accidents.
What does malloc(size) do?
Allocates a block of memory.
What does calloc(num, size) do?
calloc(num, size) - Allocates and sets memory to zero.
What does realloc(ptr, newSize) do?
realloc(ptr, newSize) - Resizes a previously allocated block.
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.
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.
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.
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.”
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
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.
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.
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
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.
What is NULL dereference?
NULL Dereference: You tried to get data from a memory address that doesn't point to anything.
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.
What are the characteristics of stack memory?
1. Memory is automatically managed.
2. Very fast.
3. Used for short-term things like function calls.
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.
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).
What is the root directory in Linux?
/ — Root directory: the starting point for everything.
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.
What is stored in /bin or /usr/bin?
/bin and /usr/bin store command programs.
What does /dev represent?
/dev is short for devices and contains files that act like hardware (e.g., keyboards, USB).
What is found in /proc?
/proc contains information about your system and processes.
What are the read permissions in Linux file permissions?
Read (r) allows a user to open and view a file.
What are the write permissions in Linux file permissions?
Write (w) allows a user to modify a file.
What are the execute permissions in Linux file permissions?
Execute (x) allows a user to run a file like a program.
What does -rw-r--r-- mean?
The owner can read/write; others can only read.
What is a process in Linux?
A running program. Each one has a PID (Process ID).
What is stdin?
What you type (keyboard).
What is stdout?
What prints out (screen).
What is stderr?
Error messages.
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.
What does 'cd folder' do?
Go into that folder.
Change your current working directory to the folder named folder
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.
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.
What does 'chown' do?
Change who owns a file.
What does 'man command' do?
Get help about a command.
What does mkdir do?
mkdir stands for “make directory”.
It’s used to create a new folder (directory) from the terminal.
What does rm do?
rm: Delete files.
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.
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.
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.
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.
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.
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.
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
What does a read operation do in File I/O?
A read operation pulls bytes from the current position forward.
What does a write operation do in File I/O?
A write operation places bytes at the current position and advances the pointer.
What happens when a file is closed?
Any remaining buffered data is flushed and the system releases the file handle.
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.
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.
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.
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
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().
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.
What does the open() function do?
open() opens a file and gives you a file descriptor.
What does open() return on success?
Returns a file descriptor (non-negative integer).
What does open() return on error?
Returns -1.
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.
What does read() return on success?
Returns number of bytes read.
What does read() return on error?
Returns -1.
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.
What does write() return on success?
Returns number of bytes written.
What does write() return on error?
Returns -1.
What does close() do?
Closes the file.
What does close() return on success?
Returns 0.
What does close() return on error?
Returns -1.
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.”
What does lseek() return on success?
Returns the new offset (position in the file).
What does lseek() return on error?
Returns -1.
What does file mode "r" mean?
"r": Read-only. File must already exist.
What does file mode "w" mean?
"w": Write-only. Deletes old content. Creates file if needed.
What does file mode "a" mean?
"a": Append. Adds to end. Creates if needed.
What does file mode "r+" mean?
"r+": Read and write. File must exist.
What does file mode "w+" mean?
"w+": Read and write. Clears file if it exists, else creates new.
What does file mode "a+" mean?
"a+": Read and write. Always adds to end. Creates if needed.
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.
What does fopen() do?
fopen(): Opens a file and gives you a FILE pointer
What value does EOF return in C?
EOF returns -1. It indicates the end of a file or an input error.
What does fputs() do?
fputs(): Write a line to a file.
fopen return on success
Success: Returns a file pointer
fopen return on error
Error: Returns EOF or null (cant be opened)
fseek return on success
Success: Returns 0
fseek return on error
Error: Returns non-zero or -1
What does fseek() do?
it is used to "go somewhere" in the file, like a cersur
fclose return on success
Returns 0
fclose return on failure
Returns EOF or -1
What does fclose() do?
closes the file
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.