d*******n 发帖数: 1339 | 1 see this
void foo()
{
char *str="whatever";
/*
define function here
*/
return;
}
After foo() returns, will memeory used to store *str be free? (provied that
no delete[] str is used) |
s****u 发帖数: 118 | 2 static的
that
【在 d*******n 的大作中提到】 : see this : void foo() : { : char *str="whatever"; : /* : define function here : */ : return; : } : After foo() returns, will memeory used to store *str be free? (provied that
|
a****l 发帖数: 8211 | 3 actually you can not use "delete[] str".....
that
【在 d*******n 的大作中提到】 : see this : void foo() : { : char *str="whatever"; : /* : define function here : */ : return; : } : After foo() returns, will memeory used to store *str be free? (provied that
|
e***n 发帖数: 286 | 4 as long as you didn't use "new" to dynamically allocate memory, you will be
fine.
by defauly, the memory is locally allocated in the stack, not in the heap
{
char *str = "12345";
} // you will be fine, no memory leak
and
{
char *str1 = new char[6];
....
delete [] str1; // must
} // you will be fine only if you have the delete [] statement
is different. |
k****f 发帖数: 3794 | 5 char*str="12345"; "12345"字符串不是在stack里面的,不能修改的。
char str[]="12345";这个才是在stack里面的。可以随便修改的。比如str[0]='6';
be
【在 e***n 的大作中提到】 : as long as you didn't use "new" to dynamically allocate memory, you will be : fine. : by defauly, the memory is locally allocated in the stack, not in the heap : { : char *str = "12345"; : } // you will be fine, no memory leak : and : { : char *str1 = new char[6]; : ....
|
j******y 发帖数: 128 | 6 真及时,本来我也有这个疑问的。
char* str="12345"; "12345"字符串不是在stack里面的,不能修改的。
那"12345"是存在哪里的?我觉得是静态内存里,不知道对不对。
如果函数foo的返回类型是char*, 那函数结尾return str;应该可以合法返回"12345"
罗? |
D*********s 发帖数: 555 | 7 usually it's in code space.
【在 j******y 的大作中提到】 : 真及时,本来我也有这个疑问的。 : char* str="12345"; "12345"字符串不是在stack里面的,不能修改的。 : 那"12345"是存在哪里的?我觉得是静态内存里,不知道对不对。 : 如果函数foo的返回类型是char*, 那函数结尾return str;应该可以合法返回"12345" : 罗?
|
c***h 发帖数: 80 | 8 程序本身的数据段里 (程序有代码段、数据段、堆栈段等segment,可以去查阅汇编语
言的书籍)
【在 j******y 的大作中提到】 : 真及时,本来我也有这个疑问的。 : char* str="12345"; "12345"字符串不是在stack里面的,不能修改的。 : 那"12345"是存在哪里的?我觉得是静态内存里,不知道对不对。 : 如果函数foo的返回类型是char*, 那函数结尾return str;应该可以合法返回"12345" : 罗?
|
j********e 发帖数: 7 | 9 When a program is loaded into memory at run time, it normally consists of
six sections:
Description | Minimum Required Access | Non-Volatile? | Run Time
Behavior
____________________________________________________________________________
__
Code |Execute-Only |Yes |Fixed/Static
Constants |Read-Only |Yes |Fixed/Static
Initialized Data |Read/Write |Yes |Fixed/Static
Uninitialized Data |Read/Write |No |Fixed/S |
t****t 发帖数: 6806 | 10 你左右不分的?
/和\好象差别挺大的……
__
【在 j********e 的大作中提到】 : When a program is loaded into memory at run time, it normally consists of : six sections: : Description | Minimum Required Access | Non-Volatile? | Run Time : Behavior : ____________________________________________________________________________ : __ : Code |Execute-Only |Yes |Fixed/Static : Constants |Read-Only |Yes |Fixed/Static : Initialized Data |Read/Write |Yes |Fixed/Static : Uninitialized Data |Read/Write |No |Fixed/S
|
G*O 发帖数: 706 | 11 应该学学编译原理之类的课程。
【在 j******y 的大作中提到】 : 真及时,本来我也有这个疑问的。 : char* str="12345"; "12345"字符串不是在stack里面的,不能修改的。 : 那"12345"是存在哪里的?我觉得是静态内存里,不知道对不对。 : 如果函数foo的返回类型是char*, 那函数结尾return str;应该可以合法返回"12345" : 罗?
|