CISC 220 Flashcards

1
Q

Why is it unwise to use gets() for production code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is morris worm?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

getc or fgetc

A

Read standard input one character at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

fgets(buffer name, buffer length, stdin);

A

Reads standard input, passing stdin for the stream pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

fscanf(FILE *stream, const char *format, &variables to store results …)

A

Reads formatted input, but mixes the I/O and parse error handling.

Returns number of things successfully converted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Rewind(FILE *f)

A

Rewinds the file to set a pointer to the beginning of the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

fputc(int ch, FILE *stream);

A

Writes a character to an output stream.

Returns the written character on success, or EOF on failure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

fputs(const char *str, FILE *stream);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

fprintf(FILE *stream, const char *format, whatever you want written…)

A

Works similarly to printf except it writes to the specified stream instead of standard out.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

long ftell(FILE *stream);

A

Returns the file position for a stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

int fseek(FILE *stream, long offset, int origin);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

FILE *f = fopen(filename, “r+”);

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

char *s = strtok(char *str, “token”);
char *s = strtok(NULL, “”);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

n = sscanf(string, “format”, new_string);

A

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: “\”%[^\”]\””.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Two pointers are equal if:

A
  • they both point to the same object
  • they both point one past the end of the same array
  • they both have the value NULL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the too-far-pointer

A

A pointer which points to the element one position past the end of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

scanf(“formatting string”, &ptr-variable);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Lifetimes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Dynamically allocated memory

A

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.

20
Q

Why is deallocation important?

A

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.

21
Q

When to use allocated memory?

A
  • 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.
22
Q

int *arr = malloc(size_t n * sizeof(int));

A

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.

23
Q

How are sizes measured in C?

A

Sizes in C are measured in terms of the size of char.

24
Q

int *arr = calloc(size_t n, sizeof(int));

A

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.

25
Q

Why can’t you use sizeof() to find out the size of an array in a function?

A

The compiler treats an array as a pointer, so when you use sizeof() it returns the size of a pointer, not the array.

You can either pass in the integer value of number of objects within the array. Or you can use INT_MIN when iterating over values in an array to indicate the end of an array.

Ex.

void g(int n, int d[n]);
void h(int a[]) {
int i = 0;
while (a[i] != INT_MIN) {
}
}

26
Q

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

A

Attempts to re-allocate a block of memory that was previously allocated by malloc, calloc, or realloc.

Returns a pointer to the newly allocated memory.

Any excess memory is not initialized.

27
Q

typedef type alias

A

This declaration provides away to declare an identifier as a type alias.
- Used to replace a complex type name.

28
Q

struct name {
double x;
double y;
};

A

A structure is a group of variables in one block of memory, having a single name.

  • zero initialized.
  • point2 p = {2.0, 3.0}
  • to dereference pointer to a structure, use —>.
  • point2 *p = malloc(sizeof(point2));
    ptr->x = 200.00

To access:

struct point2 p;
printf(“%f, %f\n”, p.x, p.y);

29
Q

What is a const type?

A

Objects declared with const-qualified types, maybe placed in read-only memory by the compiler, and if the address of a const object is never taken in a program, it may not be stored at all.
- you cannot change the variable again.

30
Q

What is an include guard?

A

ifndef HEADER_H

It prevents the header file from being included more than once in a compilation unit.

  • Traditionally implemented through preprocessor directives.

#define HEADER_H

31
Q

assert(condition);

A

Allows the programmer to cause a running program to terminate if the condition is false.

32
Q

int n = sprintf(buffer, “(%g, %g)”, p.x, p.y);

A

Is a formatted print which prints to a string.

Returns the number of characters written to the buffer NOT COUNTING THE NULL-TERMINATOR.

You must allocate a buffer large enough for a returned string.
- char *buf = malloc(15 * 2 +5 * sizeof(char));

33
Q

void *memcpy(void *dest, const void *source, size_t count);

A

Copies count characters from the object, pointed to by source to the object pointed to by dest.

  • Both objects are interpreted as arrays of unsigned char.
  • Returns a copy of dest.
34
Q

void *memmove(&dest, &source, size_t count);

A

Copies count characters from the object pointed to by source to the object pointed to by dest.

  • objects may overlap (you can have dest and source be the same array)
  • copying takes place as if the characters were copied to a temporary character array, and then copied from the array to dest.
  • returns a copy of dest.
35
Q

int n = snprintf(char *buf, size_t bufsz, const char *format, …arguments to print into buffer);

A

Write the results to a character string buffer.

  • at most bufsz -1 characters are written.
  • more safe as you can restrict number of characters to be written.
  • Returns the number of characters which would have been written to the buffer, if bufsz was ignored.
36
Q

include <stdio.h></stdio.h>

A

A preprocessor directive which processes and inserts contents of a file into the C source code file before the compiler transforms the code into machine code.

Contains function declarations for puts and printf.

37
Q

File scope

A

Identifier is accessible everywhere in the file it is declared after the point where it is declared.

If an identifier is declared outside of a function or parameter list, the identifier has file scope.

38
Q

Block scope

A

If an identifier is declared inside a block or in a parameter list, the identifier has block scope.

The identifier is accessible everywhere in the block after the point where it is declared.

39
Q

Nested scopes

A

Identifiers with the same name can be declared in different scopes.

An identifier declared at an inner scope takes precedence over an identifier declared at an outer scope.

40
Q

Function prototype scope

A

A function prototype is a declaration of a function, similar to a Java abstract method.

A parameter in a function prototype has function, prototype scope.

  • Does not include the function body
  • A C file that calls a function can be compiled as long as the function prototype is given.
41
Q

Phony targets

make target

A

When using make, a phoney target is one that is not the name of a file rather it is just a name for a recipe to be executed when you make an explicit request.

  • to build a phoney target, you tell make to specifically build that target.
42
Q

What does the C preprocessor do when it encounters an #include directive in a C source code file?

A

The preprocessor processes and inserts content from the specified header file before the compiler transforms the source code into machine code.

43
Q

How does a C program set an exit status?

A

They set their exit statuses through it’s return function, where 0 indicates a successful run and any positive integer indicates an error occurred.

44
Q

What is the main difference between an int in Java and an int in C?

A

The main difference is that the size/number of bytes for an int in Java is known and constant, while an int in C has no exact specified size.

45
Q

What character is used to indicate the end of a string?

A

\0

46
Q

Disadvantages to void pointers

A
  • has to use dynamically allocated storage, and therefore is slower and can increase the risk of memory leaks.
  • Has no static type safety.
47
Q

What are string literals?

A

A string literal is a sequence of characters enclosed by double quotes.