c语言字符常量区在栈区里吗,【C语言】堆区、栈区、常量区

堆区、栈区、常量区,以前迷迷糊糊,今天在一本书上算是看明白了。这点非常重要!

栈区:先说特点:“先进后出”,空间非常小,系统自己管理,随时使用、随时释放。像函数的形式参数,局部变量都是占用栈区。

void Stack(char **pp) {

char* temp = NULL;

}

堆区:你可以理解为内存,空间比较大,需要自己申请、自己释放,熟悉的malloc函数。

void Stack(char **pp) {

char* temp = NULL;

temp = malloc(sizeof(char) * 100);

}

常量区:就是一些字符、字符串所在空间,怎么理解呢?

int main() {

char* p = "hello world";

}

031453365d10bcd13dbb944989faf629.png

“p”,所在区,栈区,“hello world” ,所在区,栈区。但是。。。下面代码中“hello world”,在常量区。temp指向“”,所在地址区域。

void Stack(char **pp) {

char* temp = NULL;

temp = malloc(sizeof(char) * 100);

memset(temp, 0, 100);

strcpy(temp, "hello world");

}

ffa79670ce287c4c589f7b61a7826092.png

图中“2”,是temp指向堆区的首地址。

#define _CRT_SECURE_NO_WARNINGS

#include#include#includevoid Stack(char **pp) {

char* temp = NULL;

temp = malloc(sizeof(char) * 100);

memset(temp, 0, 100);

strcpy(temp, "hello world");

*pp = temp;

}

int main() {

char* p = NULL;

Stack(&p);

printf("%s\n", p);

}

代码执行过程如下:

7c9a255b99c1bf3bb39bef4e07d73551.png

#define _CRT_SECURE_NO_WARNINGS

#include#include#includevoid Stack(char **pp) {

char* temp = NULL;

temp = malloc(sizeof(char) * 100);

memset(temp, 0, 100);

strcpy(temp, "hello world");

*pp = temp;

//printf("%p\n", temp);

}

int main() {

char* p = NULL;

//printf("%p\n", &p);

Stack(&p);

printf("%s\n", p);

if (p != NULL) {

free(p);

printf("释放空间结束\n");

p=NULL;

}

else

{

printf("未完成释放空间\n");

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值