Click here to Skip to main content
15,896,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why is the following code leading to crash?

// <br />
// VC_CONSOLE.cpp : Defines the entry point for the console application. <br />
// <br />
#include "stdafx.h" <br />
#include "stdlib.h" <br />
#include "string.h" <br />
#include <stddef.h> <br />
#include <stdio.h> <br />
#include "io.h" <br />
#include <windows.h> <br />
#include <strsafe.h> <br />
#include <direct.h><br />
#include <conio.h><br />
<br />
void leaks()<br />
{<br />
    while(7)<br />
    {<br />
        char *a = (char*)malloc(28*sizeof(char));<br />
        {<br />
        strcpy(a,"cccccccc");<br />
        printf("%s\n",a);<br />
        }<br />
    }<br />
}<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
    leaks();<br />
    return 0;<br />
} <br />
</conio.h></direct.h></strsafe.h></windows.h></stdio.h></stddef.h>


The call stack is:
0012fd68 0041365e MSVCR80D!strcat(unsigned char * dst = 0x00000000 "", unsigned char * src = 0x00000000 "")+0x89
0012fe5c 00411aea VC_CONSOLE!leaks(void)+0x7e
0012ff30 00411a06 VC_CONSOLE!wmain(int argc = 1, wchar_t ** argv = 0x051cef48)+0x3a
0012ff80 0041184d VC_CONSOLE!__tmainCRTStartup(void)+0x1a6
0012ff88 76da4911 VC_CONSOLE!wmainCRTStartup(void)+0xd
0012ff94 7737e4b6 KERNEL32!BaseThreadInitThunk+0xe
0012ffd4 7737e489 ntdll!__RtlUserThreadStart+0x23
0012ffec 00000000 ntdll!_RtlUserThreadStart+0x1b


I am getting the following panic in windbg:
(1680.438): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=7efefefe ebx=7ffd7000 ecx=004157b0 edx=63636363 esi=0012fd78 edi=00000000
eip=676ef679 esp=0012fd68 ebp=0012fe5c iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00010246
MSVCR80D!strcat+0x89:
676ef679 8917            mov     dword ptr [edi],edx  ds:0023:00000000=????????
Posted

1 solution

Because you are allocating infinite times ( while(7) ) new chunks of memory (that is a finite resource).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-May-11 3:14am    
"while(7)" is a great invention! I'm almost sorry I have to award 5 to the one who spotted it. :-)
--SA
CPallini 11-May-11 3:31am    
Well, I have to agree: while(7) is really fascinating.
Sergey Alexandrovich Kryukov 11-May-11 15:26pm    
While 7! It resembles the saying we kids heard in our childhood when adults did not want to answer to the uncountable questions "why": "Because of perpendicular". Well-know writer Veresaev recollections from his book "Stories with Children" goes: he had a young friend how was asking to many "why" questions, so Veresaev decided to use this trick:

-- Why, why... Because of perpendicular.
-- Prependi.. Perpendi...
-- Perpendicular.
-- Perpendicular?
-- Yes. Perpendicular.
Next day when the writer asked the young boy "Why did not you wash your hands before dinner" he replyed:
-- Because of perpendicular.
[end of quote]

My thing is: always answer the question "why"!
--SA
CPallini 11-May-11 15:35pm    
[blockquote]My thing is: always answer the question "why"![/blockquote]
Why?
:-D
Sergey Alexandrovich Kryukov 11-May-11 16:37pm    
Because of perpendicular, of course. :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900