對於任務,我必須訂購學生列表。每一個都由一個數字(15號字符串),他父親的姓氏(字符串20),他母親的姓氏(字符串20)和他的名字(字符串20也)表示。C - 輸入大的內存泄漏
我做了一個程序,從一個文件生成的學生列表並命令它(我使用合併排序來這樣做)。
當我在少數學生(< 10000)上運行該程序時,一切都很好(沒有內存泄漏或根據valgrind的任何內容)。但是,只要我嘗試在較大的(超過100 000)上使用它,我會得到一個分段錯誤11.我用Valgrind進行了調查,它說錯誤來自strcy或strcasecmp函數,並呈現:
==2433== Invalid write of size 8
==2433== at 0x4019BD: merge (sort.c:59)
==2433== by 0x40173B: sortBeginEnd (sort.c:38)
==2433== by 0x4014B0: sortWithoutInterval (sort.c:9)
==2433== by 0x401EE0: firstSort (sort.c:166)
==2433== by 0x4009EB: main (main.c:44)
==2433== Address 0xffe79ac88 is on thread 1's stack
==2433==
==2433==
==2433== Process terminating with default action of signal 11 (SIGSEGV)
==2433== Access not within mapped region at address 0xFFE79AC88
==2433== at 0x4019BD: merge (sort.c:59)
==2433== If you believe this happened as a result of a stack
==2433== overflow in your program's main thread (unlikely but
==2433== possible), you can try to increase the size of the
==2433== main thread stack using the --main-stacksize= flag.
==2433== The main thread stack size used in this run was 8388608.
==2433==
==2433== Process terminating with default action of signal 11 (SIGSEGV)
==2433== Access not within mapped region at address 0xFFE79AC81
==2433== at 0x4A256B0: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==2433== If you believe this happened as a result of a stack
==2433== overflow in your program's main thread (unlikely but
==2433== possible), you can try to increase the size of the
==2433== main thread stack using the --main-stacksize= flag.
==2433== The main thread stack size used in this run was 8388608.
==2433==
==2433== HEAP SUMMARY:
==2433== in use at exit: 12,800,101 bytes in 500,007 blocks
==2433== total heap usage: 500,008 allocs, 1 frees, 12,800,669 bytes allocated
==2433==
==2433== LEAK SUMMARY:
==2433== definitely lost: 0 bytes in 0 blocks
==2433== indirectly lost: 0 bytes in 0 blocks
==2433== possibly lost: 0 bytes in 0 blocks
==2433== still reachable: 12,800,101 bytes in 500,007 blocks
==2433== suppressed: 0 bytes in 0 blocks
==2433== Rerun with --leak-check=full to see details of leaked memory
==2433==
==2433== For counts of detected and suppressed errors, rerun with: -v
==2433== ERROR SUMMARY: 7452721 errors from 31 contexts (suppressed: 0 from 0)
可能的錯誤是我用了太多的內存(每個學生代表79個字符= 316個字節,我有100 000人,因此是31個600 000字節,如果我是正確的)?
PS:我不是真正熟悉棧和堆的概念
編輯:
「一切都很好」 的valgrind報告:
==2454==
==2454== HEAP SUMMARY:
==2454== in use at exit: 0 bytes in 0 blocks
==2454== total heap usage: 50,008 allocs, 50,008 frees, 1,280,669 bytes allocated
==2454==
==2454== All heap blocks were freed -- no leaks are possible
==2454==
==2454== For counts of detected and suppressed errors, rerun with: -v
==2454== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
EDIT2:
的代碼是here如果你想檢查它。
編輯LAST:
我終於找到了解決辦法感謝@倫丁的答案。問題是我沒有使用malloc來爲mergeSort的合併部分分配臨時數組。
我會調查一下堆/棧的問題,以充分理解問題。
我想,首先你應該爲'segfault'煩惱。檢查並向我們顯示'sort.c:59' –
通過valgrind運行您的「一切都很好」也是值得你花時間的。它可能不像你想象的那樣「好」。 *發佈代碼*。 – WhozCraig
請給我們展示一些代碼! –