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?
What happens when attempting to dereference a void * pointer?
What happens when attempting to dereference a void * pointer?
Which of the following pointer assignments is legally permissible?
Which of the following pointer assignments is legally permissible?
Why can assignments between void * pointers and other pointer types be dangerous?
Why can assignments between void * pointers and other pointer types be dangerous?
Signup and view all the answers
What is the primary purpose of the C standard allocator?
What is the primary purpose of the C standard allocator?
Signup and view all the answers
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?
Signup and view all the answers
Which statement is true regarding structure values?
Which statement is true regarding structure values?
Signup and view all the answers
What is a limitation of the sizeof operator when used with arrays?
What is a limitation of the sizeof operator when used with arrays?
Signup and view all the answers
What does the 'next' member in the struct IntList represent?
What does the 'next' member in the struct IntList represent?
Signup and view all the answers
What does a void * type indicate in C programming?
What does a void * type indicate in C programming?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
Which member access operator is used with structure pointers?
Which member access operator is used with structure pointers?
Signup and view all the answers
What is the purpose of the '->' operator in relation to structure pointers?
What is the purpose of the '->' operator in relation to structure pointers?
Signup and view all the answers
Which of the following statements regarding structures is false?
Which of the following statements regarding structures is false?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
Which of the following is not a member of the struct ComplexList?
Which of the following is not a member of the struct ComplexList?
Signup and view all the answers
Which operation is not legal on structures?
Which operation is not legal on structures?
Signup and view all the answers
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?
Signup and view all the answers
The syntax 'struct IntList node = { 7, NULL };' performs what action?
The syntax 'struct IntList node = { 7, NULL };' performs what action?
Signup and view all the answers
What is the purpose of structures in C programming?
What is the purpose of structures in C programming?
Signup and view all the answers
What indicates that memory allocation in C is manual?
What indicates that memory allocation in C is manual?
Signup and view all the answers
Which type of structure can form linked lists in C?
Which type of structure can form linked lists in C?
Signup and view all the answers
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?
Signup and view all the answers
What are members of a struct in C?
What are members of a struct in C?
Signup and view all the answers
What is a potential downside of manual memory allocation in C?
What is a potential downside of manual memory allocation in C?
Signup and view all the answers
What does the term 'self-referential structure' mean in C?
What does the term 'self-referential structure' mean in C?
Signup and view all the answers
What is the main role of the C library regarding memory allocation?
What is the main role of the C library regarding memory allocation?
Signup and view all the answers
What does the function malloc() return?
What does the function malloc() return?
Signup and view all the answers
What is the main difference between malloc() and calloc()?
What is the main difference between malloc() and calloc()?
Signup and view all the answers
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?
Signup and view all the answers
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()?
Signup and view all the answers
How is memory for an array of 10 integers allocated using malloc()?
How is memory for an array of 10 integers allocated using malloc()?
Signup and view all the answers
When should you use calloc() instead of malloc()?
When should you use calloc() instead of malloc()?
Signup and view all the answers
Which statement correctly describes how malloc() and calloc() handle memory allocation?
Which statement correctly describes how malloc() and calloc() handle memory allocation?
Signup and view all the answers
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?
Signup and view all the answers
What function is used in C to free allocated memory?
What function is used in C to free allocated memory?
Signup and view all the answers
What is the result of a failed memory allocation in C?
What is the result of a failed memory allocation in C?
Signup and view all the answers
What does the term 'use-after-free' refer to?
What does the term 'use-after-free' refer to?
Signup and view all the answers
How can setting freed pointer variables to NULL help prevent errors?
How can setting freed pointer variables to NULL help prevent errors?
Signup and view all the answers
Why is out-of-bounds access common in heap allocations?
Why is out-of-bounds access common in heap allocations?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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 memory -
calloc(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.