CMSC 216 Exam 2 (Notes, Lecture Slides)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Does realloc change the location in memory?

If it can, it will reallocate new_size bytes to the same location, otherwise it allocates new memory space elsewhere in the heap of size new_size

What is the problem with a dangling pointer?

If that memory gets allocated later in the program, you could have 2 pointers accessing and modifying the data there

What lives in the heap in Java? What about C?

Java: arrays and objects C: anything you want

"new" in Java vs malloc/calloc: unable to allocate memory

Java: exception C: return NULL

"new" in Java vs malloc/calloc: return type

Java: reference C: void pointer

do we need to cast when using malloc or calloc?

NO!!!

********************************** What do malloc/calloc return if the requested amount of memory can't be allocated?

NULL

Can we fix a memory leak?

No if nothing points to it, we cannot call free on it... If we are in a loop this could cause the heap to fill up

int ^f(void){ int x = 42; return &x; } is this safe?

No returning a pointer to something that will disappear when the function exits?

with realloc, do we have to worry about freeing the old space if new space is allocated?

No, it is handled for us

What should a source file depend on?

Nothing (the save button)

free(NULL) is this valid?

yes it just does nothing

typedef struct { enum month_name month; int day; int year; } Date; Date today, yesterday; Is this legal? Do we need a tag?

yes legal we do not need a tag

s1 and s2 are structs... s1 = s2 is this legal?

yes... it copies all elements over

int ^p = NULL; p = malloc(sizeof(^p)); is this valid?

yes... sizeof does not follow the pointer

What does segmentation fault typically mean?

We are accessing memory our process is not supposed to access

Why is Java garbage collection bad for some applications?

We never know when it will kick in

When would we not need a tag for a struct?

When we are not going to declare other structs of that same type later on in the program

What does realloc return?

a pointer to the updated memory location or NULL if memory could not be allocated

parameter of malloc/calloc

allocates size *bytes* of memory from the heap (if enough is available)

main.x needs to be (re)created if

any object files compiled to create it are changed

Why can't we perform array assignment... E.g. arr1 = arr2

because arr1 is a *constant* pointer to the first element

Why is a stack used to store local variables?

because it has the same property as function calls (FIFO)

Why do we not need to cast the return values of memory allocation functions?

because they return void pointers

In what order should you declare variables in a struct?

declare the small variables like char's last

print and watch variable x as you step

display x

Memory from the heap is ___________ allocated memory

dynamically

T/F free changes the value of the pointer its called on

false

T/F structs can be directly compared

false

T/F the size of a structure is the size of the sum of the sizes of its fields

false

T/F you can call free on a pointer pointing somewhere that wasn't dynamically allocated

false this is an error

T/F free() changes the value stored in memory

false, it only frees it

T/F free() is a garbage collector

false... it only frees one thing

What is a memory leak?

forgetting to free memory

create an executable program called pgm1 to debug pgm1.c

gcc -o pgm1 pgm1.c -g

start the debugger on pgm1

gdb pgm1

why should we do malloc(sizeof(^p)) rather than malloc(sizeof(int))

if we change the type of p, the expression does not have to be modified

show all things you can print

info

which is better to use info locals or info variables

info locals

What happens in STRCPY if the second string is not properly nul terminated?

it can manipulate other parts of memory

T/F free sets a pointer to NULL. Why can or can't it do this?

it can't because it is passed by value so we can free the data but we can't change the pointer

What does -> do?

it dereferences a pointer to a structure and refers to one field of the structure the pointer points to

After we allocate memory, we should always check that

it is not NULL

Date ^dp = malloc(sizeof(Date)); dp -> month = OCT; dp -> day = 3; dp -> year = 2019; free(dp); dp -> day = 4; What is this?

it uses a dangling pointer

Object file should depend on

its own associated source file and any files #include'd

list in debugger

list l

go to line 11

list 11

we must change main.o if _________ or any _________ files are changed

main.c, or #included

utility that automates the process of building programs

make

int ^f(void){ int x = 42; return &x; } Quick fix?

make x static

Which has precedence over the other... makefile vs Makefile

makefile has precedence over Makefile

What memory allocating function initializes memory? which doesn't?

malloc does not initialize memory, calloc does

________________ is when memory is not freed, and nothing points to it anymore

memory leak

return type of malloc/calloc

void pointer

p -> day is equivalent to

(^p).day

How to get number of elements in array?

(int)(sizeof(arr)/sizeof(arr[0]));

int arr[5] = {3, 5, 7, 9, 13}; int ^p = &arr[0]; printf("%d %d\n", (int)sizeof(arr), (int)sizeof(p));

