堆栈查询 [英] Stack query

查看:65
本文介绍了堆栈查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char * foo(int idx)

{

switch(idx)

{

case 0 :return" Hello";

case 1:returnGoodbye;

}

return" world" ;;

}

Newby问题。


我几周前问过类似的问题。在那个

的时候,我想知道在程序的生命周期中,由

返回字符串的类似函数分配的内存是否曾被处理过,并且

被告知它不会。

我在我们的应用程序中发现了一些这样的代码,并希望在建议更改之前得到我的

事实fn返回指向

const char * s在别处定义的指针。


我只是想知道堆栈。这个内存是否分配在堆栈上

(我们的终端系统上没有堆可用,所以我假设如此)?如果是这样,并且

这个内存没有被移除,那么堆栈的推送和弹出如何在字符串之前放置在堆栈中的
项目?


(我不知道这是不是一个真正的问题。请不要告诉我,如果

它不是,或者至少指出我很好的方向那可以)


谢谢

char *foo(int idx)
{
switch (idx)
{
case 0:return "Hello";
case 1:return "Goodbye";
}
return "world";
}
Newby question.

I asked another question a few weeks ago about something similar. At that
time I wondered whether the memory allocated by a similar function for the
returned strings was ever disposed of during the lifetime of the program and
was told it wouldn''t be.
I''d spotted some code like this in our application and wanted to get my
facts right before suggesting changes so that the fn returned pointers to
const char*s defined elsewhere.

Im just wondering about stacks. Was this memory allocated on the stack
(there''s no heap available on our end system so I assume so)? If so, and
this memory''s not removed, then how will the stack push and pops work for
items placed in the stack before the string?

(Ive no idea if this is a true ansii c question. Please dont tell me off if
it isnt, or at least point me nicely in a direction of a ng that can)

thanks

推荐答案

" Ned" <无**** @ spam.com>写道:

#char * foo(int idx)

#{

#switch(idx)

# {

#case 0:return" Hello";

#case 1:return" Goodbye";

#}

#return" world" ;;

#}


#我只是想知道堆栈。这个内存是否在堆栈中分配


字符串文字是按静态分配的。此代码与


char * foo(int idx)相同{

static char A [] =" Hello";

static char B [] =" Goodbye";

static char C [] =" world";

switch(idx){

案例0:返回A;

案例1:返回B;

}

返回C;

}


-

Derk Gwen http://derkgwen.250free.com/html/index.html

TEMPORARILY CLOSED

首先打开PERIOD
"Ned" <no****@spam.com> wrote:
# char *foo(int idx)
# {
# switch (idx)
# {
# case 0:return "Hello";
# case 1:return "Goodbye";
# }
# return "world";
# }

# Im just wondering about stacks. Was this memory allocated on the stack

String literals are allocated statickally. This code is the same as

char *foo(int idx) {
static char A[] = "Hello";
static char B[] = "Goodbye";
static char C[] = "world";
switch (idx) {
case 0: return A;
case 1: return B;
}
return C;
}

--
Derk Gwen http://derkgwen.250free.com/html/index.html
TEMPORARILY CLOSED
BE OPENED AFTER FIRST PERIOD


#Im只是想知道堆栈。这个内存是否在堆栈中分配

字符串文字是以静态方式分配的。此代码与

char * foo(int idx)相同{
静态字符A [] =" Hello" ;;
static char B [] =" Goodbye" ;;
静态字符C [] =" world" ;;
switch(idx){
情况0:返回A;
情况1:返回B;
}
返回C;
}
# Im just wondering about stacks. Was this memory allocated on the stack

String literals are allocated statickally. This code is the same as

char *foo(int idx) {
static char A[] = "Hello";
static char B[] = "Goodbye";
static char C[] = "world";
switch (idx) {
case 0: return A;
case 1: return B;
}
return C;
}




感谢Derk,这更有意义!



Thanks Derk, that makes far more sense!


在文章< bg ********** @ reader-00.news.insnet.cw.net>
In article <bg**********@reader-00.news.insnet.cw.net>
" Ned" <无**** @ spam.com>写道:
#char * foo(int idx)
#{
#switch(idx)
#{
#case 0:return" Hello" ;;
#case 1:returnGoodbye;
#}
#return" world";
#}

#我只想知道堆栈。这个内存是否分配在堆栈上


(目前还不清楚这个内存应该引用什么,

虽然结果是,最明显的猜测 - 内存

持有字符串文字的文字 - 是Ned的意思。)


文章< vj * ***********@corp.supernews.com>

Derk Gwen< de ****** @ HotPOP.com>写道:字符串文字是静态分配的。此代码与

char * foo(int idx)相同{
静态字符A [] =" Hello" ;;
static char B [] =" Goodbye" ;;
静态字符C [] =" world" ;;
switch(idx){
情况0:返回A;
情况1:返回B;
}
返回C;
}
"Ned" <no****@spam.com> wrote:
# char *foo(int idx)
# {
# switch (idx)
# {
# case 0:return "Hello";
# case 1:return "Goodbye";
# }
# return "world";
# }

