Skip to content

Instantly share code, notes, and snippets.

@afnanenayet
Last active December 17, 2021 21:18
Show Gist options
  • Save afnanenayet/1c0a71b40a67b63a42773fe2a9539f5e to your computer and use it in GitHub Desktop.
Save afnanenayet/1c0a71b40a67b63a42773fe2a9539f5e to your computer and use it in GitHub Desktop.
An example program that used to demonstrate how to use GDB/LLDB and Valgrind with the tutorial at http://www.afnan.io/debugging-c-with-gdb-and-valgrind/
/* gdb_valgrind_example.c Afnan Enayet
*
* An example designed to help a user understand how to
* use GDB/LLDB and Valgrind
*
* Functions:
* - init_str: a functions that initializes a string
* to "hello"
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/****** Function prototypes ******/
static void init_str(char *str);
/****** Function definitions *****/
/* Initializes and creates a string that says "hello"
*/
static void init_str(char *str)
{
str = malloc(5);
strcpy(str, "hello");
}
int main(void)
{
// Allocating a string with the literal "hello"
char *str = NULL;
printf("This next string should be blank...\n");
init_str(str);
printf("%s\n", str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment