How to correct the problem?

This is a discussion on How to correct the problem? within the C Programming forums, part of the General Programming Boards category; In Linux, I have following instructions from main() function: Code: #include <stdio.h> char* testFn1(const char* sampleString) { } int main() ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    7

    Angry How to correct the problem?

    In Linux, I have following instructions from main() function:
    Code:
    #include <stdio.h>
    char* testFn1(const char* sampleString)
    {
    
    }
    
    int main()
    {
                 char *inputString, *outString;
                 inputString = malloc(13*sizeof(char));   //line number 41
                inputString = "Sample Input";
                  outString = testFn1(inputString);
                free(inputString);
    }
    When I run this program in valgrind to check the memory leaks, it reports as follows:
    ==22554== 1 errors in context 1 of 1:
    ==22554== Invalid free() / delete / delete[]
    ==22554== at 0x4004EFA: free (vg_replace_malloc.c:235)
    ==22554== by 0x80486C5: main (ex54.c:50)
    ==22554== Address 0x80488CD is not stack'd, malloc'd or (recently) free'd
    --22554--
    --22554-- supp: 12 Ubuntu-stripped-ld.so
    ==22554==
    ==22554== IN SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
    ==22554==
    ==22554== malloc/free: in use at exit: 13 bytes in 1 blocks.
    ==22554== malloc/free: 3 allocs, 3 frees, 32 bytes allocated.
    ==22554==
    ==22554== searching for pointers to 1 not-freed blocks.
    ==22554== checked 47,188 bytes.
    ==22554==
    ==22554==
    ==22554== 13 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==22554== at 0x4004405: malloc (vg_replace_malloc.c:149)
    ==22554== by 0x8048660: main (ex54.c:41)
    ==22554==
    ==22554== LEAK SUMMARY:
    ==22554== definitely lost: 13 bytes in 1 blocks.
    ==22554== possibly lost: 0 bytes in 0 blocks.
    ==22554== still reachable: 0 bytes in 0 blocks.
    ==22554== suppressed: 0 bytes in 0 blocks.
    --22554-- memcheck: sanity checks: 0 cheap, 1 expensive

    I don't understand why the malloc and free of inputString are reported as problematic. What is wrong in those statements. How to correct them?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to copy strings in c you need to use strcpy function

    in your case - you loose the pointer to malloced memory - storing in the var address of the const string...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    7

    Problem solved

    Thanks Vart,

    After using strcpy, that memory leak is not happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus Problem
    By GCNDoug in forum C++ Programming
    Replies: 0
    Last Post: 05-12-2008, 07:47 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  4. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  5. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21