Dynamic Memory Allocation
add the size of one row to the address of data, get the address of this, then add the size of the two elements of a row to this
(data + 1) + 2 means
AA[k][j]
AA[k] + j points to
AA[k][0]
AA[k] is the address of
int (*a)[10];
Declares (without allocating) a pointer to an array of int(s). The pointer to the array must be dereferenced to access the value of each element.
int a[10];
Declares and allocates an array of int(s)
p = (int*) malloc(100 * sizeof(int));
a memory space equivalent to "100 times the size of an int" bytes is reserved
dynamic memory allocation
ability of a program to use more memory space at execution time
dynamic memory allocation
ability to use more memory space at execution time
malloc
allocates (distributes) requested number of bytes and returns a pointer to the first byte of the allocated space
calloc
allocates (distributes) space for an array of elements, initializes them to zero and then returns a pointer to the memory
malloc()
allocates the specified number of bytes in memory
a pointer to the first object of the array
an array name without an index represents
yes
are global variables accessible from all parts of the program
array of structures array of pointers
array elements can be
node
basic unit of a data structure, such as a linked list or tree data structure
library routines
c supports allocating and freeing memory dynamically using what
two dimensional array
considered to be a one dimensional array of rows, which are, themselves, one dimensional arrays ex. int AA[MAX][15]
free(ptr);
deallocates (returns) the memory referred to by the pointer so it can be reused
int *a[10];
declares and allocates an array of pointer to int (must be dereferenced individually)
realloc(ptr, num_bytes);
finds space for new allocation (for new process of distributing) copies original data into new space frees old space returns pointer to new space
free
frees previously allocated (distributed) space
make each pointer point to a 1d array of the appropriate size
how do you allocate 2d or higher arrays
stop the code ask again save user data ask for less free up something
how do you handle an error involving malloc
NULL (always check for null when using malloc and calloc)
if malloc and calloc fail they return
dynamically allocated variables
items created during run-time and are allocated (distributed) on the heap
the address of the newly-allocated block of memory
malloc and calloc both return what
at the time of execution
memory space required can be specified when
realloc
modifies the size of previously allocated (distributed) space
void pointer
not originally inc relatively recent addition "generic" pointer
program execution
number of data item keeps changing during
dereference
obtain from (a pointer) the address of a data item held in another location.
n-dimensional array
one dimensional array whose elements are arrays of dimension
ptr = malloc(sizeof(struct node));
returns a pointer to the place in memory returns NULL, if no more memory is available
sizeof(struct node)
returns the size of bytes of the structure
the address of the first row of the array
the address of the beginning of the entire array is the same as
the address of the first element of the first row
the address of the first row of the array is the same as
the first element of the array ex. AA appoints to AA[0]
the array name is a pointer to
the first object in row 0 ex. AA[0] points to AA[0][0]
the array name of a 2d array is a pointer to
*(array_name+row)+column
the general form for getting the address of any element of any row is
malloc and calloc
these two functions both allocate memory
amount of data
this cannot be predicted beforehand
parentheses
to declare a pointer to an array type you must use
segmentation violation (program crash)
trying to use memory after freeing it can cause
function malloc
used to allocate (distribute) memory hold new nodes
function free
used to deallocate (return) memory releases space no longer needed
calloc()
used to dynamically allocate(distribute) arrays
calloc()
variant of malloc() takes the number of "things" to be allocated (distributed) and the size of each thing (in bytes)
calloc() sets all the values in the allocated memory to zeros
what does calloc() do that malloc() doesn't
returns the address of the chunk of memory that was distributed
what does calloc() return
the operating system may use the memory for another program
what does the operating system do when you free some memory
#include <stdlib.h>
what header file do you use to use malloc & free functions
AA[k][j]
what value does this access *(AA[k] + j)
a grid
when we declare a 2d array we can visualize it as
no
when you free memory, the memory is erased or destroyed
global data section
where are global variables allocated in
run-time stack
where are local variables allocated executed in
global data section runtime stack dynamically allocated - heap
where is variable memory allocated in
it is used for applications like free where the block of memory located at some address will be freed without any necessity of defining the type
why is void pointer used