Data Allocation Flashcards
pointer
variable that holds a memory address, rather than holding data. Indicated by a *
don’t declare multiple pointers on one line
must be declared to another variable of same type, not just to a value
pointer’s reference operator
&, obtains variable’s address
dereference operator
prepended to a pointer’s variable name to retrieve that data to which the pointer variable points
*valPointer = 10; // changes someInt to 10
NULL means
nothing
when checking if a pointer is NULL, only use the value; don’t use a deference pointer
Syntax errors
Occurs when there is an error in the code that prevents the program from compiling
Runtime Error
Program is running but encounters an issue with data, that compiler couldn’t predict during compilation
calloc
allocates a block of memory for an array of elements, initializing all bits in the allocated memory to zero
malloc(bytes)
specifies the number of bytes to allocate in a memory. found in stdlib.h library. returns a void pointer of memory address to the memory location. returns null if function failed to allocate memory (all memory is used up)
(double*)malloc(sizeof(double));
The double above converts the call from a void to a double returner, but not necessary because it automatically does this in C.
how many bits in an int?
32 bits, 4 bytes
free(pointerVariable)
deallocates a memory block pointed to by a given poitner=
Memory Leak
Occurs when a program allocates memory but loses the ability to access the allocated memory.
Garbage collection
Automatic process of finding and freeing unreachable allocated memory locations
Unusable Memory
Memory locations that have been dynamically allocated but can no longer be used by a program.
strchr(sourceStr, searchChar)
Returns NULL if searchChar does not exist in sourceStr, Else, returns pointer to first occurrence
strrchr(sourceStr, searchChar)
Returns NULL is searchChar does not exist in sourceStr. Else, returns poitner to LAST occurrence (searches in reverse)
strstr(str1, str2)
Returns NULL if str2 does not exist in str1. Else, returns a char pointer pointing to the first character of the first occurrence of string str2 within string str1.
Dynamically Allocated Array
determining the total number of bytes needed to store the desired number of elements, using the following form:
pointerVariableName = (dataType*)malloc(numElements * sizeof(dataType))
Statically allocated array
declare arrays to have a fixed size
fgets
name, size, how (stdin)
strcat
concatenates the string at the end of a given array
1 byte = x bits
8 bits (wide), which is the amount of data stored at a single address
True or False: In this example, the scalar variables were arranged in memory in the order in which they were declared
False
True or False: Scalar variables of the same data type were packed together.
True
True or False: For each of the two array variables, the first element is adjacent to the second element in memory
True