1/19
Flashcards covering pointers, memory regions (stack vs. heap), struct access, and string handling from the Week 2 CSC lecture.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What are the two parameters passed to the main function in C?
The parameters are int argc, which is the count of arguments, and char **argv (or char *argv[]), which is the argument vector representing an array of strings.
What is the first element of the argv array (argv[0]) always assigned to?
The first element argv[0] is the name of the program that is being run.
Why is strtol() often preferred over atoi() for converting strings to integers?
strtol() allows the programmer to check if the string contained any digits and if there were any remaining characters after the number, providing better error checking.
In C, what does a pointer represent?
A pointer is a value that represents a memory address indicating where a piece of data can be found.
How do you acquire the memory address of a variable in C?
You use the ampersand operator &, which is the "address of" operator (e.g., &var).
What is the purpose of the dereferencing operator *?
The dereferencing operator is used to follow a memory address to access or modify the actual value stored at that location.
What does malloc() return, and what does this return type signify?
malloc() returns a void * (void pointer), which is a generic pointer to any type and should be cast to the specific type intended for use.
What is a segmentation fault?
A segmentation fault occurs when a program attempts to access a memory location it does not have permission to access, such as address 0 (null).
What are the primary differences between memory on the stack versus memory on the heap?
Stack memory is used for local function variables and is reclaimed once a function returns; heap memory is allocated dynamically via malloc(), is persistent for the lifetime of the program, and typically offers much more space (gigabytes vs. megabytes).
What is a memory leak?
A memory leak occurs when a program allocates memory on the heap but loses access to the pointer before calling free(), making that memory unreachable until the program terminates.
Why is the sizeof() operator used in conjunction with malloc()?
sizeof() determines the number of bytes for a specific type (e.g., sizeof(int)), ensuring malloc() allocates the correct amount of memory regardless of the system's architecture.
If an integer is 4 bytes and p is an int * pointing to address 1024, what is the address of p + 1?
The address would be 1028, because C automatically increments pointers in multiples of the size of the underlying data type.
What special character must terminate every valid string in C?
Every valid string must end with the null terminator, represented as \0 (which is numeric value 0).
When using strlen() to find the length of a string, how is the null terminator handled?
strlen() counts the characters in the string up to, but not including, the null terminator.
Under what circumstances should memmove() be used instead of memcpy()?
memmove() should be used when the source and destination memory buffers overlap, as it safely handles the transfer using a temporary buffer.
How does C pass parameters to functions?
All C function parameters are passed by value, meaning the function receives a copy of the argument's value rather than the original variable itself.
In the declaration int *x, y;, what are the resulting types of x and y?
x is a pointer to an integer (int *), but y is a standard integer (int), because the * symbol only applies to the variable directly to its right.
What is the benefit of using the arrow operator -> when working with structs?
The arrow operator -> is a shortcut that dereferences a pointer to a struct and then accesses its member (e.g., p->length is equivalent to (*p).length).
What happens to memory freed via the free() function?
The memory is released back to the system, and any pointers still holding that address become invalid; using them again causes a "use after free" bug.
What is the significance of the NULL macro in C?
NULL is effectively zero and is used to represent a pointer that does not point to any valid memory address, serving as a safe way to initialize or check pointers.