您好我正在學習一些調試概念。在這個程序中,我試圖模擬核心轉儲。我預計核心將被傾倒但它不會生成核心。程序執行沒有任何問題。後免費指針記憶,我可以重新分配價值
首先,我分配用於PTR 20個字節。我將一個新字符串複製到ptr。然後我免費ptr然後打印ptr它沒有任何pblm工作。最後我重新分配一些其他字符串,我期望這次它可能會生成核心轉儲。但我沒有得到任何核心轉儲。任何人都可以解釋爲什麼它沒有生成核心轉儲。
int main()
{
char *ptr;
ptr =(char*) malloc (20);
strcpy(ptr,"MemoryOperations");
printf("Before Free memory : %s\n",ptr);
free(ptr);
printf("After Free memory : %s\n",ptr);
strcpy(ptr,"MemReassign");
printf("After Re Assigning : %s\n",ptr);
return 0;
}
相同的代碼我用dbx中運行,
(dbx) check -all
access checking - ON
memuse checking - ON
(dbx) run
Running: a.out
(process id 19081)
RTC: Enabling Error Checking...
RTC: Running program...
Before Free memory : MemoryOperations
Read from unallocated (rua):
Attempting to read 1 byte at address 0x100101a48
which is 2232 bytes into the heap; no blocks allocated
stopped in _ndoprnt at 0xffffffff671abbf0
0xffffffff671abbf0: _ndoprnt+0x1c04: call _PROCEDURE_LINKAGE_TABLE_+0x620 [PLT] ! 0xffffffff67340d20
(dbx) exit
如果你想有一個核心轉儲,叫'中止()'。 –
那件事和我查過的所有東西,但我不知道它不會導致問題的原因。 – Jeyamaran
編寫C時不要施加'malloc'的返回值。 –