由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - char *p = "string literal"; 和 char a[] = "string liter (转载)
相关主题
[合集] c++的题问一个 char * 和 char [] 的问题
请教什么时候变量会被load进stack,什么时候进入heap呢?问个指针array 的简单问题
what's wrong with this C++ code?expression in Java
谁来解释一下这个是compiler问题吗?一个c++小问题
问个char * 的问题python 小问题
在c中如果一个function return 一个字符串谁能示范一个小的C程序
array和pointer在作为函数返回时有啥区别 (C)请熟悉Scheme (LISP)编程的高手帮忙
写惯了C++ code,再写C code真不习惯这个C++程序为什么不能运行
相关话题的讨论汇总
话题: string话题: char话题: literal话题: liter话题: understand
进入Programming版参与讨论
1 (共1页)
G*******n
发帖数: 2041
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: GreenBean (I am THE Greenbean), 信区: JobHunting
标 题: char *p = "string literal"; 和 char a[] = "string liter
发信站: BBS 未名空间站 (Tue Aug 12 01:56:44 2008)
char *f(void)
{
char a[] = "string literal";
return a;
}
//bad: returning address of local variable or temporary
char *f(void)
{
char *p = "string literal";
return p;
}//Is this good?
这里这个p怎么理解,不也是local的吗?据说是static的所以没问题?可是没定义是static
的啊?
b**a
发帖数: 1118
2
String is different with array.
G*******n
发帖数: 2041
3
麻烦展开说说,为什么这里的p在函数返回后它指向的地址还能保持原来的值?
这部分memory什么时候会释放?

【在 b**a 的大作中提到】
: String is different with array.
t****t
发帖数: 6806
4
p指向的地址从来没有变过, 这部分内存也不会被释放
f*****Q
发帖数: 1912
5
现在的同校们是不是都不学内存管理啊?
G*******n
发帖数: 2041
6
学过,就是不知道它属于哪一种

【在 f*****Q 的大作中提到】
: 现在的同校们是不是都不学内存管理啊?
G*******n
发帖数: 2041
7
岂不是有memory leak
caller可以释放这部分内存吗?比如free(p)
谢谢

【在 t****t 的大作中提到】
: p指向的地址从来没有变过, 这部分内存也不会被释放
a****l
发帖数: 8211
8
It's good if you understand why. But, if you don't understand why this works
, better do not try to understand and use this techinique, because it is
going to do more harm than good to you.
The point is, if you truly understand the C language and how computer works, you naturally knows why this works or might not work. If you don't understand, that means your level is not up to using these kind of technique.

【在 G*******n 的大作中提到】
: 岂不是有memory leak
: caller可以释放这部分内存吗?比如free(p)
: 谢谢

o********r
发帖数: 79
9
http://www.codeguru.com/forum/archive/index.php/t-364067.html
这个解释的比较清楚。

【在 G*******n 的大作中提到】
: 岂不是有memory leak
: caller可以释放这部分内存吗?比如free(p)
: 谢谢

T*****9
发帖数: 2484
10
如果p指向另外一个地址,会不会有内存泄漏?

【在 t****t 的大作中提到】
: p指向的地址从来没有变过, 这部分内存也不会被释放
相关主题
在c中如果一个function return 一个字符串问一个 char * 和 char [] 的问题
array和pointer在作为函数返回时有啥区别 (C)问个指针array 的简单问题
写惯了C++ code,再写C code真不习惯expression in Java
进入Programming版参与讨论
T*****9
发帖数: 2484
11
肯定不可以啊

【在 G*******n 的大作中提到】
: 岂不是有memory leak
: caller可以释放这部分内存吗?比如free(p)
: 谢谢

t****t
发帖数: 6806
12
当然不会

【在 T*****9 的大作中提到】
: 如果p指向另外一个地址,会不会有内存泄漏?
f*****Q
发帖数: 1912
13
做选择题吧,A,Stack B,Data C,BSS D,Heap

【在 G*******n 的大作中提到】
: 学过,就是不知道它属于哪一种
G*******n
发帖数: 2041
14
谢谢
不过还是不懂为什么:内存不会被释放,而又不会造成memory leak
请明示,谢谢

【在 t****t 的大作中提到】
: 当然不会
G*******n
发帖数: 2041
15
我猜B
原来如此,thanks a lot!

【在 f*****Q 的大作中提到】
: 做选择题吧,A,Stack B,Data C,BSS D,Heap
T*****9
发帖数: 2484
16
data

【在 f*****Q 的大作中提到】
: 做选择题吧,A,Stack B,Data C,BSS D,Heap
y***n
发帖数: 1594
17
Here is a good explanation about this problem:
http://www.codeguru.com/forum/showthread.php?t=351721
y*****a
发帖数: 171
18
"string literal" is pre-allocated in .rodata section at compile time (no memory lead proble). a[] is local to the stack but not directly point to the string (see the assemble code). p directly points to "string literal".
.section .rodata
.LC0:
.string "string literal"
1 (共1页)
进入Programming版参与讨论
相关主题
这个C++程序为什么不能运行问个char * 的问题
c字符串内存分配问题在c中如果一个function return 一个字符串
will static/global var be initialized to 0 in C/C++array和pointer在作为函数返回时有啥区别 (C)
A question about c++ pointer写惯了C++ code,再写C code真不习惯
[合集] c++的题问一个 char * 和 char [] 的问题
请教什么时候变量会被load进stack,什么时候进入heap呢?问个指针array 的简单问题
what's wrong with this C++ code?expression in Java
谁来解释一下这个是compiler问题吗?一个c++小问题
相关话题的讨论汇总
话题: string话题: char话题: literal话题: liter话题: understand