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
True or False: The result of incrementing an int pointer by 1 is the next sequential address in memory.
False.
Adding 1 to a pointer variable increments it by the size of the variable type, so that it points to the next consecutive value of that type. Since an int is 4 bytes, it takes 4 consecutive addresses to store an int, so incrementing an int pointer by 1 adds 4 to the address.
Code
The region where the program instructions are stored
Static Memory
The region where global variables (variables declared outside any function) as well as static local variables (variables declared inside functions starting with the keyword “static”) are allocated. Static variables are allocated once and stay in the same memory location for the duration of a program’s execution
The Stack
The region where a function’s local variables are allocated during a function call. A function call adds local variables to the stack, and a return removes them, like adding and removing dishes from a pile. Because this memory is also automatically allocated and deallocated, it is also called automatic memory.
All non-static, local variables inside functions are located on the stack.
pass by pointer / pass by reference
used to return two values, by putting a * before a parameter name and & before the corresponding argument variable
(*structPtr).membername is equivalent to
structPtr->membername;
True or False: Like Python, C has a garbage collector that automatically frees dynamically allocated memory when it is no longer referenced in the program.
False.
In C programs, dynamically allocated memory must be explicitly deallocated using the free library call.
True or False: Static variables are called “static” because their values can’t be reassigned
False.
Static variables can be assigned new values and retain their values between function calls.
Number of Bytes a char stores:
1
number of bytes for an ip address
8
number of bytes for string
1*N
Pointer to a variable goes into the….
heap
number of bytes for char pointer
4 bytes
purpose of sizeof()
counts number of bytes
number of bytes in a double
8