Day 7 Flashcards
What is Smatch?
Allows access to string results
What is regex_search()?
Used to determine a match in a target sequence. Returns a Boolean value of true if there was a match, and false if there was no match.
What are the REGEX parameters?
Target sequence (subject) Regular expression (pattern) Matches: return matched data using str()
What does () mean in regard to regular expressions?
Creates a group that may be referenced by a number
What does {} mean in regard to regular expressions?
Specifies the number of times to repeat the previous character
What does \d mean in regard to regular expression?
Shortcut character class that matches any digit character [0-9]
What does \w mean in regard to regular expression?
Shortcut character class that matches any word character [A-Z a-z 0-9]
What does \s mean in regard to regular expression?
Shortcut for character class that matches any whitespace character [\t\n]
What does + mean in regard to regular expressions?
Shortcut that matches one or more of the previous character
What is Regex?
The header file for Regular Expressions. A string that describes data. Defines search patterns. Used for pattern matching, extracting specific info from a document, etc.
What is an Array?
- A memory structure that groups data of the same type in adjacent memory locations in the stack.
- After declared, cannot be changed in size (makes it non-dynamic)
- Special pointer that always points to the base address of the array
- The size of an array is the address of index 0.
What are the [] used for with an array?
Used to indicate the initial array size using an element count.
Can never hold more elements than the initial number inside the brackets.
Analyze: Float grades[8] = {89.5, 75.3, 90.6, 97.8, 62.2, 78.4, 80.7} (0-7)
There are 6 grades, but one not listed is set at 0.0 (this is the last index 7). Therefore there are 8 elements. (One element will be uninitialized)
How do you change the value of the last position available with the array float grades = {89.5, 75.3, 90.6, 97.8, 62.2, 78.4, 80.7, 0.0}?
Indicate the last position as grades [7] = 100.0. This changes the last value 0.0 to 100
What would cause a buffer overflow with the array
Float grades[8] = {89.5, 75.3, 90.6, 97.8, 62.2, 78.4, 80.7?
If the programmer tried to write the value index to eight (not to be confused with elements). Looks like grade[8]. This would reference a ninth element with an array with only enough space for 8 elements..
How do you record the byte size of an array?
int vals[10] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
cout «_space;sizeof( vals ) «_space;” Bytes”
• When you know the number of elements, you multiply that by 4.
• Remember, that each byte of an int or float is 4.
• Therefore, in this problem, if there are 10 elements you would do 4x10 to get the total bytes in the array.
• “sizeof()” is the indicator to execute this.
What is the variable name of an array?
• It is a special pointer. Its value is the address of the first element of the array.
• Contiguous or adjacent
• Example:
o 1. float grades[10] – {10,20,30,40,50,60,70,80,90,100}
o 2. Float *gradeptr = &grades;
o 1. float grades[10] – {10,20,30,40,50,60,70,80,90,100}
o 2. float gradeptr = &grades[0]
*notice that *gradeptr got the value of 0, which is the index number of 10 the first element.
What is Pointer Arithmetic?
• Since pointers have addresses and values, simple math within the array can manipulate the data at which they point.
• note in the example that the pointer will have a value of 0 because it will hold the value of the first element which is 10.
• For example:
o int grades[10] = {10,20,30,40,50,60,70,80,90,100}
o int grade = *(grades + 2) because the pointer value WAS 0, when we add 2 it now is 2 and points to the 3rd element: 30.
o Int grade = *(grades +6) same concept, we have to find the element that holds the value of 6. This is the 7th element: 70.
THIS IS NOT A QUESTION JUST A NOTE:
In a question when it says memory location and inputs something like &1000, you have to add 4bytes per index you go through.
Example taken from etc:
• If the base address of the array vals is &1000; what memory location does valsPtr hold at the end of the following code? Int vals[10] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
int *valsPtr = &vals[2];
Answer explained:
o The question wants you to go to the index of 2, indicated by &val[2] (which is 15). Given the “vals” address given in the problem, you will start your address at 1000 and add 4 bytes (the number of bytes in a int or float) until you are at index 2. The answer would be &1008. Let’s say they wanted index of 4…. Then the answer would be &1016.
o Be careful to note whether they are asking for memory location, memory address, or code.
Define: Dynamic Memory Allocation
Allows a programmer to request contiguous blocks of memory from the heap
Common dynamic memory functions
New- allocates enough space for any single built-in or user-defined data, type, class, or structure. Returns a pointer to the allocated memory or nullptr if the allocation failed.
Delete- frees memory allocated with new
New [] – similar to the above except used for arrays. Returns a pointer to the allocated memory or nullptr if the allocation failed.
Delete [] – frees memory allocated with new [] operator.
Memory Leak using the wrong version of delete
How do I know where a variable or value is stored?
Everything that we’ve allocated with keyword is coming out of the heap, but we have to store it somewhere in a pointer and if you’re declaring it with a pointer it’s in the stack.
Value = heap
Variable = stack
example of heap vs stack
EXAMPLE In which segment of memory is the variable whereAmI located? #include using namespace std; int main()
{ int *whereAmI = new int; *whereAmI = 42; cout << whereAmI; return 0; } Stack
The variable whereAmI in the following code points to a value stored in memory. In which segment of memory is the value whereAmI points to stored? #include using namespace std; int main()
{ int *whereAmI = new int; *whereAmI = 42; cout << whereAmI; return 0; HEAP