0

Im trying to learn how malloc works with pointers and such and i've tried freeing my memory of pointer but when I check for memory leaks using valgrind it still says I have 32 bytes that are reachable.

#include <stdio.h>
#include <stdlib.h>

void fib2(int* a);

int main()
{
  int *pointer;

  //allocates space for 2 elements for pointer
  pointer = malloc(100 * sizeof(int));

  //prints first two fibonacci values
  printf("0 1 ");

  //calls fib2 func and apsses pointer into it
  fib2(pointer);

  //frees pointer memory
  free(pointer);

  printf("\n");

  return 0;
}

//generates fibonacci sequence
void fib2(int* a)
{
  int i;

  //initial fibonacci array initialized
  a[0] = 0;
  a[1] = 1;

  //generates and calculates fibonacci sequence and prints
  for(i = 2; i < 12; i++)
  {
    a[i] = a[i - 1] + a[i - 2];
    printf("%d ", a[i]);

  }
}

**edit Valgrind output in text form as requested earlier

==5451== Memcheck, a memory error detector
==5451== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==5451== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info

==5451== Command: ./lab3
==5451==  0 1 1 2 3 5 8 13 21 34 55 89 
==5451== 
==5451== HEAP SUMMARY:
==5451==     in use at exit: 32 bytes in 1 blocks
==5451==   total heap usage: 50 allocs, 49 frees, 107,863 bytes allocated
==5451== 
==5451== LEAK SUMMARY:
==5451==    definitely lost: 0 bytes in 0 blocks
==5451==    indirectly lost: 0 bytes in 0 blocks
==5451==      possibly lost: 0 bytes in 0 blocks
==5451==    still reachable: 32 bytes in 1 blocks
==5451==         suppressed: 0 bytes in 0 blocks
==5451== Rerun with --leak-check=full to see details of leaked memory
==5451== 
==5451== For counts of detected and suppressed errors, rerun with: -v
==5451== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Truong
  • 53
  • 6

1 Answers1

0

Ok figured it out it was an issue with my Valgrind

Truong
  • 53
  • 6