简体   繁体   中英

Replace an existing content in a table (without memory-leaks) in C

Here is my problem.

I would like to change two of the contents of an array of words (the array is made up of numbers only) using the addresses (void myfunction(&tabs[0], &tabs[1])) but I have memory errors.

In my array I have for example: tabs[0] = "12" and tabs[1] = "2" and I would like to replace them by 012 and 002. Once I have these values, I free tabs[0] and tabs[1] and strdup the two new values back into tabs[0] and tabs[1] but I get errors. I don't see how to do this.

Can someone help me?

Here is my code:

char *infinyAdd(char *number1, char *number2)
{
    char result[4080] = {0};

    findSizeStr(&number1, &number2);
    fprintf(stdout, "Transform: %s - %s\n", number1, number2);

    // fprintf(stdout, "Number 01: %s - Number 02: %s\n", number1, number2);
    // sprintf(result, "%d", atoi(number1) + atoi(number2));
    return (strdup("42")); // Don't worry about that
}
void setZeroStr(char *toSet, char *oneZero, size_t lenght, bool state)
{
    char num1[4080] = {0};
    char num2[4080] = {0};

    (state == true) ? sprintf(num1, "0%s", toSet), sprintf(num2, "0%s", oneZero) : \
        sprintf(num1, "%s%s", memset(num1, '0', lenght), toSet), sprintf(num2, "0%s", oneZero);

    free(toSet); // Here is the problem
    free(oneZero); // Here is the problem
    toSet = strdup(num1);
    oneZero = strdup(num2);
}

void findSizeStr(char **number01, char **number02)
{
    switch (sizeStr((*number01), (*number02))) {
        case LONG: setZeroStr((*number02), (*number01), strlen((*number01)), false);
        break;
        case SHORT: setZeroStr((*number01), (*number02), strlen((*number02)), false);
        break;
        case EQUAL: setZeroStr((*number02), (*number01), 0, true);
        break;
        default: fprintf(stderr, "[-] Error: String Invalid\n"); break;
    }
}

Here is the leaks:

Your operation: 10 + 2
Your operand: +
==7370== Invalid read of size 1
==7370==    at 0x483DB86: strlen (vg_replace_strmem.c:459)
==7370==    by 0x48DB74D: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c620 is 0 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== Invalid read of size 1
==7370==    at 0x483DB94: strlen (vg_replace_strmem.c:459)
==7370==    by 0x48DB74D: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c621 is 1 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== Invalid read of size 1
==7370==    at 0x48F0D24: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48D9C16: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c621 is 1 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== Invalid read of size 1
==7370==    at 0x4842130: mempcpy (vg_replace_strmem.c:1536)
==7370==    by 0x48F0C41: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48D9C16: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c620 is 0 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
Transform: 10 - 2
Resultat: 42
==7370== Invalid free() / delete / delete[] / realloc()
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x401C45: freeTabs (array.c:19)
==7370==    by 0x401401: doOperand (main.c:42)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c620 is 0 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== 
==7370== HEAP SUMMARY:
==7370==     in use at exit: 8 bytes in 2 blocks
==7370==   total heap usage: 12 allocs, 12 frees, 5,306 bytes allocated
==7370== 
==7370== 4 bytes in 1 blocks are definitely lost in loss record 1 of 2
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401A06: setZeroStr (modifyString.c:25)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== 4 bytes in 1 blocks are definitely lost in loss record 2 of 2
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401A1C: setZeroStr (modifyString.c:26)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== LEAK SUMMARY:
==7370==    definitely lost: 8 bytes in 2 blocks
==7370==    indirectly lost: 0 bytes in 0 blocks
==7370==      possibly lost: 0 bytes in 0 blocks
==7370==    still reachable: 0 bytes in 0 blocks
==7370==         suppressed: 0 bytes in 0 blocks
==7370== 
==7370== ERROR SUMMARY: 15 errors from 7 contexts (suppressed: 0 from 0)
==7370== 
==7370== 2 errors in context 1 of 7:
==7370== Invalid free() / delete / delete[] / realloc()
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x401C45: freeTabs (array.c:19)
==7370==    by 0x401401: doOperand (main.c:42)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c620 is 0 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== 
==7370== 2 errors in context 2 of 7:
==7370== Invalid read of size 1
==7370==    at 0x483DB86: strlen (vg_replace_strmem.c:459)
==7370==    by 0x48DB74D: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c620 is 0 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== 
==7370== 3 errors in context 3 of 7:
==7370== Invalid read of size 1
==7370==    at 0x4842130: mempcpy (vg_replace_strmem.c:1536)
==7370==    by 0x48F0C41: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48D9C16: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c620 is 0 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== 
==7370== 3 errors in context 4 of 7:
==7370== Invalid read of size 1
==7370==    at 0x48F0D24: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48D9C16: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c621 is 1 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== 
==7370== 3 errors in context 5 of 7:
==7370== Invalid read of size 1
==7370==    at 0x483DB94: strlen (vg_replace_strmem.c:459)
==7370==    by 0x48DB74D: __vfprintf_internal (in /usr/lib64/libc-2.31.so)
==7370==    by 0x48C6109: fprintf (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401592: infinyAdd (addition.c:20)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Address 0x4a3c621 is 1 bytes inside a block of size 3 free'd
==7370==    at 0x483B9F5: free (vg_replace_malloc.c:538)
==7370==    by 0x4019F7: setZeroStr (modifyString.c:24)
==7370==    by 0x401A98: findSizeStr (modifyString.c:32)
==7370==    by 0x40156B: infinyAdd (addition.c:19)
==7370==    by 0x40138E: doOperand (main.c:35)
==7370==    by 0x4014C0: coreSystem (main.c:59)
==7370==    by 0x401505: main (main.c:67)
==7370==  Block was alloc'd at
==7370==    at 0x483A809: malloc (vg_replace_malloc.c:307)
==7370==    by 0x48FE0AE: strdup (in /usr/lib64/libc-2.31.so)
==7370==    by 0x401D8E: wordsArray (array.c:42)
==7370==    by 0x4014A9: coreSystem (main.c:58)
==7370==    by 0x401505: main (main.c:67)
==7370== 
==7370== ERROR SUMMARY: 15 errors from 7 contexts (suppressed: 0 from 0)

use the free() function before quitting your program, I think the leaks come from your maloc of number1 & number2 variables.

Try to fix all your valgrind issues that will also surely help you to have a better code

It's not clear what you're trying to do, but the strange trenery operation gives me the willies...

See what you can make of this:

int main() {
    char *leadFill = "000000000000"; // As many as needed...
    char *str = "QuickBrownFoxes";
    printf( "%.*s%s\n", 10, leadFill, str );
    printf( "%.*s%s\n", 5, leadFill, str );
    printf( "%.*s%s\n", 0, leadFill, str );

    return 0;
}

Output:

0000000000QuickBrownFoxes
00000QuickBrownFoxes
QuickBrownFoxes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM