Week 5 - Gemini Flashcards
(41 cards)
What does it mean for a C-style string to be ‘null-terminated’?
It means the sequence of characters forming the string ends with a special null character (\0
). This null character marks the end of the string and is essential for string manipulation functions to know where the string data stops.
How does the required size of a character array relate to the length of the C-style string it needs to hold?
The character array must be at least one character larger than the string it needs to hold to accommodate the terminating null character (\0
). For example, “Hello” (5 chars) needs a char array of size 6.
How can you initialize a C-style string greeting
with the value “Hello” during declaration in C++ (two common ways)?
-
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
\n2.char greeting[] = "Hello";
(size automatically determined to include\0
)
What is the purpose of the strcpy(s1, s2)
function from <cstring>
? What are potential risks?
strcpy(s1, s2)
copies the string s2
(including the null terminator) into string s1
. Risk: If s1
is not large enough to hold s2
, a buffer overflow will occur, leading to undefined behavior.
What is the purpose of the strcat(s1, s2)
function from <cstring>
? What are potential risks?
strcat(s1, s2)
concatenates (appends) string s2
to the end of string s1
. The null terminator of s1
is overwritten by the first character of s2
, and a new null terminator is added at the end. Risk: If s1
is not large enough to hold its original content plus s2
, a buffer overflow will occur.
What does the strlen(s1)
function from <cstring>
return?
It returns the length of the C-style string s1
, which is the number of characters before the null terminator. The null terminator itself is not counted.
What does the strcmp(s1, s2)
function from <cstring>
return, and what do the return values signify?
It compares two C-style strings s1
and s2
lexicographically. Returns: 0 if s1
and s2
are identical; a value less than 0 if s1 < s2
; a value greater than 0 if s1 > s2
.
What is an ‘array’ in C++? What is a key characteristic of all elements in an array?
An array is a collection of a fixed number of elements of the same data type stored in contiguous memory locations. Each element can be accessed using an index.
How are elements in a C++ array indexed? If an array arr
has 5 elements, what are the valid indices?
Elements are 0-indexed. For an array with 5 elements, the valid indices are 0, 1, 2, 3, and 4.
How do you declare an integer array named numbers
that can hold 10 integers?
int numbers[10];
How can you initialize all elements of an integer array myArray
of size 5 to zero at the time of declaration?
int myArray[5] = {0};
(This specific syntax initializes all elements to 0 if at least one initializer is 0 and others are omitted for aggregate types).
What happens if you initialize an array with fewer values than its declared size, e.g., int myArray[5] = {1, 2, 3};
?
The provided values initialize the first elements (myArray[0]=1, myArray[1]=2, myArray[2]=3), and the remaining elements are value-initialized (for int
, this means they become 0).
When passing an array to a function, what is actually passed? Why is it common to pass the array’s size as a separate argument?
When an array is passed to a function, typically a pointer to its first element is passed. The function does not inherently know the array’s size from this pointer alone. Therefore, the size is passed as a separate argument to allow the function to correctly iterate through the array and avoid accessing memory out of bounds.
Why is it generally problematic or not recommended to directly return a statically allocated local array from a function?
A statically allocated local array exists on the stack and is destroyed when the function exits. Returning a pointer or reference to it would result in a dangling pointer/reference, leading to undefined behavior when accessed by the caller.
What is the relationship between an array name (e.g., myArray
) and a pointer to its first element?
The array name, when used in many contexts (like assignment to a pointer or passing to a function), decays into a pointer to its first element. So, myArray
is often equivalent to &myArray[0]
.
Explain how pointer arithmetic can be used to access array elements, e.g., if int *ptr = myArray;
, how can *(ptr + i)
be used?
If ptr
points to the first element of myArray
, then *(ptr + i)
accesses the element at index i
. The expression ptr + i
calculates the memory address of the i-th element based on the size of the data type ptr
points to.
How do you dynamically allocate an array of n
integers using the new
operator and assign it to a pointer dynArray
?
int *dynArray = new int[n];
How must dynamically allocated array memory (e.g., dynArray = new int[n];
) be de-allocated?
Using the delete[]
operator: delete[] dynArray;
Using delete dynArray;
is incorrect and leads to undefined behavior.
What is the main advantage of using dynamic arrays (allocated with new[]
) over static arrays?
Dynamic arrays allow their size to be determined at runtime (e.g., based on user input or other calculations), whereas the size of static arrays must be a constant known at compile time.
If a function dynamically allocates an array and returns a pointer to it, who is typically responsible for de-allocating that memory? What is a common convention?
The caller of the function is typically responsible for de-allocating the memory using delete[]
. A common convention is that if a function returns a pointer to dynamically allocated memory, the documentation should clearly state that the caller owns the memory and must free it.
How can you iterate through the elements of an array using a pointer and pointer arithmetic instead of array indexing? For an array starting at begin
with n
elements.
int* end = begin + n; for (int* ptr = begin; ptr != end; ptr++) { /* use *ptr to access element */ }
How do you declare a two-dimensional integer array matrix
with 3 rows and 4 columns?
int matrix[3][4];
When declaring a multi-dimensional array (e.g., int arr[][5] = {{1,2},{3,4}}
), which dimensions (if any) can be omitted if it’s initialized?
All dimensions except the first (leftmost) must be specified. The compiler can deduce the first dimension from the number of initializers. So, int arr[][5]
is valid if initialized, but int arr[3][]
is not.
What is the fundamental concept of a ‘class’ in C++ object-oriented programming?
A class is a blueprint for creating objects. It bundles data (member variables or attributes) and functions (member functions or methods) that operate on that data into a single user-defined type.