20 8

How big is a pointer?

8 bytes/ 64 bits

Why can we assign structs but not assign arrays?

C does not know how many bytes to copy with arbitrarily sized arrays, but we know the size of a struct so it does not need to worry about the sizes of the elements inside

What should an executable file be dependent on?

All the object files that have been linked together to create it

What should we do if an allocated memory region is the wrong size?

Allocate new space of the right size. Copy the data from the old space to the new space. Free the old space. Make the pointer point to the new space.

typedef struct { enum month_name month; int day; int year; } Date; How can we initialize a struct of this type?

Date today = {OCT, 1, 2019};

s1 and s2 are structs... s1 == s2 is this legal?

no

typedef struct { int d; double f; S2 *s; } S2; is this legal?

no!

show the current variables in existence

p print

Why is user managed heap better and why is it worse compared to garbage collection?

pros: faster cons: more work for programmer, more prone to errors

What function can we use if the allocated memory region is the wrong size?

realloc

what does pipe do? |

redirects the output of one command to input of another command

What happens if an initializer has too few values?

remaining fields are initialized to 0

since free() does not change its parameter pointer, what should we do after freeing the memory?

set the pointer to NULL right after the call (or point it somewhere else)

begin execution with debugger

start/run

What C library has functions for allocating and deallocating memory

stdlib.h

step in debugger

step s

struct Chipmunk { int age; float weight; }; How to declare a variable of this type?

struct Chipmunk chippy;

struct tag form declaration for Chipmunk with int age, float weight

struct Chipmunk { int age; float weight; };

What happens if an initializer has too many values?

syntax error

struct tag { member-list; } variable-list; Which are optional?

tag and variable-list depending on the form of the definition

What is the benefit of using a typedef for a struct ?

to not have to use the word struct

__________ changes modification time, timestamp changes but file is not modified

touch

T/F A definition of a union looks like a structure but its fields all share the same memory space so only one of them "exists" or is "active" at a time

true

T/F ALL memory in the heap is created by malloc and calloc and realloc

true

T/F In the scope where the array is declared, the compiler knows how big the array is but when you pass an array to a func, all it sees is a pointer to the first element so it does not know size

true

T/F a common paradigm is to include a union inside a struct as well as an enum that can be used to keep track of which union variant is currently active

true

T/F a structure can have common fields and varying fields by having a union member

true

T/F array subscripting is equivalent to pointer arithmetic and vice versa

true

T/F dynamically allocated memory can still exist, even after the scope that it was declared in exits

true

T/F freeing the same thing more than once is undefined

true

T/F make ensures minimum compilation if your makefile is correct

true

T/F the stack is automatically deallocated, but the heap is not

true

T/F we can have an array of structures

true

T/F we should always cast when using sizeof

true

T/F we should not use a dangling pointer because it could clobber data that occupies it later

true

T/F you must initialize all elements of an array or fields of a struct that has been dynamically allocated before using it

true

T/F we can only call pointer pointing to the beginning of data and not pointing to the middle of the data

true... It is syntactically valid but undefined

typedef struct { int d; double f; S2 *s; } S2; How can we fix this?

typedef struct S2 { int d; double f; struct S2 *s; } S2_type;

Freeing the same thing more than once is ___________________

undefined

What is the effect of malloc, calloc, and realloc if the requested size is zero bytes

undefined

Shortcut for reallocating memory

void ^realloc(void ^ptr, size_t new_size);

function that releases the memory pointed to by a pointer and returns it to the free memory pool in the heap

void free(void^ p)

void my_strcpy(char ^q, const char ^p){ while (^p != '\0') { ^q = ^p; q++; p++; } q = '\0'; } Better way to write this?

void my_strcpy(char ^ q, const char ^p){ while((^q++ = ^p++)) ; }


Set pelajaran terkait

chapter 7 (middle childhood: body and mind)

View Set

Upper limb trapezius, levator scapulae, and rhomboid major + minor pg 300

View Set

Unit 6 AP Biology, Unit 5 AP Biology Review, Cells, Cell Membrane, Cell Transport Review, Unit 4/Part 5 AP Biology Review, Cellular Energetics Review, Biochemistry Unit Review Terms, AP Biology Cell Parts Review

View Set

T3: Lesson 2 - The Origins of Hinduism & Buddhism -- Text 2: The Caste System Shapes India

View Set

Chapter 58: Drugs Affecting Gastrointestinal Motility

View Set

Adobe Photoshop CC18 Practice Training Test

View Set

Exploring the Religions of Our World chapter 2

View Set