# Im just wondering about stacks. Was this memory allocated on the stack
(It is not really clear what "this" memory is supposed to refer to,
although as it turns out, the most obvious guess -- the memory
holding the text of the string literals -- is what Ned meant.)

In article <vj************@corp.supernews.com>
Derk Gwen <de******@HotPOP.com> writes:String literals are allocated statickally. This code is the same as

char *foo(int idx) {
static char A[] = "Hello";
static char B[] = "Goodbye";
static char C[] = "world";
switch (idx) {
case 0: return A;
case 1: return B;
}
return C;
}




确实是相同的关于存储持续时间。然而,

,在其他一些方面有所不同:


- 原始版本有三个匿名数组,而不是

三个命名数组。新版本命名了数组(A,B,

和C),其名称在foo()中可见。这个变化很可能无关紧要。


- 原始中的匿名数组可能会也可能不会(在

编译器中) (酌情决定权)放在只读存储器中。如果是这样的话,

这与命名数组是静态

const char A []的效果大致相同。等等。匿名数组的* type *

是char的数组N。但是存储本身*可能是*只读。

C做这个相当离奇的事情主要是出于历史原因

(const在C89之前不存在)。这个改变可能很好,因为它意味着:


char * p = foo(n);

p [i ] = new_value;


在原文中无效,但在替换中有效。


- 最后,匿名数组的内容在原始的

可能会或可能不会(再次由编译器自行决定)与其他字符串文字共享

,而名称的内容为
$替换中的b $ b数组肯定不会被共享。再次,这个

的变化可能很重要。鉴于替换代码,以及对p [i]的分配,如上面的第二个差异,

a稍后调用替换foo()将返回修改了

字符串,但程序中没有其他字符串会被修改。

另一方面,如果编译器选择不放置字符串
$ b $在只读内存中的b文字,但* *共享字符串文字,

然后类似下面的代码可能产生如下所示的输出

(虽然从技术上讲,效果是undefined):


char * p = foo(0);

p [0] =''J'';

printf(我希望Jello在这里:%s \ n,p);

printf(这里我们打印%s \ n,你好);


[输出]


我希望Jello在这里:Jello

我们在这里打印Jello


简而言之,使用实际的显式数组(类型为char [N])具有

根本不同的p作为一个已知的,独立的,具有良好行为的普通阵列,您可以使用可预测的行为进行修改。使用匿名数组(char [N]类型)生成

字符串文字给你一个*可能*是只读的数组和

* may * share在程序中存储其他字符串文字。

存储持续时间相同,但可访问性和唯一性

可能会更改。只要你不试图覆盖字符串

literals - 实际上,将它们视为const char的数组。 -

这没关系。在这方面,如果字符串

文字具有类型const char [N],那将是很好的,但是因为const更确切地说是在B中打破了,并且在1989年之前不存在,他们不能有这样的

a类型。

-

In-Real-Life:风河系统(BSD工程)的Chris Torek

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+ 1 801 277 2603

电子邮件:忘了它 http:/ /67.40.109.61/torek/index.html (目前)

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



It is indeed "the same" with respect to storage duration. It is,
however, different in some other respects:

- The original version has three anonymous arrays, rather than
three named arrays. The new version has named arrays (A, B,
and C) whose names are visible inside foo(). This change is
unlikely to matter.

- The anonymous arrays in the original may or may not (at the
compiler''s discretion) be placed in read-only memory. If so,
this has much the same effect as if the named arrays were "static
const char A[]" and so on. The *type* of the anonymous arrays
is "array N of char" but the storage itself *may* be read-only.
C does this rather bizarre thing mainly for historical reasons
("const" did not exist before C89). This change might well
matter, because it means that:

char *p = foo(n);
p[i] = new_value;

is invalid in the original, but valid in the replacement.

- Finally, the contents of the anonymous arrays in the original
may or may not (again at the compiler''s discretion) be shared
with other string literals, while the contents of the named
arrays in the replacement are certainly not shared. Again, this
change might well matter. Given the replacement code, and a
(valid) assignment to p[i] as in the second difference above,
a later call to the replacement foo() will return the modified
string, but no other strings in the program will be modified.
On the other hand, if the compiler chooses not to place string
literals in read-only memory, yet *does* share string literals,
then something like the code below might produce the output
shown below (although technically the effect is undefined):

char *p = foo(0);
p[0] = ''J'';
printf("I expect Jello here: %s\n", p);
printf("And here we print %s\n", "Hello");

[output]

I expect Jello here: Jello
And here we print Jello

In short, using an actual explicit array (of type "char [N]") has
the fundamentally different property of being a known, separate,
well-behaved ordinary array, which you can modify with predictable
behavior. Using the anonymous array (of type char [N]) produced
by a string literal gives you an array that *may* be read-only and
*may* share storage with other string literals in the program.
The storage duration is the same, but the accessibility and uniqueness
may change. As long as you do not attempt to overwrite string
literals -- in effect, treating them as arrays of "const char" --
this will not matter. In that respect, it would be nice if string
literals had type "const char [N]", but since "const" is rather
broken in C, and did not exist before 1989, they cannot have such
a type.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.


这篇关于堆栈查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