存档在 ‘C/C++’ 分类

C++常见错误之一   new[]与delete[]

2009年11月5日

int *data;

data=new int[100];

delete data;

以上程序在Visual C++ 6.0中是正确的,然而在最新的ANSI/ISO C++标准中,它却是错误的。

在以往的日子里,我也一直这么写。昨日在使用CodeGuard除错,发现在delete data一行时报告资源类型不匹配。一查CodeGuard报告,提示使用new []形式创建的资源要使用delete []释放。所以正确的程序应该如下:

int *data;

data=new int[100];

delete[] data;

以上的程序在 Borland C++ Builder 6 中测试通过,CodeGuard对未修改的错误程序报告如下:

Error 00019. 0x350010 (Thread 0x04F4):
Resource type mismatch: a(n) object was expected.
delete(0x00CF5244)

Call Tree:
0x004011C0(=NDTest.exe:0x01:0001C0) D:\Program Files\Borland\CBuilder6\Projects\NDTest\MainUnit.cpp#15
0x3266FFB3(=CC3260.DLL:0x01:06EFB3)

The object array (0x00CF5244) [size: 400 bytes] was created with new[]
Call Tree:
0x004011B4(=NDTest.exe:0x01:0001B4) D:\Program Files\Borland\CBuilder6\Projects\NDTest\MainUnit.cpp#13
0x3266FFB3(=CC3260.DLL:0x01:06EFB3)

——————————————
Functions called:
delete (25 times)
realloc (1 times)
memcpy (1 times)
delete[] (2 times)
free (15 times)
new[] (15 times)
new (29 times)
calloc (5 times)
malloc (9 times)
Resource types used:
object array (15 allocs, 14 max)
object (29 allocs, 17 max)
memory block (15 allocs, 10 max)
Modules used:
00400000 05/13/2003 10:56:46 D:\Program
Files\Borland\CBuilder6\Projects\NDTest\NDTest.exe
0CD00000 01/30/2002 17:38:38 D:\PROGRA~1\Borland\CBUILD~1\Bin\CG32.DLL
32600000 01/30/2003 06:04:00 D:\PROGRA~1\Borland\CBUILD~1\Bin\CC3260.DLL
65D20000 07/22/2002 12:05:04 C:\WINNT\System32\USP10.dll
6C330000 07/22/2002 12:05:04 C:\WINNT\System32\LPK.DLL
75E00000 07/22/2002 12:05:04 C:\WINNT\System32\IMM32.DLL
77D90000 11/11/2002 15:33:44 C:\WINNT\system32\ADVAPI32.DLL
77DF0000 11/04/2002 10:58:50 C:\WINNT\system32\USER32.DLL
77E60000 11/04/2002 10:58:54 C:\WINNT\system32\KERNEL32.DLL
77F40000 07/23/2002 16:34:08 C:\WINNT\system32\GDI32.dll
77F80000 04/04/2003 15:47:22 C:\WINNT\system32\ntdll.dll
786F0000 11/20/2002 16:53:24 C:\WINNT\system32\RPCRT4.dll
==========================================

Win32多线程C程序示例

2009年11月5日

// Win32 多线程示例
// 网址: http://www.hyzgame.com
//————————————————————————————————–
#include <windows.h>
#include <iostream.h>
//————————————————————————————————–
DWORD WINAPI Print123(void *)
{
int i;

for(i=0;i<10;i++)
cout<<“123″<<endl;

return(0);
}

//————————————————————————————————–
DWORD WINAPI PrintABC(void *)
{
int i;

for(i=0;i<10;i++)
cout<<“ABC”<<endl;

return(0);
}
//————————————————————————————————–
DWORD WINAPI Printabc(void *)
{
int i;

for(i=0;i<10;i++)
cout<<“abc”<<endl;

return(0);
}
//————————————————————————————————–
int main(int argc,char **argv)
{
HANDLE handles[3];
DWORD  ids[3];

handles[0]=CreateThread(NULL,0,Print123,NULL,NULL,&ids[0]);
handles[1]=CreateThread(NULL,0,PrintABC,NULL,NULL,&ids[1]);
handles[2]=CreateThread(NULL,0,Printabc,NULL,NULL,&ids[2]);

WaitForMultipleObjects(3,handles,true,INFINITE);

CloseHandle(handles[0]);
CloseHandle(handles[1]);
CloseHandle(handles[2]);

return(0);
}

堆排序算法C源程序

2009年11月5日

#include<stdio.h>
#include<stdlib.h>

void isift(int *p,int i,int n)
{
int j,t;

t=p[i];
j=2*(i+1)-1;

while(j<=n)
{
if((j<n)&&(p[j]<p[j+1]))j++;

if(t<p[j])
{
p[i]=p[j];
i=j;
j=2*(i+1)-1;
}
else j=n+1;
}

p[i]=t;
return;
}

void pihap(int *p,int n)
{
int i,mm,t;

mm=n/2;
for(i=mm-1;i>=0;i–)
isift(p,i,n-1);

for(i=n-1;i>=1;i–)
{
t=p[0];
p[0]=p[i];
p[i]=t;

isift(p,0,i-1);
}

return;
}

void main(int argc,char *argv[])
{
int i;
int data[32];

printf(“未排序:”);
for(i=0;i<32;i++)
printf(“%d,”,data[i]=rand()%100);

pihap(data,32);

printf(“\n已排序:”);
for(i=0;i<32;i++)
printf(“%d,”,data[i]);
}

鄂ICP备09027626号