ECE 2220 - Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is array label equivalent to

&array[0]

How to call a function pointer

()

enum syntax

(Definition and allocation can be separate as long as everything's named right)

How to reference fields of struct pointer

-> or (*ptr).

What must be at the end of a string*******

A null character

enum trick

Adding another enum element gives a count of the previous elements

Unions: what they do and how to use

Allow the user to reference the same memory area in different ways

Bitfields: what they are, what size they take up, how to declare, how to reference bits, thing to remember

Allows programmer to reference bits or groups of bits by name Take up an integer number of word-sized (typically int sized) BYTES (i.e. typically multiples of 4 bytes) typeName.fieldName LSB COMES FIRST!!!!

What to do with void pointers

Always cast if you can

Note about how command line arguments are read*

As STRINGS!! separated by whitespace (unless quotes are placed around argument) Use sscanf to convert

How do enums work

Assigns 0 to first item, and each item is 1 more than the previous one, unless at any point the programmer specifies a value Always allocates ints

Pointer vs. array label

Both are addresses, but an array label is a constant address

Two ways to do string arrays

Can do a 2D array, which works well for constant strings but wastes space Can do an array of string literals (pointers), saves space but doesn't work well for non-constant strings

What can't you do with arrows

Can't use ->->, not matter what parentheses you use

How does printf work with strings

Dereferences the arguments it's given, goes there, and reads until it hits a null

Pointer arithmetic when you have inc/dec with a reference, all surrounded by parentheses, which has an inc/dec

Does outside first, always

What not to do while defining struct type (2)

Don't put undimensioned array anywhere but the bottom Don't initialize a value

Opaque types purpose

Encapsulation

UCSD format

First byte of string stores length of string

sizeof(Array)

Gives size of WHOLE array in bytes

Function pointers: what they are and how to declare and initialize

Hold addresses of functions instead of data returnType (*functionName) (argTypes,...) functionName = someFunction

Pointer arithmetic order of operations if incrementing/decrementing, THEN dereferencing

If it was a pre-increment/decrement, that happens before dereferencing. If it was a post-increment/decrement, that happens AFTER dereferencing

Memory leakage

If you forget to free memory, or assign a new memory allocation to a pointer before freeing it

How is memory allocated in general

In blocks. May be a little more than you asked for, but not guaranteed

Rule with using []

Indexed value MUST be known to be an address (array or pointer)

How to free memory for multi-dimensional arrays

Inside-out

Size of all pointers

Machine address size

Memory required for using pointers for multi-dimensional arrays

Memory for elements + pointer for each row + pointer for the whole 2D array

What to use when allocating struct

Must use struct keyword

How much memory does a struct DEFINITION allocate?

None!

sizeof normally

Normally gives size of whatever type you give it (no weird addressing stuff)

Benefits of null-terminated vs. UCSD

Null-terminated is simpler, UCSD gives length easier

4 main uses for pointers

Pass by reference Reduce size of parameters Allow dynamically allocated variables Variable functions (function pointers)

Pointer arithmetic

Pointer arithmetic multiplies by sizeof(whatever type it POINTS to) ANYTIME you add or subtract (including incrementing). Note: if using NAME of array, it uses size of array type (because array name is address of first element). If using address of array, it uses size of the whole array. This isn't really sizeof, just pointer arithmetic

What does NULL refer to

Pointers or ASCII 0

Pointer order of operations with no parentheses

Post-inc/dec, *, pre-inc/dec

Note about argc and argv

Program counts itself as one argument (included in argc, it's argv[0])

Top pad

Represents how much "slack space" should be allocated (or left) whenever brk is called to expand (shrink) the heap to reduce number of system calls

Good rules of thumb with malloc (3)

Should cast Check for null Eventually free and null the pointer

Pointer arithmetic with two different types

Some compilers don't allow it, others use first type

Static vs. dynamic allocation: benefits (3 and 3)

Static allocation requires less memory, is faster, & less overhead for malloc and free, but dynamic allocation lets us store data of unknown size, free memory, and allocate memory in different parts

Static vs. dynamic (in general)

Static is at compile time, dynamic is at runtime

What happens if you write too many bits with bitfields

Takes LSB's

Trim space

The minimum amount of freed memory that would cause the system to shrink the heap by calling brk.

What if union members are different sizes?

Union is size of biggest member, everything just points to lowest address

malloc system calls

Uses mmap which maps memory and brk which sets heap size

Can you have nested structures

Yes

Which comes first, & or []?

[]

strcat, strncat: signatures and behavior

char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t maxlen); Adds a String (Concatenates) to the END!!! of Another String (no matter where it starts). Returns the address of the destination argument. Appends null

strchr: signature and behavior

char *strchr(const char *s, int c); Find the first instance of a char within a string. Returns the address of the location of that char or a zero if not present.

strcpy, strncpy: signatures, what they do, and side effects

char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t maxlen); Copies a string to memory location. Returns the address copied to strncpy does not append a NULL to the end of its copied characters if it stops prematurely with maxlen.

