Computer Science Concepts: Strings, Pointers, and Memory Management

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/40

flashcard set

Earn XP

Description and Tags

These flashcards cover essential concepts related to strings, pointers, memory allocation, and file handling in the context of C programming.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

41 Terms

1
New cards

String Terminator

The character used to signify the end of a string in C, represented as '\0'.

2
New cards

Dynamic Memory Allocation

The process of allocating memory at runtime using functions such as malloc and calloc.

3
New cards

Memory Leak

A situation where allocated memory is not freed, leading to a reduction in available memory.

uses heap memory and pointers

4
New cards

Pointers

Variables that store the memory address of another variable.

5
New cards

Pass by Reference\ pass by pointer

A method of passing arguments to functions where the function receives a reference to the variable, allowing it to modify the original value.

ex: int functionName( int* var1, int* var2){}

6
New cards

File Pointer

A pointer used to refer to an open file in C for reading or writing data.

7
New cards

strlen() function

A standard library function that returns the length of a string excluding the null terminator.

8
New cards

malloc() function

A standard library function that allocates a specified number of bytes in memory and returns a pointer to it.\

ex: data type *ptrVar = (data type*)malloc(sizeof(var) * length);

9
New cards

fopen() function

A function used to open a file and return a file pointer.

10
New cards

fclose() function

A function used to close an opened file and free any resources associated with it.

11
New cards

argc and argv

Parameters in the main function that represent the count of command line arguments and the list of arguments themselves, respectively.

12
New cards

Dereferencing a pointer (* - asterisk)

The act of accessing the value stored at the memory address that a pointer is pointing to.

13
New cards

2D Array of Strings

An array that contains strings organized in rows and columns.

14
New cards

Freeing Memory

The process of releasing previously allocated memory back to the system to prevent memory leaks.

15
New cards

ampersand (&)

The operator used in C to obtain the address of a variable.

16
New cards

calloc() function

A function that allocates memory for an array of elements, initializing all bytes to zero.

ex: data type* varName = calloc(numOfElements, sizeof(data type));

17
New cards

library need for things like strcpy() or strlen()

string.h

18
New cards

what is around characters

single quotes- ‘A’

19
New cards

what is around strings/string literals

double quotes- “Hello”

20
New cards

what should pointers be initialized to

NULL

21
New cards

how to declare a pointer

data type *variableNamePointer = &varibaleName;

this syntax allows the pointer to hold the address of the variable as its “value”.

22
New cards

what is the notation of a pointer address

hexadecimal notation

23
New cards

what do pointers allow for functions

to access local variables from other functions and to manipulate data in memory directly.

24
New cards

how to process a file

  1. declare a file pointer: FILE *ptrName;

  2. set file pointer to the desired file using fopen function: ptrName = fopen("filename.txt", "mode");

  3. do what you need with the text file

  4. and then close the file: fclose(ptrName).

25
New cards

3 types of file modes

"r" for read, "w" for write, "a" for append.

26
New cards

fscanf();

A function used to read formatted input from a file. It allows for retrieving data from a file based on specified format specifiers.

27
New cards

fscanf syntax

fscanf(filePointer, “formatIdentifier”, variable);

ex: fscanf(stdin, "%d", &num);

28
New cards

fprintf();

A function used to write formatted output to a file. It allows for displaying data to a file based on specified format specifiers.

29
New cards

fprintf syntax

fprintf(filePointer, “formatIdentifier”, variable);

ex: fprintf(stdout, "Hello, %s!\n", name);

30
New cards

stack memory

A type of memory allocation that occurs in a last-in, first-out manner, used for storing local variables and function call information. Memory is automatically managed and released when a function exits.

31
New cards

global memory

Memory that is accessible throughout the program, typically used for storing global variables and data that must persist beyond a single function call. This memory remains allocated until the program terminates.

32
New cards

heap memory

A type of memory allocation used for dynamic storage where variables are allocated and freed manually, with no automatic management. It allows for more flexible memory usage but requires careful handling to avoid memory leaks.

33
New cards

realloc()

Used to change the size of a previously allocated memory block, allowing dynamic resizing of memory. It can either expand or shrink the allocated memory while preserving the contents, returning a pointer to the reallocated memory.

34
New cards

realloc syntax

data type* realloc(pointerVar, sizeof(var) * num of elements)

35
New cards

free()

must use after allocating memory

36
New cards

free syntax

free(pointerVar);

37
New cards

can you use scanf to read in strings

yes, but it stops at the first whitespace.

38
New cards

arrays with 2d strings

dataype[x][y];

where x = number of strings

and y = number of chars in each string

ex: char words[5][20]; (holds 19 chars + \0)

39
New cards

setting a variable to an address

int i = &x

40
New cards

setting a variable to a value at an address

int i = *x

41
New cards

example of a memory leak