CourseWare Wiki
Switch Term
Winter 2024 / 2025
Winter 2023 / 2024
Winter 2022 / 2023
Winter 2021 / 2022
Winter 2020 / 2021
Winter 2019 / 2020
Winter 2018 / 2019
Older
Search
Log In
b221
courses
be5b99cpl
labs
lab04
Warning
This page is located in archive. Go to the latest version of this
course pages
. Go the latest version of
this page
.
Table of Contents
Lab04 - Strings and (de)allocation
Valgrind
Documentation
Strings
Dynamic memory
Lab04 - Strings and (de)allocation
Valgrind
use
valgrind
for memory leak detection
can detect forgotten allocated memory and the exact location of out-of-bound access
can report using uninitialized variables/memory
for better output, compile your program with
-g
flag
valgrind ./your-program
Documentation
https://en.cppreference.com/w/c
learn to understand the function declaration (the name, return type, and parameters)
the definition (code to be executed) might not always be available
Strings
Remember you can use
#include <string.h>
for helper functions
Let's declare an array of characters, and then ask the user for their first name
Print the name, its length, and say if it's the same as your own name
You can use the function
strcmp
to compare the strings, will return zero if the same
Dynamic memory
Modify your program to use dynamic memory for the name
Ask the user for other names
Use
realloc
to make the original name string bigger
Use
memcpy
to append the additional name to the original name/string
courses/be5b99cpl/labs/lab04.txt
· Last modified: 2022/10/26 10:06 by
ulricji1