程序的源代码如下:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
#define INITSIZE 100
#define LISTINCREMENT 100
#define MIDSTRLEN 30
//typedef char ElemType
struct sqList
{
int length;
char* elem;
int listsize;
};
void InitList(sqList* L)
{
L->elem = (char*)malloc(sizeof(char)*INITSIZE);
L->length = 0;
L->listsize = INITSIZE;
}
void GetLen(sqList* L)
{
for (int i = 0; i < (L->listsize); i++)
{
if (*(L->elem + i) != '\0')
{
++(L->length);
}
}
}
char GetElem(sqList L, int i)
{
char e;
e = *(L.elem + i - 1);
return e;
}
void ListInsert(sqList* L, char e)
{
if (L->length >= L->listsize)
{
char* newbase;
newbase = (char*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(char));
if (newbase == NULL)
{
cout<<"Error when alloting memory!"<<endl;
exit(0);
}
L->elem = newbase;
L->listsize = L->listsize + LISTINCREMENT;
}
char* p;
p = L->elem + L->length;
*p = e;
++(L->length);
}
void main (int argc, char* argv[])
{
sqList la, lb;
char str1[MIDSTRLEN];
char str2[MIDSTRLEN];
InitList(&la);
InitList(&lb);
ifstream infile("input.txt");
if (!infile)
{
cout<<"input.txt can't be opened!"<<endl;
system("pause");
exit(0);
}
infile.getline(str1, sizeof(str1));
infile.getline(str2, sizeof(str2));
for (int i = 0; i <= sizeof(str1); i++)
{
if(str1[i] != '\32' || str1[i] != '\0')
{
*(la.elem) = str1[i];
++(la.elem);
}
}
for (int m = 0; m <= sizeof(str2); m++)
{
if(str2[m] != '\32' || str2[m] != '\0')
{
*(lb.elem) = str2[m];
++(lb.elem);
}
}
infile.close();
GetLen(&la);
GetLen(&lb);
for (int n = 1; n < lb.length; n++)
{
char e;
e = GetElem(lb, n);
for (int j = 0; j < la.length; j++)
{
if (*(la.elem + j) == e)
{
ListInsert(&la, e);
}
}
}
ofstream outfile("output.txt");
for (int k = 0; k < 2*(la.length); k++)
{
outfile<<(*(la.elem + k))<<" "<<endl;
}
outfile.close();
system("pause");
}
程序在c++ 2008下编译通过但运行时出现错误如下:
Windows 已在 rerg.exe 中触发一个断点。
其原因可能是堆被损坏,这说明 rerg.exe 中或它所加载的任何 DLL 中有 Bug。
原因也可能是用户在 rerg.exe 具有焦点时按下了 F12。
输出窗口可能提供了更多诊断信息。
输出窗口中的信息:
“rerg.exe”: 已加载“C:\Users\freedom\Desktop\rerg\Debug\rerg.exe”,已加载符号。
“rerg.exe”: 已加载“C:\Windows\System32\ntdll.dll”
“rerg.exe”: 已加载“C:\Windows\System32\kernel32.dll”
“rerg.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll”,已加载符号。
“rerg.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll”,已加载符号。
“rerg.exe”: 已加载“C:\Windows\System32\shimeng.dll”
“rerg.exe”: 已加载“C:\Windows\System32\apphelp.dll”
“rerg.exe”: 已加载“C:\Windows\AppPatch\AcLayers.dll”
“rerg.exe”: 已加载“C:\Windows\System32\user32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\gdi32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\advapi32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\rpcrt4.dll”
“rerg.exe”: 已加载“C:\Windows\System32\shell32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\msvcrt.dll”
“rerg.exe”: 已加载“C:\Windows\System32\shlwapi.dll”
“rerg.exe”: 已加载“C:\Windows\System32\ole32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\oleaut32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\userenv.dll”
“rerg.exe”: 已加载“C:\Windows\System32\secur32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\winspool.drv”
“rerg.exe”: 已加载“C:\Windows\System32\mpr.dll”
“rerg.exe”: 已加载“C:\Windows\System32\imm32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\msctf.dll”
“rerg.exe”: 已加载“C:\Windows\System32\lpk.dll”
“rerg.exe”: 已加载“C:\Windows\System32\usp10.dll”
“rerg.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.6001.18000_none_5cdbaa5a083979cc\comctl32.dll”
HEAP[rerg.exe]: Invalid address specified to RtlValidateHeap( 00870000, 0087523F )
Windows 已在 rerg.exe 中触发一个断点。
其原因可能是堆被损坏,这说明 rerg.exe 中或它所加载的任何 DLL 中有 Bug。
原因也可能是用户在 rerg.exe 具有焦点时按下了 F12。
输出窗口可能提供了更多诊断信息。
程序“[1432] rerg.exe: 本机”已退出,返回值为 0 (0x0)。
请大家帮忙解答一下。