Podcast
Questions and Answers
What is a key characteristic of a void * pointer in C?
What is a key characteristic of a void * pointer in C?
- It cannot be assigned to other pointer types.
- It can store any pointer type. (correct)
- It can only point to integer types.
- It requires explicit type casting for assignment.
What happens when attempting to dereference a void * pointer?
What happens when attempting to dereference a void * pointer?
- It converts the void * pointer to an integer.
- It yields the address of the pointed location.
- It retrieves the value at the pointed address.
- It results in an error. (correct)
Which of the following pointer assignments is legally permissible?
Which of the following pointer assignments is legally permissible?
- Assigning an int * pointer to a double * pointer.
- Assigning an int * pointer to an int variable.
- Assigning a void * pointer directly to a char * pointer.
- Assigning a double * pointer to a void * pointer. (correct)
Why can assignments between void * pointers and other pointer types be dangerous?
Why can assignments between void * pointers and other pointer types be dangerous?
What is the primary purpose of the C standard allocator?
What is the primary purpose of the C standard allocator?
What operator can be used to determine the size in bytes of a variable or type?
What operator can be used to determine the size in bytes of a variable or type?
Which statement is true regarding structure values?
Which statement is true regarding structure values?
What is a limitation of the sizeof operator when used with arrays?
What is a limitation of the sizeof operator when used with arrays?
What does the 'next' member in the struct IntList represent?
What does the 'next' member in the struct IntList represent?
What does a void * type indicate in C programming?
What does a void * type indicate in C programming?
What is the correct way to declare a variable of type struct IntList?
What is the correct way to declare a variable of type struct IntList?
How do you access the value member of a structure pointer using the pointer variable?
How do you access the value member of a structure pointer using the pointer variable?
Which member access operator is used with structure pointers?
Which member access operator is used with structure pointers?
What is the purpose of the '->' operator in relation to structure pointers?
What is the purpose of the '->' operator in relation to structure pointers?
Which of the following statements regarding structures is false?
Which of the following statements regarding structures is false?
What will happen if you do not include a semicolon at the end of a structure declaration?
What will happen if you do not include a semicolon at the end of a structure declaration?
What would the expression sizeof(matrix) yield in the context of the provided function?
What would the expression sizeof(matrix) yield in the context of the provided function?
Which of the following is not a member of the struct ComplexList?
Which of the following is not a member of the struct ComplexList?
Which operation is not legal on structures?
Which operation is not legal on structures?
How would you initialize the next member of a struct IntList instance to NULL?
How would you initialize the next member of a struct IntList instance to NULL?
The syntax 'struct IntList node = { 7, NULL };' performs what action?
The syntax 'struct IntList node = { 7, NULL };' performs what action?
What is the purpose of structures in C programming?
What is the purpose of structures in C programming?
What indicates that memory allocation in C is manual?
What indicates that memory allocation in C is manual?
Which type of structure can form linked lists in C?
Which type of structure can form linked lists in C?
How does C handle complex data whose size may not be known at compile time?
How does C handle complex data whose size may not be known at compile time?
What are members of a struct in C?
What are members of a struct in C?
What is a potential downside of manual memory allocation in C?
What is a potential downside of manual memory allocation in C?
What does the term 'self-referential structure' mean in C?
What does the term 'self-referential structure' mean in C?
What is the main role of the C library regarding memory allocation?
What is the main role of the C library regarding memory allocation?
What does the function malloc() return?
What does the function malloc() return?
What is the main difference between malloc() and calloc()?
What is the main difference between malloc() and calloc()?
Which of the following is true regarding the size information of a pointer?
Which of the following is true regarding the size information of a pointer?
What will the next pointer in the allocated structure head be set to when using calloc()?
What will the next pointer in the allocated structure head be set to when using calloc()?
How is memory for an array of 10 integers allocated using malloc()?
How is memory for an array of 10 integers allocated using malloc()?
When should you use calloc() instead of malloc()?
When should you use calloc() instead of malloc()?
Which statement correctly describes how malloc() and calloc() handle memory allocation?
Which statement correctly describes how malloc() and calloc() handle memory allocation?
In the context of memory allocation, which of the following statements is correct?
In the context of memory allocation, which of the following statements is correct?
What function is used in C to free allocated memory?
What function is used in C to free allocated memory?
What is the result of a failed memory allocation in C?
What is the result of a failed memory allocation in C?
What does the term 'use-after-free' refer to?
What does the term 'use-after-free' refer to?
How can setting freed pointer variables to NULL help prevent errors?
How can setting freed pointer variables to NULL help prevent errors?
Why is out-of-bounds access common in heap allocations?
Why is out-of-bounds access common in heap allocations?
What should you do if you try to allocate a large amount of memory but it fails?
What should you do if you try to allocate a large amount of memory but it fails?
Which of the following best describes the proper use of the free() function?
Which of the following best describes the proper use of the free() function?
What is a likely consequence of using a pointer after it has been freed?
What is a likely consequence of using a pointer after it has been freed?
Flashcards
What is a struct in C?
What is a struct in C?
A structure in C is a custom data type that combines multiple data items of different types into a single unit, called a member. These members are stored together in memory.
What is the sizeof
operator?
What is the sizeof
operator?
The sizeof
operator in C determines the size of a data type or variable in bytes. It is useful for understanding memory allocation and ensuring data fits within allocated memory.
What is a void*
pointer?
What is a void*
pointer?
A void*
pointer in C is a generic pointer that can point to any data type. It is used for generic functions or when the exact data type is unknown.
What is memory allocation?
What is memory allocation?
Signup and view all the flashcards
What are allocation errors?
What are allocation errors?
Signup and view all the flashcards
What are self-referential structures?
What are self-referential structures?
Signup and view all the flashcards
How does C manage memory allocation?
How does C manage memory allocation?
Signup and view all the flashcards
What is a memory leak?
What is a memory leak?
Signup and view all the flashcards
Structure
Structure
Signup and view all the flashcards
Structure Members
Structure Members
Signup and view all the flashcards
Structure Declaration
Structure Declaration
Signup and view all the flashcards
Structure Instance
Structure Instance
Signup and view all the flashcards
Dot (.) Operator
Dot (.) Operator
Signup and view all the flashcards
Structure Pointer
Structure Pointer
Signup and view all the flashcards
Arrow (->) Operator
Arrow (->) Operator
Signup and view all the flashcards
Linked Lists with Structures
Linked Lists with Structures
Signup and view all the flashcards
Address of a structure
Address of a structure
Signup and view all the flashcards
Copying a structure
Copying a structure
Signup and view all the flashcards
Accessing structure members
Accessing structure members
Signup and view all the flashcards
sizeof operator
sizeof operator
Signup and view all the flashcards
How to use the sizeof operator
How to use the sizeof operator
Signup and view all the flashcards
Sizeof with arrays
Sizeof with arrays
Signup and view all the flashcards
The void* type
The void* type
Signup and view all the flashcards
The void type
The void type
Signup and view all the flashcards
Why is assigning a void pointer to a typed pointer dangerous?
Why is assigning a void pointer to a typed pointer dangerous?
Signup and view all the flashcards
What is the standard allocator?
What is the standard allocator?
Signup and view all the flashcards
Why is dereferencing a void pointer dangerous?
Why is dereferencing a void pointer dangerous?
Signup and view all the flashcards
Why can't you assign a typed pointer directly to another typed pointer?
Why can't you assign a typed pointer directly to another typed pointer?
Signup and view all the flashcards
What is the free()
function?
What is the free()
function?
Signup and view all the flashcards
What is a 'use-after-free' error?
What is a 'use-after-free' error?
Signup and view all the flashcards
What is out-of-bounds memory access?
What is out-of-bounds memory access?
Signup and view all the flashcards
What is memory allocation in C?
What is memory allocation in C?
Signup and view all the flashcards
What happens when memory allocation fails?
What happens when memory allocation fails?
Signup and view all the flashcards
Explain the difference between malloc()
and calloc()
.
Explain the difference between malloc()
and calloc()
.
Signup and view all the flashcards
What is the purpose of the realloc()
function?
What is the purpose of the realloc()
function?
Signup and view all the flashcards
Does C have automatic memory management?
Does C have automatic memory management?
Signup and view all the flashcards
What does malloc()
do?
What does malloc()
do?
Signup and view all the flashcards
What is calloc
's purpose?
What is calloc
's purpose?
Signup and view all the flashcards
Can you tell how much memory a pointer points to?
Can you tell how much memory a pointer points to?
Signup and view all the flashcards
How do you allocate memory for a struct?
How do you allocate memory for a struct?
Signup and view all the flashcards
How is an array allocated in memory?
How is an array allocated in memory?
Signup and view all the flashcards
Study Notes
Course Information
- Course title: Systems Programming
- Course code: CSE 220
- Instructors: Ethan Blanton, Carl Alphonce, & Eric Mikida
- Department: Computer Science and Engineering
- University: University at Buffalo
Effective Questions
- For programming questions, ask:
- What did you do?
- What did you expect to happen?
- What actually happened?
- How are they different?
- When asking questions, provide context, steps taken, expected outcome and observed outcome
Building Complex Applications
- Building more complex applications requires:
- Data structures, including self-referential structures
- Allocation of memory at program run time
- Structures are provided by the C language
- Memory allocation is provided by the C library
Structures
- A C struct aggregates multiple data items into one value.
- These items are called members.
- The aggregate is a new (struct) type.
- Members of a structure are stored together.
- Self-referential structures can form linked lists, etc.
Memory Allocation
- The amount of memory used by complex data may not be known at compile time.
- Solving this requires memory allocation.
- Self-referential structures (like lists) are normally allocated.
- Allocation and release of memory in C is manual.
- This makes C memory efficient, but also prone to leaks.
The C Struct
- A struct is a compound data type consisting of one or more other types.
- Example:
struct IntList { int value; struct IntList *next; };
- This struct contains an integer and a pointer.
value
andnext
are called members of the structure.- Any variable of type
struct IntList
contains both of these members.
Declaring and Using Structures
- Syntax for structure declaration:
struct StructureTypeName { // Members in structure // Each member has a type and a name } varname;
- Instance of structure can be created where structure is declared, or using the typename later
struct StructureTypeName varname;
Accessing Structure Members
- The
.
operator is used to access structure members - Example:
struct IntList node = { 7, NULL }; node.value = 3;
- Any member of a structure can be accessed with the
.
operator. - Example:
complexlist.complex.real = 0
;
Structure Pointers
- The
.
operator is cumbersome for structure pointers:(*list).next = NULL;
- The
->
operator is syntactic sugar for(*).
:list->next = NULL;
- The
->
operator can be used to access any member of a structure via a pointer to the structure type.
Operations on Structures
- Structure value:
- Can have its address taken with
&
- Can be copied with
=
- Can be used to access a member with
.
- Can have its address taken with
- Structure pointer:
- Can do all the things any pointer can do
- Can be used to access a member with
->
The sizeof operator
- There are several operators used to help with reflection in C.
sizeof
operator returns the size (in bytes) of its operand.- Operand can be:
- A variable
- An expression that is "like" a variable
- A type
- Expressions "like" variable include members of structures.
Looking at sizeof
sizeof
arrays is not reliable.- Only arrays declared within the current scope will be correct.
The void* type
- The type
void*
is used to indicate a pointer of unknown type. void*
variable can store any pointer type.- Type checks are mostly bypassed when assigning to/from
void*
. - Any attempt to dereference a
void*
pointer is an error.
Pointer Assignments
- Pointers in C are typed.
- Example:
int i; double d; int *pi = &i; double *pd = &d;
- Example:
void*
can be used to assign integer pointers to double pointers, but this is dangerous as it bypasses type safety.
The Standard Allocator
- The C library contains a standard allocator.
- With this allocator you can request memory from the system.
- Allocated memory is identified by its address.
Requesting Memory
malloc(size_t size)
: returns a void pointer to usable memorycalloc(size_t nmemb, size_t size)
: returns a void pointer to usable memory with all bytes set to zero
Allocation Sizes
- The size of memory allocated with
malloc
orcalloc
is not obvious but it's at least as much as the user requested. - typically the size of allocations can be derived from:
- variables or arguments.
- members in a struct
- knowledge of the data (like string length
strlen()
Allocating a Structure
- Example:
struct IntList *get_list_pointer() { struct IntList *head = calloc(1, sizeof(struct IntList)); return head; }
Allocating an Array
malloc(10* sizeof(int))
: Allocates array of 10 integers.calloc(10,sizeof(int))
: Allocates array of 10 integers and sets all bytes to 0.
Freeing Memory
- C has no garbage collector.
- The programmer is responsible for freeing memory after use.
- Use the
free()
function:void free(void *ptr)
- Free accepts pointers allocated with
malloc()
,calloc()
, orrealloc()
. - Once a pointer is freed, it should not be used again.
Failed Allocations
- Allocations can fail.
- If allocation fails (
malloc
orcalloc
), the function returnsNULL
. - Modern machines return
NULL
for unreasonable allocations (e.g., 2 GB when you meant 2 KB). - Failed allocations can be normal on smaller systems.
Use-after-free
- A common error is
use-after-free
. - Occurs when a freed pointer is used after it's freed, potentially leading to incorrect results or program crashes.
Out-of-bounds access
- Heap allocations don't have obvious size.
- This makes out-of-bounds access easy.
- The compiler won't typically catch an attempt to access memory outside the allocated space.
Summary
- Structs are collections of values.
- Structs can be self-referential.
- The C standard library contains a flexible allocator.
- Standard allocator allocations are sized by the programmer.
- C doesn't have a way to query the size of an allocation
References
- Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Second Edition. Chapter 2: 2.7; Chapter 6: Intro, 6.1–6.7. Prentice Hall, 1988.
- Linux man-pages project. man 3 malloc.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge of pointers and structures in C programming with this quiz. Covering key concepts such as void pointers, memory allocation, and the use of operators, this quiz will challenge your understanding of these fundamental programming topics. Perfect for students and developers looking to refresh their skills.