H
H
Hello World2019-03-21 21:32:21
C++ / C#
Hello World, 2019-03-21 21:32:21

Why is the program leaking memory?

I wrote a program that removes spaces:

the code
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *inputString(size_t size);
char *CutSpaces(char *str);

int main(){
  char *str, *new_str;
  
  str = inputString(10);	
  new_str = CutSpaces(str);	
  
  printf("%s\n", new_str);


  free(new_str);	
  free(str);
  return 0;
}

char *inputString(size_t size){
  char *str;
  int ch;
  size_t len = 0;
  str = calloc(size, sizeof(char));
  if (!str) return str;
  while ((ch = getchar()) != EOF && ch != '\n' ){
    str[len++]=ch;
    if (len==size){
      char *tmp  = realloc(str, sizeof(char)*(size+=16));
      if (!tmp) 
        return str;
      else 
        str = tmp;
    }
  
    
  }
  str[len++] = '\0';
  return realloc(str, sizeof(char)*len);
}



char *CutSpaces(char *str){
  char *dst = calloc(strlen(str)+1, sizeof(char));
  if (!dst)
    perror("bad alloc");
  char *dstPtr = dst;
  while (*str){
    if (*str != ' ')
      *dstPtr++ = *str;
    str++;
  }
  free(dstPtr);
  return dst;	
}

I may have reinvented the wheel with inputString. Then how do I enter a string with an unknown length?
The program works, but valgrind complains about a memory leak:
log
[[email protected] CutSpaces]$  valgrind --leak-check=full ./main 
==27117== Memcheck, a memory error detector
==27117== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==27117== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==27117== Command: ./main
==27117== 
sdlfks; dlgsd;gl ksd;gl       
sdlfks;dlgsd;glksd;gl   <-- Оно работает
==27117== 
==27117== HEAP SUMMARY:
==27117==     in use at exit: 24 bytes in 1 blocks
==27117==   total heap usage: 6 allocs, 5 frees, 2,132 bytes allocated
==27117== 
==27117== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
==27117==    at 0x4839B65: calloc (vg_replace_malloc.c:752)
==27117==    by 0x1092D1: CutSpaces (in /path/to)
==27117==    by 0x1091BA: main (in /path/to)
==27117== 
==27117== LEAK SUMMARY:
==27117==    definitely lost: 24 bytes in 1 blocks
==27117==    indirectly lost: 0 bytes in 0 blocks
==27117==      possibly lost: 0 bytes in 0 blocks
==27117==    still reachable: 0 bytes in 0 blocks
==27117==         suppressed: 0 bytes in 0 blocks
==27117== 
==27117== For counts of detected and suppressed errors, rerun with: -v
==27117== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


Point out errors in the code

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
J
jcmvbkbc, 2019-03-21
@hello-world00

Point out errors in the code

You can build the program with -g, and valgrind will tell you exactly to the line in the source where the leaked memory was allocated.
char *str = NULL;
scanf("%m[^\n]", &str);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question