
c语言堆栈问题
#include<stdio.h>voidmain(){char*p=(char*)malloc(sizeof(int)*10);char*q=NULL;strcpy(p...
#include<stdio.h>
void main()
{
char *p = (char *)malloc(sizeof(int)*10);
char *q = NULL;
strcpy(p,"Hello");
free(p);
q = (char *)malloc(sizeof(int)*10);
strcpy(p,"Hello");
if(p!=NULL)
{
*p = 'M';
}
printf("%s\n",p);
getch();
}结果是什么,为什么,谢谢。。。。麻烦详细点
free()的含义是指针指向NULL,还是指针指向一个随机区域成为野指针,还是依然指向原来位置,但其他指针可以访问,谢谢!如果最后不是printf("%s\n",p);而是printf("%s\n",q);呢?结果又会怎样 展开
void main()
{
char *p = (char *)malloc(sizeof(int)*10);
char *q = NULL;
strcpy(p,"Hello");
free(p);
q = (char *)malloc(sizeof(int)*10);
strcpy(p,"Hello");
if(p!=NULL)
{
*p = 'M';
}
printf("%s\n",p);
getch();
}结果是什么,为什么,谢谢。。。。麻烦详细点
free()的含义是指针指向NULL,还是指针指向一个随机区域成为野指针,还是依然指向原来位置,但其他指针可以访问,谢谢!如果最后不是printf("%s\n",p);而是printf("%s\n",q);呢?结果又会怎样 展开
展开全部
#include <stdio.h>//别见怪我加这么多include,不然我的编译器通不过
#include <stdlib.h>//to use malloc,free
#include <string.h>//to use strcpy
#include <conio.h> //to use getch
void main()
{
char *p = (char *)malloc(sizeof(int)*10); //申请4*10(2*10)byte,首地址存于p,没理由,把int改为char吧,那样1*10
char *q = NULL;
strcpy(p,"Hello"); //"Hello"拷贝到p中即p[0]=H,...,p[4]=o,p[5]=0
free(p); //释放刚才申请的内存,但p指向未变
q = (char *)malloc(sizeof(int)*10);//又申请赋给q,此时申请的不一定和刚才的一样,因为是动态的
strcpy(p,"Hello");//又将"Hello"复制到以p指向为首地址的连续单元,实际这是非法操作,因为还是刚才释放的空间处,而此处可能已被系统另作他用了,隐患!!!
if(p!=NULL) //p还未变,当然非空
{
*p = 'M'; //*p代表p[0]存储单元,原来是'H',现在把'M'存进去了
}
printf("%s\n",p); //打印p指向的字符串,即"Mello"
getch(); //没啥用,就是读入个字符,有的环境不加屏幕一闪,看不到结果
//malloc申请的内存不用时要用free释放掉,以便系统再行分配,本例中q指向的空间就没释放掉,而释放掉的呢,可别像本例再操作它了
}
//顺便说这只是动态内存分配,还没到堆栈呢
#include <stdlib.h>//to use malloc,free
#include <string.h>//to use strcpy
#include <conio.h> //to use getch
void main()
{
char *p = (char *)malloc(sizeof(int)*10); //申请4*10(2*10)byte,首地址存于p,没理由,把int改为char吧,那样1*10
char *q = NULL;
strcpy(p,"Hello"); //"Hello"拷贝到p中即p[0]=H,...,p[4]=o,p[5]=0
free(p); //释放刚才申请的内存,但p指向未变
q = (char *)malloc(sizeof(int)*10);//又申请赋给q,此时申请的不一定和刚才的一样,因为是动态的
strcpy(p,"Hello");//又将"Hello"复制到以p指向为首地址的连续单元,实际这是非法操作,因为还是刚才释放的空间处,而此处可能已被系统另作他用了,隐患!!!
if(p!=NULL) //p还未变,当然非空
{
*p = 'M'; //*p代表p[0]存储单元,原来是'H',现在把'M'存进去了
}
printf("%s\n",p); //打印p指向的字符串,即"Mello"
getch(); //没啥用,就是读入个字符,有的环境不加屏幕一闪,看不到结果
//malloc申请的内存不用时要用free释放掉,以便系统再行分配,本例中q指向的空间就没释放掉,而释放掉的呢,可别像本例再操作它了
}
//顺便说这只是动态内存分配,还没到堆栈呢
展开全部
mello
虽然free(p),但char *p 变量还是有一个确定地址。
你strcpy(p,"Hello")可以,只是不知道是否会覆盖别的内存的内容
.就算你没有p = (char *)malloc(sizeof(int)*10)也照样可以给p赋值,但是你无法确定是否会覆盖别的内容
虽然free(p),但char *p 变量还是有一个确定地址。
你strcpy(p,"Hello")可以,只是不知道是否会覆盖别的内存的内容
.就算你没有p = (char *)malloc(sizeof(int)*10)也照样可以给p赋值,但是你无法确定是否会覆盖别的内容
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c++就是麻烦!
比C语言麻烦多了1
比C语言麻烦多了1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
堆 和 栈 的关系
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我帮你写了InitStack和StackEmpty函数,程序最终结果如下:
#define
maxnum
20
#include<stdio.h>
#include<stdlib.h>
struct
stacktype
{
int
stack[maxnum];
int
top;
};
struct
stacktype
*S;//顶一个堆栈
int
push(struct
stacktype
*s,int
x)
{
if(s->top>=maxnum-1)
return
false;
else
s->top++;
s->stack[s->top]=x;
return
true;
}
int
pop(struct
stacktype
*s)
{
if(s->top
<0)
return
NULL;
else
s->top--;
return(s->stack[s->top+1]);
}
//初始化堆栈
void
InitStack(struct
stacktype*
&S)
{
S
=
(struct
stacktype
*)malloc(sizeof(struct
stacktype));
S->top
=
-1;
}
//判断堆栈是否为空
bool
StackEmpty(struct
stacktype
*S)
{
if
(S->top
<0)
{
return
true;
}
return
false;
}
void
dec_to_bin(int
n,int
b)
{
int
e;
InitStack(S);//请问初始化堆栈函数怎么写?
if
(S
==NULL)
{
printf("error
\n");
return;
}
while(n)
{
push(S,n%b);
n=n/b;
}
while(!StackEmpty(S))//判断栈为空的函数怎么写?
{
e=pop(S);
printf("%d",e);
}
}
void
main()
{
dec_to_bin(13,2);
printf("\n");
}
程序运行结果为:
1101
#define
maxnum
20
#include<stdio.h>
#include<stdlib.h>
struct
stacktype
{
int
stack[maxnum];
int
top;
};
struct
stacktype
*S;//顶一个堆栈
int
push(struct
stacktype
*s,int
x)
{
if(s->top>=maxnum-1)
return
false;
else
s->top++;
s->stack[s->top]=x;
return
true;
}
int
pop(struct
stacktype
*s)
{
if(s->top
<0)
return
NULL;
else
s->top--;
return(s->stack[s->top+1]);
}
//初始化堆栈
void
InitStack(struct
stacktype*
&S)
{
S
=
(struct
stacktype
*)malloc(sizeof(struct
stacktype));
S->top
=
-1;
}
//判断堆栈是否为空
bool
StackEmpty(struct
stacktype
*S)
{
if
(S->top
<0)
{
return
true;
}
return
false;
}
void
dec_to_bin(int
n,int
b)
{
int
e;
InitStack(S);//请问初始化堆栈函数怎么写?
if
(S
==NULL)
{
printf("error
\n");
return;
}
while(n)
{
push(S,n%b);
n=n/b;
}
while(!StackEmpty(S))//判断栈为空的函数怎么写?
{
e=pop(S);
printf("%d",e);
}
}
void
main()
{
dec_to_bin(13,2);
printf("\n");
}
程序运行结果为:
1101
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询