【发布时间】:2015-12-05 07:21:18
【问题描述】:
我创建了一个结构指针。并为其赋值。我试图打印分配给它的值。它抱怨未处理的异常访问冲突写入位置0xCDCDCDCD。这样做有什么问题?如何无一例外地完成这项任务?
StructCol.h
#include "stdafx.h"
#ifndef StructCol_H
#define StructCol_H
#include<string>
using namespace std;
struct ABC
{
string test;
int no;
void print()
{
cout << test << endl;
cout << no << endl;
}
};
#endif
StructTest2.cpp
#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<stdio.h>
#include "StructCol.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ABC*abc = (ABC*)malloc(sizeof(ABC));
abc->no = 47;
abc->test = "fyp";
abc->print();
//delete abc;
//abc->print();
_getch();
return 0;
}
【问题讨论】:
标签: c++