#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
typedef struct l{
string a;
}*ll,l;
int main()
{ ll t=(ll)malloc(sizeof(l));
cin>>t->a;
cout<<t->a;
return 0;
}
结果运行时出现xxx指令引用的xxx内存,该内存不鞥为read,要终止程序请单击确定,要调试程序请单击取消;
是指针和string冲突?如果是的话,那我以后想要在结构体中定义string类型的变量并且要输入该怎么写?
11 个解决方案
#1
既然用了C++的类,就不要用malloc了,这样肯定没问题
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
typedef struct l{
string a;
}*ll;
int main()
{ ll t=new l;
cin>>t->a;
cout<<t->a;
system("pause");
return 0;
}
#2
你的头文件和结构体都不规范, 写规范在调试。
#3
严格上,我忘了delete了
程序 cout<<t->a;
后面加
delete t;
程序 cout<<t->a;
后面加
delete t;
#4
++
这样更好些
#include<iostream>
#include<string>
using namespace std;
typedef struct {
string a;
}*testp,test;
int main()
{ testp t =new test;
cin>>t->a;
cout<<t->a;
delete t;
system("pause");
return 0;
}
#5
唉,悔不该啊,当初C++没学精,就是学数据结构的时候书上用的是malloc,然后后来就一直没怎么抬去深究,出现问题了才郁闷。不过这个为什么不能用malloc啊
#6
在c++中,struct作一种特殊的class
在我#4楼的程序中
testp t =new test;
它会调用test隐式的构造函数,会初始化string a;
而malloc只是一个函数,它只能申请一段内存而不会初始化string a;
所以你的程序要出错
在C++中,如果你用了类(class或struct),且类中有非基本类型(如string实际也是个class),就不能再用malloc对类指针初始化了.
在我#4楼的程序中
testp t =new test;
它会调用test隐式的构造函数,会初始化string a;
而malloc只是一个函数,它只能申请一段内存而不会初始化string a;
所以你的程序要出错
在C++中,如果你用了类(class或struct),且类中有非基本类型(如string实际也是个class),就不能再用malloc对类指针初始化了.
#7
6楼说的对, 我刚用gdb调试确实如6楼所说的。
#8
学习了 六楼好牛逼啊
#9
有点冒昧,我是想模拟一个进程调度,用数组模拟时成功了,但是用链表模拟时结果发现错误太多,大家差钱饭后没事时帮小弟修改修改,有可能的话标志处错误的地方。不胜感激。
首先是,我输入数据后调用show(PCBList l),竟然不显示输入的结果,查了一遍也没发现什么错误,然后调用manage(PCBList&l)运行一个时间片修改数据显示一次,结果老是出现内存不可read的错误。很是无语。下面是我的源程序。大家帮忙看看,不胜感激!
#include<iostream>
#include<string>
#include<iomanip>
#include<malloc.h>
using namespace std;
typedef struct PCB{
int id;
int priority;
int cputime;
int alltime;
int startblock;
int blocktime;
string state;
struct PCB *next;
}PCB,*PCBList;
void initinstance(PCBList&l){
l=new PCB;
l->next=NULL;
}
void deletel(PCBList&l){
PCBList p=l;
while(p){
delete l;
l=p;
p=p->next;
}
}
void show(PCBList l);
void CreateList(PCBList&l)
{ char flag=1;
PCBList p=l;
while(flag){
p->next=new PCB;
cout<<"请输入PCB的ID号:"<<endl;
cin>>p->id;
cout<<"请输入PCB的优先级PRIORITY"<<endl;
cin>>p->priority;
cout<<"请输入PCB在CPU中已经执行的时间CPUTIME:"<<endl;
cin>>p->cputime;
cout<<"请输入PCB的总共需要执行的时间:"<<endl;
cin>>p->alltime;
cout<<"请输入PCB的STARTBLOCK:"<<endl;
cin>>p->startblock;
cout<<"请输入PCB的BLOCKTIME:"<<endl;
cin>>p->blocktime;
cout<<"请输入PCB的STATE:"<<endl;
cin>>p->state;
cout<<"是否要继续输入?(y/n)"<<endl;
string a; cin>>a;
if(a=="n")
flag=0;
p->next=NULL;
}
}
void manage(PCBList&l)
{
PCBList p=l->next; PCBList t=l->next; int f=-100;
while(t)
{ if(t->state=="ready"&&t->priority>f)
f=p->priority;
}
cout<<"RUNNING_PROG:";
while(p){
if(p->state=="running")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"READY_QUEUE:";
while(p){
if(p->state=="ready")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"BLOCK_QUEUE:";
while(p){
if(p->state=="block")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
while(p){
//需要解决的问题,从就绪态到running态
if(p->state=="ready")
{ if(p->priority==f)
p->state="running";
else
p->priority++;
}
else if(p->state=="running")//需要解决的,从运行态到就绪态
{ p->priority-=3; p->cputime++;
p->alltime--;
if(p->alltime==0)
{ p->state="over";}
else
{ if(p->startblock>0)
{ p->startblock--;
if(p->startblock==0)
{
p->state="block";
}
else
p->state="ready";
}
else
p->state="ready";
}
}
else if(p->state=="block")
{ if(p->blocktime>0)
{p->blocktime--;
if(p->blocktime==0)
p->state="ready";
}
}
p=p->next;
}
show(l);
}
void show(PCBList l)
{ PCBList p;
p=l->next;
cout<<setw(10)<<"ID";
while(p){
cout<<setw(5)<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"PRIORITY";
while(p){
cout<<setw(5)<<p->priority;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"CPUTIME";
while(p){
cout<<setw(5)<<p->cputime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"ALLTIME";
while(p){
cout<<setw(5)<<p->alltime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STARTBLOCK";
while(p){
cout<<setw(5)<<p->startblock;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"BLOCKTIME";
while(p){
cout<<setw(5)<<p->blocktime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STATE";
while(p){
cout<<setw(5)<<p->state;
p=p->next;
}
}
int main(){
PCBList l,pf;
initinstance(l);
CreateList(l);
cout<<"初始状态:"<<endl;
show(l);
cout<<endl;
cout<<"wof";
int t=1;
int flag=1;
while(flag)
{ pf=l->next;
flag=0;
while(pf)
{ if(pf->state!="over")
flag=1;
pf=pf->next;
}
manage(l);
t++;
}
deletel(l);
return 0;
}
首先是,我输入数据后调用show(PCBList l),竟然不显示输入的结果,查了一遍也没发现什么错误,然后调用manage(PCBList&l)运行一个时间片修改数据显示一次,结果老是出现内存不可read的错误。很是无语。下面是我的源程序。大家帮忙看看,不胜感激!
#include<iostream>
#include<string>
#include<iomanip>
#include<malloc.h>
using namespace std;
typedef struct PCB{
int id;
int priority;
int cputime;
int alltime;
int startblock;
int blocktime;
string state;
struct PCB *next;
}PCB,*PCBList;
void initinstance(PCBList&l){
l=new PCB;
l->next=NULL;
}
void deletel(PCBList&l){
PCBList p=l;
while(p){
delete l;
l=p;
p=p->next;
}
}
void show(PCBList l);
void CreateList(PCBList&l)
{ char flag=1;
PCBList p=l;
while(flag){
p->next=new PCB;
cout<<"请输入PCB的ID号:"<<endl;
cin>>p->id;
cout<<"请输入PCB的优先级PRIORITY"<<endl;
cin>>p->priority;
cout<<"请输入PCB在CPU中已经执行的时间CPUTIME:"<<endl;
cin>>p->cputime;
cout<<"请输入PCB的总共需要执行的时间:"<<endl;
cin>>p->alltime;
cout<<"请输入PCB的STARTBLOCK:"<<endl;
cin>>p->startblock;
cout<<"请输入PCB的BLOCKTIME:"<<endl;
cin>>p->blocktime;
cout<<"请输入PCB的STATE:"<<endl;
cin>>p->state;
cout<<"是否要继续输入?(y/n)"<<endl;
string a; cin>>a;
if(a=="n")
flag=0;
p->next=NULL;
}
}
void manage(PCBList&l)
{
PCBList p=l->next; PCBList t=l->next; int f=-100;
while(t)
{ if(t->state=="ready"&&t->priority>f)
f=p->priority;
}
cout<<"RUNNING_PROG:";
while(p){
if(p->state=="running")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"READY_QUEUE:";
while(p){
if(p->state=="ready")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"BLOCK_QUEUE:";
while(p){
if(p->state=="block")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
while(p){
//需要解决的问题,从就绪态到running态
if(p->state=="ready")
{ if(p->priority==f)
p->state="running";
else
p->priority++;
}
else if(p->state=="running")//需要解决的,从运行态到就绪态
{ p->priority-=3; p->cputime++;
p->alltime--;
if(p->alltime==0)
{ p->state="over";}
else
{ if(p->startblock>0)
{ p->startblock--;
if(p->startblock==0)
{
p->state="block";
}
else
p->state="ready";
}
else
p->state="ready";
}
}
else if(p->state=="block")
{ if(p->blocktime>0)
{p->blocktime--;
if(p->blocktime==0)
p->state="ready";
}
}
p=p->next;
}
show(l);
}
void show(PCBList l)
{ PCBList p;
p=l->next;
cout<<setw(10)<<"ID";
while(p){
cout<<setw(5)<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"PRIORITY";
while(p){
cout<<setw(5)<<p->priority;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"CPUTIME";
while(p){
cout<<setw(5)<<p->cputime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"ALLTIME";
while(p){
cout<<setw(5)<<p->alltime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STARTBLOCK";
while(p){
cout<<setw(5)<<p->startblock;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"BLOCKTIME";
while(p){
cout<<setw(5)<<p->blocktime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STATE";
while(p){
cout<<setw(5)<<p->state;
p=p->next;
}
}
int main(){
PCBList l,pf;
initinstance(l);
CreateList(l);
cout<<"初始状态:"<<endl;
show(l);
cout<<endl;
cout<<"wof";
int t=1;
int flag=1;
while(flag)
{ pf=l->next;
flag=0;
while(pf)
{ if(pf->state!="over")
flag=1;
pf=pf->next;
}
manage(l);
t++;
}
deletel(l);
return 0;
}
#10
C++ 尽量用new、delete配对使用。 对象就肯定的使用new好了, 会涉及到构造什么的。 基本类型无所谓了。
#11
你最后的一大段代码我没看,只解释一楼里代码的问题。:)
你struct l里的string类没有初始化,把代码改成如下(加一行):
int main()
{ ll t=(ll)malloc(sizeof(s));
new(&t->a) string; //初始化
cin>>t->a;
cout<<t->a;
return 0;
}
你struct l里的string类没有初始化,把代码改成如下(加一行):
int main()
{ ll t=(ll)malloc(sizeof(s));
new(&t->a) string; //初始化
cin>>t->a;
cout<<t->a;
return 0;
}
#1
既然用了C++的类,就不要用malloc了,这样肯定没问题
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
typedef struct l{
string a;
}*ll;
int main()
{ ll t=new l;
cin>>t->a;
cout<<t->a;
system("pause");
return 0;
}
#2
你的头文件和结构体都不规范, 写规范在调试。
#3
严格上,我忘了delete了
程序 cout<<t->a;
后面加
delete t;
程序 cout<<t->a;
后面加
delete t;
#4
++
这样更好些
#include<iostream>
#include<string>
using namespace std;
typedef struct {
string a;
}*testp,test;
int main()
{ testp t =new test;
cin>>t->a;
cout<<t->a;
delete t;
system("pause");
return 0;
}
#5
唉,悔不该啊,当初C++没学精,就是学数据结构的时候书上用的是malloc,然后后来就一直没怎么抬去深究,出现问题了才郁闷。不过这个为什么不能用malloc啊
#6
在c++中,struct作一种特殊的class
在我#4楼的程序中
testp t =new test;
它会调用test隐式的构造函数,会初始化string a;
而malloc只是一个函数,它只能申请一段内存而不会初始化string a;
所以你的程序要出错
在C++中,如果你用了类(class或struct),且类中有非基本类型(如string实际也是个class),就不能再用malloc对类指针初始化了.
在我#4楼的程序中
testp t =new test;
它会调用test隐式的构造函数,会初始化string a;
而malloc只是一个函数,它只能申请一段内存而不会初始化string a;
所以你的程序要出错
在C++中,如果你用了类(class或struct),且类中有非基本类型(如string实际也是个class),就不能再用malloc对类指针初始化了.
#7
6楼说的对, 我刚用gdb调试确实如6楼所说的。
#8
学习了 六楼好牛逼啊
#9
有点冒昧,我是想模拟一个进程调度,用数组模拟时成功了,但是用链表模拟时结果发现错误太多,大家差钱饭后没事时帮小弟修改修改,有可能的话标志处错误的地方。不胜感激。
首先是,我输入数据后调用show(PCBList l),竟然不显示输入的结果,查了一遍也没发现什么错误,然后调用manage(PCBList&l)运行一个时间片修改数据显示一次,结果老是出现内存不可read的错误。很是无语。下面是我的源程序。大家帮忙看看,不胜感激!
#include<iostream>
#include<string>
#include<iomanip>
#include<malloc.h>
using namespace std;
typedef struct PCB{
int id;
int priority;
int cputime;
int alltime;
int startblock;
int blocktime;
string state;
struct PCB *next;
}PCB,*PCBList;
void initinstance(PCBList&l){
l=new PCB;
l->next=NULL;
}
void deletel(PCBList&l){
PCBList p=l;
while(p){
delete l;
l=p;
p=p->next;
}
}
void show(PCBList l);
void CreateList(PCBList&l)
{ char flag=1;
PCBList p=l;
while(flag){
p->next=new PCB;
cout<<"请输入PCB的ID号:"<<endl;
cin>>p->id;
cout<<"请输入PCB的优先级PRIORITY"<<endl;
cin>>p->priority;
cout<<"请输入PCB在CPU中已经执行的时间CPUTIME:"<<endl;
cin>>p->cputime;
cout<<"请输入PCB的总共需要执行的时间:"<<endl;
cin>>p->alltime;
cout<<"请输入PCB的STARTBLOCK:"<<endl;
cin>>p->startblock;
cout<<"请输入PCB的BLOCKTIME:"<<endl;
cin>>p->blocktime;
cout<<"请输入PCB的STATE:"<<endl;
cin>>p->state;
cout<<"是否要继续输入?(y/n)"<<endl;
string a; cin>>a;
if(a=="n")
flag=0;
p->next=NULL;
}
}
void manage(PCBList&l)
{
PCBList p=l->next; PCBList t=l->next; int f=-100;
while(t)
{ if(t->state=="ready"&&t->priority>f)
f=p->priority;
}
cout<<"RUNNING_PROG:";
while(p){
if(p->state=="running")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"READY_QUEUE:";
while(p){
if(p->state=="ready")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"BLOCK_QUEUE:";
while(p){
if(p->state=="block")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
while(p){
//需要解决的问题,从就绪态到running态
if(p->state=="ready")
{ if(p->priority==f)
p->state="running";
else
p->priority++;
}
else if(p->state=="running")//需要解决的,从运行态到就绪态
{ p->priority-=3; p->cputime++;
p->alltime--;
if(p->alltime==0)
{ p->state="over";}
else
{ if(p->startblock>0)
{ p->startblock--;
if(p->startblock==0)
{
p->state="block";
}
else
p->state="ready";
}
else
p->state="ready";
}
}
else if(p->state=="block")
{ if(p->blocktime>0)
{p->blocktime--;
if(p->blocktime==0)
p->state="ready";
}
}
p=p->next;
}
show(l);
}
void show(PCBList l)
{ PCBList p;
p=l->next;
cout<<setw(10)<<"ID";
while(p){
cout<<setw(5)<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"PRIORITY";
while(p){
cout<<setw(5)<<p->priority;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"CPUTIME";
while(p){
cout<<setw(5)<<p->cputime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"ALLTIME";
while(p){
cout<<setw(5)<<p->alltime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STARTBLOCK";
while(p){
cout<<setw(5)<<p->startblock;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"BLOCKTIME";
while(p){
cout<<setw(5)<<p->blocktime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STATE";
while(p){
cout<<setw(5)<<p->state;
p=p->next;
}
}
int main(){
PCBList l,pf;
initinstance(l);
CreateList(l);
cout<<"初始状态:"<<endl;
show(l);
cout<<endl;
cout<<"wof";
int t=1;
int flag=1;
while(flag)
{ pf=l->next;
flag=0;
while(pf)
{ if(pf->state!="over")
flag=1;
pf=pf->next;
}
manage(l);
t++;
}
deletel(l);
return 0;
}
首先是,我输入数据后调用show(PCBList l),竟然不显示输入的结果,查了一遍也没发现什么错误,然后调用manage(PCBList&l)运行一个时间片修改数据显示一次,结果老是出现内存不可read的错误。很是无语。下面是我的源程序。大家帮忙看看,不胜感激!
#include<iostream>
#include<string>
#include<iomanip>
#include<malloc.h>
using namespace std;
typedef struct PCB{
int id;
int priority;
int cputime;
int alltime;
int startblock;
int blocktime;
string state;
struct PCB *next;
}PCB,*PCBList;
void initinstance(PCBList&l){
l=new PCB;
l->next=NULL;
}
void deletel(PCBList&l){
PCBList p=l;
while(p){
delete l;
l=p;
p=p->next;
}
}
void show(PCBList l);
void CreateList(PCBList&l)
{ char flag=1;
PCBList p=l;
while(flag){
p->next=new PCB;
cout<<"请输入PCB的ID号:"<<endl;
cin>>p->id;
cout<<"请输入PCB的优先级PRIORITY"<<endl;
cin>>p->priority;
cout<<"请输入PCB在CPU中已经执行的时间CPUTIME:"<<endl;
cin>>p->cputime;
cout<<"请输入PCB的总共需要执行的时间:"<<endl;
cin>>p->alltime;
cout<<"请输入PCB的STARTBLOCK:"<<endl;
cin>>p->startblock;
cout<<"请输入PCB的BLOCKTIME:"<<endl;
cin>>p->blocktime;
cout<<"请输入PCB的STATE:"<<endl;
cin>>p->state;
cout<<"是否要继续输入?(y/n)"<<endl;
string a; cin>>a;
if(a=="n")
flag=0;
p->next=NULL;
}
}
void manage(PCBList&l)
{
PCBList p=l->next; PCBList t=l->next; int f=-100;
while(t)
{ if(t->state=="ready"&&t->priority>f)
f=p->priority;
}
cout<<"RUNNING_PROG:";
while(p){
if(p->state=="running")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"READY_QUEUE:";
while(p){
if(p->state=="ready")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<"BLOCK_QUEUE:";
while(p){
if(p->state=="block")
cout<<"->"<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
while(p){
//需要解决的问题,从就绪态到running态
if(p->state=="ready")
{ if(p->priority==f)
p->state="running";
else
p->priority++;
}
else if(p->state=="running")//需要解决的,从运行态到就绪态
{ p->priority-=3; p->cputime++;
p->alltime--;
if(p->alltime==0)
{ p->state="over";}
else
{ if(p->startblock>0)
{ p->startblock--;
if(p->startblock==0)
{
p->state="block";
}
else
p->state="ready";
}
else
p->state="ready";
}
}
else if(p->state=="block")
{ if(p->blocktime>0)
{p->blocktime--;
if(p->blocktime==0)
p->state="ready";
}
}
p=p->next;
}
show(l);
}
void show(PCBList l)
{ PCBList p;
p=l->next;
cout<<setw(10)<<"ID";
while(p){
cout<<setw(5)<<p->id;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"PRIORITY";
while(p){
cout<<setw(5)<<p->priority;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"CPUTIME";
while(p){
cout<<setw(5)<<p->cputime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"ALLTIME";
while(p){
cout<<setw(5)<<p->alltime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STARTBLOCK";
while(p){
cout<<setw(5)<<p->startblock;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"BLOCKTIME";
while(p){
cout<<setw(5)<<p->blocktime;
p=p->next;
}
cout<<endl;
p=l->next;
cout<<setw(10)<<"STATE";
while(p){
cout<<setw(5)<<p->state;
p=p->next;
}
}
int main(){
PCBList l,pf;
initinstance(l);
CreateList(l);
cout<<"初始状态:"<<endl;
show(l);
cout<<endl;
cout<<"wof";
int t=1;
int flag=1;
while(flag)
{ pf=l->next;
flag=0;
while(pf)
{ if(pf->state!="over")
flag=1;
pf=pf->next;
}
manage(l);
t++;
}
deletel(l);
return 0;
}
#10
C++ 尽量用new、delete配对使用。 对象就肯定的使用new好了, 会涉及到构造什么的。 基本类型无所谓了。
#11
你最后的一大段代码我没看,只解释一楼里代码的问题。:)
你struct l里的string类没有初始化,把代码改成如下(加一行):
int main()
{ ll t=(ll)malloc(sizeof(s));
new(&t->a) string; //初始化
cin>>t->a;
cout<<t->a;
return 0;
}
你struct l里的string类没有初始化,把代码改成如下(加一行):
int main()
{ ll t=(ll)malloc(sizeof(s));
new(&t->a) string; //初始化
cin>>t->a;
cout<<t->a;
return 0;
}