64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream.h>
#include "malloc.h"
struct Node{
int data;
Node * next;
};
void create(Node *L){
Node * p;
L=(Node*)malloc(1*sizeof(Node*));
(*L).data=0;
(*L).next=NULL;
p=(Node*)malloc(1*sizeof(Node*));
p->data=0;
p->next=NULL;
while(p->data!=-1){
p=(Node*)malloc(1*sizeof(Node*));
cin>>p->data;
p->next=NULL;
if(!(*L).next){
L->next=p;
}
p->next=p;
}
}
int main(){
Node L;
Node *p;
create(&L); //头结点传递
cout<<L.data;
p=&L;
p=p->next;
while(p->next){
cout<<p->data<<" ";
p=p->next;
}
return 0;
}
#include <iostream>
using std::cout;
using std::cin;
struct Node{
int data;
Node * next;
};
void create(Node *L){
Node * p;
//L=(Node*)malloc(1*sizeof(Node*)); // It had been allocated memory in the main function
(*L).data=0;
(*L).next=NULL;
//p=(Node*)malloc(1*sizeof(Node*)); // It should be an item of Node, but not a pointer
//p=new Node;
//p->data=0;
//p->next=NULL;
p=L;
while(p->data!=-1){
//p=(Node*)malloc(sizeof(Node)); //The same reason with the previous line
p=new Node;
cin>>p->data;
p->next=NULL;
if(!(*L).next)
{
L->next=p;
}
//p->next=p;
L=p;
}
}
int main()
{
Node L;
Node *p,*temp;
create(&L); //头结点传递
cout<<L.data;
p=&L;
p=p->next;
while(p->next){
cout<<p->data<<" ";
temp=p;
p=p->next;
delete temp;
}
return 0;
}
// remeber to fee memory
//你是时候看看这本书了
如果函数的参数是一个指针,不要指望用该指针去申请动态内存。示例7-4-1 中,
Test 函数的语句GetMemory(str, 200)并没有使str 获得期望的内存,str 依旧是NULL,
为什么?
void GetMemory(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
char *str = NULL;
GetMemory(str, 100); // str 仍然为 NULL
strcpy(str, "hello"); // 运行错误
}
示例7-4-1 试图用指针参数申请动态内存
毛病出在函数GetMemory 中。编译器总是要为函数的每个参数制作临时副本,指针
参数p 的副本是 _p,编译器使 _p = p。如果函数体内的程序修改了_p 的内容,就导致
参数p 的内容作相应的修改。这就是指针可以用作输出参数的原因。在本例中,_p 申请
了新的内存,只是把_p 所指的内存地址改变了,但是p 丝毫未变。所以函数GetMemory
并不能输出任何东西。事实上,每执行一次GetMemory 就会泄露一块内存,因为没有用
free 释放内存。
如果非得要用指针参数去申请内存,那么应该改用“指向指针的指针”,见示例7-4-2。
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100); // 注意参数是 &str,而不是str
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
#include <iostream>
using std::cout;
using std::cin;
struct Node{
int data;
Node * next;
};
void create(Node *L){
Node * p;
//L=(Node*)malloc(1*sizeof(Node*)); // It had been allocated memory in the main function
(*L).data=0;
(*L).next=NULL;
//p=(Node*)malloc(1*sizeof(Node*)); // It should be an item of Node, but not a pointer
//p=new Node;
//p->data=0;
//p->next=NULL;
p=L;
while(p->data!=-1){
p=(Node*)malloc(sizeof(Node)); //The same reason with the previous line
cin>>p->data;
p->next=NULL;
if(!(*L).next)
{
L->next=p;
}
//p->next=p;
L=p;
}
}
int main()
{
Node L;
Node *p;
create(&L); //头结点传递
cout<<L.data;
p=&L;
p=p->next;
while(p->next){
cout<<p->data<<" ";
p=p->next;
}
return 0;
}
//随便改了一下
#include <iostream.h>
#include "malloc.h"
struct Node{
int data;
Node * next;
};
void create(Node** L){
Node * p;
Node *q; //游标指针
*L=(Node*)malloc(1*sizeof(Node*)); //定义头结点
(**L).data=0;
(**L).next=NULL;
q=(*L); //游标指向头结点
int nData=0;
cin>>nData;
while(nData!=-1){
p=(Node*)malloc(1*sizeof(Node*));
p->data = nData;
p->next=NULL;
q->next=p;
q = q->next;
cin>>nData;
}
}
int main(){
Node* L;
Node *p;
create(&L); //头结点传递
cout<<(*L).data<<" ";
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
return 0;
}