This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Lab04 - Strings and (de)allocation


  • 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


  • learn to understand the function declaration (the name, return type, and parameters)
  • the definition (code to be executed) might not always be available


  • 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