求大神啊,C++ new指针应用错误,程序奔溃。

SureGOGOGO 2016-05-30 08:57:50
MFC做的一个RC4的一个加密解密程序,采用new动态分配内存,分配后用delete程序就奔溃。
界面如下:

输入后的结果如下:

程序的RC4_sysDlg.cpp源代码如下:
// RC4_sysDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RC4_sys.h"
#include "RC4_sysDlg.h"
#include "RC4.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRC4_sysDlg dialog

CRC4_sysDlg::CRC4_sysDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRC4_sysDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRC4_sysDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRC4_sysDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRC4_sysDlg)
DDX_Control(pDX, IDC_type, m_type);
DDX_Control(pDX, IDC_weishu, m_weishu);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRC4_sysDlg, CDialog)
//{{AFX_MSG_MAP(CRC4_sysDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON3, OnExecute)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRC4_sysDlg message handlers

BOOL CRC4_sysDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

m_type.AddString("文件");
m_type.AddString("字符串");
m_type.SetCurSel(1);
m_weishu.AddString("256位");
m_weishu.AddString("128位");
m_weishu.AddString("56位");
m_weishu.SetCurSel(1);
((CButton*)GetDlgItem(IDC_jiami))->SetCheck(TRUE);//选上
// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CRC4_sysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CRC4_sysDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRC4_sysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CRC4_sysDlg::OnExecute()
{
CString key_str; //密匙
CString Input_str; //输入的文本
CString Output_str; //输出结果
int nDex=256; //选中的位数
//char c_key[256];

char c_type='3';

CString num_str;//选中的文本
GetDlgItem(IDC_EDIT1)->GetWindowText(key_str);
GetDlgItem(IDC_EDIT3)->GetWindowText(Input_str);
GetDlgItem(IDC_EDIT2)->GetWindowText(Output_str);
if(key_str==""||Input_str=="")
MessageBox("密匙和输入均不能为空!","错误信息",MB_ICONERROR);
else
{
m_weishu.GetWindowText(num_str);
if(num_str=="256位")
{
nDex=256;
c_type='2';
}
if(num_str=="56位")
{
nDex=56;
c_type='0';
}
if(num_str=="128位")
{
nDex=128;
c_type='1';
}
//分配内存空间
char *sbox= new char [nDex]; //sBox
char *s_box= new char [nDex]; //sBox备份
char *key= new char [nDex]; //密匙
char *pData= new char [nDex]; //输入的密文内容


if(sbox) memset(sbox,0,nDex);
if(key) strcpy(key,key_str.GetBuffer(key_str.GetLength())); //初始化密匙
if(pData) strcpy(pData,Input_str.GetBuffer(Input_str.GetLength())); //初始化密文
Rc.rc4_init(sbox,pData,c_type,strlen(key)); //初始化sbox
if(s_box)
for(int i=0;i<nDex;i++)
s_box[i]=sbox[i]; //保存sbox
//GetDlgItem(IDC_EDIT2)->SetWindowText(s_box);
Rc.rc4_crypt(sbox,pData,c_type,strlen(key));
GetDlgItem(IDC_EDIT2)->SetWindowText(sbox);
delete []s_box;
delete []sbox;
delete []pData;
delete []key;


}

}

void CRC4_sysDlg::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case 1:
break;
default:
break;
}
CDialog::OnTimer(nIDEvent);
}

头文件RC4_sysDlg.h的代码如下:
// RC4_sysDlg.h : header file
//

#if !defined(AFX_RC4_SYSDLG_H__58E34607_0269_457B_A64C_0F11B7FB7552__INCLUDED_)
#define AFX_RC4_SYSDLG_H__58E34607_0269_457B_A64C_0F11B7FB7552__INCLUDED_
#include "Rc4.h"
#define low 0
#define mid 1
#define large 2
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


/////////////////////////////////////////////////////////////////////////////
// CRC4_sysDlg dialog

class CRC4_sysDlg : public CDialog
{
// Construction
public:
CRC4_sysDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CRC4_sysDlg)
enum { IDD = IDD_RC4_SYS_DIALOG };
CComboBox m_type;
CComboBox m_weishu;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRC4_sysDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;
Rc4 Rc;


// Generated message map functions
//{{AFX_MSG(CRC4_sysDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnExecute();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_RC4_SYSDLG_H__58E34607_0269_457B_A64C_0F11B7FB7552__INCLUDED_)

RC4加密类的源代码如下:
// Rc4.cpp: implementation of the Rc4 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RC4_sys.h"
#include "Rc4.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Rc4::Rc4()
{

}

Rc4::~Rc4()
{

}


void Rc4::rc4_init( char *s, char *data,char strong,int Len) { //初始化函数
int i,j,jiamilen;
if(strong=='2') jiamilen=256;
if(strong=='1') jiamilen=128;
if(strong=='0') jiamilen=56;
//unsigned char k[jiamilen] = {0},tmp;
char *k=new char[jiamilen];
memset(k,0,jiamilen);
char tmp;

for(i = 0;i < jiamilen;i++) {
s[i] = i;
k[i] = data[i%Len];
}
for(i = 0; i < jiamilen; i++) {
j = (j + s[i] + k[i])%Len;
tmp = s[i];
s[i] = s[j]; //交换s[i]和s[j]
s[j] = tmp;
}
delete []k;
}

void Rc4::rc4_crypt( char *s, char *Data,char strong,int Len) { //加解密
int i = 0, j = 0, t = 0;
int jiamilen;
if(strong=='2') jiamilen=256;
if(strong=='1') jiamilen=128;
if(strong=='0') jiamilen=56;
unsigned long k = 0;
unsigned char tmp;
for(k = 0;k <Len;k++) {
i = (i + 1)%jiamilen;
j = (j + s[i])%jiamilen;
tmp = s[i];
s[i] = s[j]; //交换s[x]和s[y]
s[j] = tmp;
t = (s[i] + s[j])%jiamilen;
Data[k] ^= s[t];
}
}
...全文
239 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-05-31
  • 打赏
  • 举报
回复
我猜将来让楼主死不瞑目的原因之一必定是该检查的地方漏检查。
SureGOGOGO 2016-05-30
  • 打赏
  • 举报
回复
引用 3 楼 qq423399099 的回复:
检查一下strcpy的内容有多长
应该不用检查吧,因为我是根据秘钥长度分配的内存,所以不用检查吧。
小灸舞 2016-05-30
  • 打赏
  • 举报
回复
检查一下strcpy的内容有多长
SureGOGOGO 2016-05-30
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
先单步调试找到是哪个delete造成的错误。 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 然后检查数组是否越界,造成了堆破坏,导致delete出错。
额,下了断点,每一个delete都出错了,找不出原因啊。
小灸舞 2016-05-30
  • 打赏
  • 举报
回复
先单步调试找到是哪个delete造成的错误。
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
然后检查数组是否越界,造成了堆破坏,导致delete出错。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