CISC 220 Flashcards
(47 cards)
Why is it unwise to use gets() for production code?
Because there is no specified limit to the amount of characters read, it is insecure.
Gets stops reading after reading a NULL terminator or EOF.
You can write into memory that you shouldn’t be able to.
What is morris worm?
Since gets doesn’t half a bound on its input, it is possible to overflow the buffer without an error message.
The worm is able to take control to replicate itself on a host computer.
getc or fgetc
Read standard input one character at a time.
fgets(buffer name, buffer length, stdin);
Reads standard input, passing stdin for the stream pointer
fscanf(FILE *stream, const char *format, &variables to store results …)
Reads formatted input, but mixes the I/O and parse error handling.
Returns number of things successfully converted.
Rewind(FILE *f)
Rewinds the file to set a pointer to the beginning of the file.
fputc(int ch, FILE *stream);
Writes a character to an output stream.
Returns the written character on success, or EOF on failure.
fputs(const char *str, FILE *stream);
Writes a null-terminated string to an output stream.
- the terminating null character is not written.
- returns a non-negative value on success or EOF on failure.
fprintf(FILE *stream, const char *format, whatever you want written…)
Works similarly to printf except it writes to the specified stream instead of standard out.
long ftell(FILE *stream);
Returns the file position for a stream.
int fseek(FILE *stream, long offset, int origin);
Sets the file position for a stream.
If the stream is open in binary mode/UNIX, the new position is exactly offset bites, measured from the beginning of the file (ie. array index) if origin is SEEK_SET, from the current file position if origin is SEEK_CUR, and from the end of the file, if origin is SEEK_END.
FILE *f = fopen(filename, “r+”);
Allows you to read and write to a file with the +.
To do this, you have to call fseek() or rewind() if you want to read after writing to a file. If you want to write after reading, you have to call fseek() or rewind().
char *s = strtok(char *str, “token”);
char *s = strtok(NULL, “”);
Parses a string by a token.
Searches for the first character not equal to the token character, then find the first token and replaces that token with a null-terminator and returns a pointer to the beginning of the substring.
- tokenizing with strtok fails when there is an empty field.
Line-oriented data can be read from a file using fgets and then processed using strtok or scanf.
n = sscanf(string, “format”, new_string);
Uses a format string to control the tokenization of a string.
- Returns the number of successful conversions.
- most conversions eat up all the whitespace and discards the leading white space characters, and ends when a trailing white space character is found (or at end of string). %c and %[] can be used to convert whitespace.
- the maximum field whist can be specified after the %.
- %s and %[] always store the null-terminator as well as the matched characters.
- can use the following format to match spaces: “\”%[^\”]\””.
Two pointers are equal if:
- they both point to the same object
- they both point one past the end of the same array
- they both have the value NULL
What is the too-far-pointer
A pointer which points to the element one position past the end of the array.
scanf(“formatting string”, &ptr-variable);
Reads data from standard input and tries to convert the data into the format, specified by the formatting string.
Returns the number of successfully converted arguments, or EOF, if input failure occurs.
Lifetimes
Automatic Storage: lifetime of the block that they are defined in. Lifetime begins when function is entered, and ends when function returns.
Static storage duration: the duration of objects declared at file scope. The lifetime is the entire execution of the program, and the value stored in the object is initialized only once, prior to the main function.
Allocated storage duration: the lifetime of an allocated object begins with the allocated function, returns and ends when the deallocation function is called.
Regular arrays -automatic storage
Variable length arrays - automatic storage
Dynamically allocated memory - allocated storage duration
Dynamically allocated memory
Data structures, such as stacks, cues, lists, sets, and maps are usually implemented so that they can grow and shrink as elements are added, and removed.
Why is deallocation important?
If you do not deallocate newly allocated memory, the program will continue to consume more and more memory until there is no more memory left to be consumed.
When to use allocated memory?
- used when the exact memory requirements are not known before runtime.
- less efficient than statically allocated memory because the memory manager needs to allocate memory from the heap.
- Memory leaks occur when the requester neglects to return unused memory back to the memory manager.
int *arr = malloc(size_t n * sizeof(int));
Attempts to allocate an uninitialized block of memory of a specified size (sizes are measured in terms of the size of char in C).
Returns a pointer to the allocated memory, or the NULL pointer if the request could not be fulfilled.
How are sizes measured in C?
Sizes in C are measured in terms of the size of char.
int *arr = calloc(size_t n, sizeof(int));
Attempts to allocate a block of memory for n objects, each having a specified size, then initializes all bites in the allocated memory to 0.
Returns a pointer to the allocated memory or NULL if request fails.