strstr: signature and behavior

char *strstr(const char *s1, const char *s2); Scans a String, s1, for the Occurrence of a Substring, s2. Returns the address of the string found, or a zero if the string is not present.

memcmp: signature and behavior

int memcmp(void *ptr1, const void *ptr2, size_t num); Compares two blocks of memory. Returns a int based on first different value in the blocks: negative if *ptr < *ptr2, zero if equal, and positive if s1 > s2.

sprintf: signature and behavior

int sprintf(char *str, const char *format,...); Prints a formatted string to str (including null). Returns the number of characters written.

strcmp, strncmp: signatures and behavior*

int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t maxlen); Compares two Strings. Returns a negative int if s1 < s2, 0 if equal, and positive int if s1 > s2 (uses ASCII) strcmp just returns -1,0,1, strncmp returns the actual difference

Character functions: tolower, toupper, isalpha, isalnum, isblank, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit

isalpha: letters isalnum: alphanumeric isblank**: space or tab iscntrl: control character isdigit: digits isgraph: all printable characters except space islower: lowercase isprint: all printable characters including space ispunct**: printable but not space or alphanumeric isspace: whitespace isupper: uppercase isxdigit: hex digits

memcpy vs memmove

memcpy is faster, memmove is safer

strlen()

size_t strlen(const char *s); Returns length of string starting where s points***

strspn(): signature and behavior

size_t strspn(const char *s1, const char *s2); Returns the length of the initial portion of s1 which consists only of characters that are anywhere in s2. Think of s2 as a set of characters Always watch for parameter order for all these. Don't just assume it's str1 then str2

How to force compiler to allocate struct elements contiguously

struct __attribute__ ((__packed__)) structType ...

How to declare struct

struct typeName { } variableName

Private opaque type definition

typedef struct tOpaqueType { ... } *pHiddenOT; // A pointer to actual object struct

Function pointers with typedef

typedef void (*fptr)(void) turns it into just fptr

Public opaque type definition

typedef void *tOpaqueType; // Void Pointer to Opaque "Object"

malloc signature and use

void * malloc(size_t size); Size is number of BYTES to be allocated

memchr: signature and behavior

void *memchr(const void *ptr1, int c, size_t num); Searches for the first occurrence of byte in memory (though it passes an int) within a certain block of memory given by its starting address and size

memcpy: signature and behavior

void *memcpy(void *dest, const void *src, size_t bytes); Copies a block of memory directly from src to dest. Returns the address copied to, dest.

memmove: signature and behavior

void *memmove(void *dest, const void *src, size_t bytes); Copies a block of memory from src to dest employing a buffer if necessary to allow copying of overlapping data. Returns the address copied to, dest.

memset: signature and behavior

void *memset(void *ptr, int value, size_t num_of_chars); Set a block of memory to a certain character value. Returns the starting address written, ptr

realloc: signature and behavior

void *realloc(void *ptr, size_t size) Works like malloc(), but it allows the user to increase the size of an allocation without destroying values saved in the initial allocation

qsort

void qsort(void *base, size_t nelem, size_t size, int (*fcmp)(const void *, const void *)); base - base address of stuff to sort fcmp - function that returns -1, 0, or 1 depending on whether the lower-index element is less, than, equal to greater than the other. Takes void pointers (should cast in function definition!)

calloc: signature and behavior

void* calloc (size_t num, size_t size); Zeroes out memory at allocation


Conjuntos de estudio relacionados

ELNEC Pallative Care Nursing Quizes. End of Life Nursing Education Consortium. Module 1: Introduction to Palliative Care Nursing. Section 2: An Overview of Palliative Care

View Set

Microbiology (Dr. AL, LSUE) Exam 2

View Set