为什么这种去参考不起作用? [英] Why this de-referencing does not work?

查看:66
本文介绍了为什么这种去参考不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的程序崩溃了以下代码,


void lower(char * src,char * ret){

while(src){

char c = tolower(* src);

* ret = c; < -----------------------------在这里崩溃

++ ret;

++ src;

}

返回;

}


这个函数是这样的,


char * s1 =" aBcDeFg";

char * s2 =" 1234567";

降低(s1,s2);


你看到了什么问题吗?

解决方案

< li ***** @ hotmail.comwrote in message

news:11 ********************* @ o11g2000prd.googlegro ups.com ...




我的程序在以下代码崩溃,

void lower(char * src,char * ret){

while(src){

char c = tolower(* src);

* ret = c; < -----------------------------在这里崩溃

++ ret;

++ src;

}

返回;

}



继续循环,直到指针呈现空值。你希望

继续,直到指针指向的字符为空值。


karl m


li*****@hotmail.com 说:




我的程序崩溃了以下代码,


void lower(char * src,char * ret) {

while(src){



这应该是:


while(* src ){


这是第一个修复。


char c = tolower(* src);

* ret = c; < -----------------------------在这里崩溃

++ ret;

++ src;

}

返回;

}


这个函数是这样的,


char * s1 =" aBcDeFg";

char * s2 =" 1234567";

降低(s1,s2);


你看到了什么问题吗?



是的,你试图写入你不拥有的记忆。这是第二个

修复:


char * s1 =" aBcDeFg";

char s2 [] = 1234567;

降低(s1,s2);

-

Richard Heathfield

" Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


在文章< 11 ******* **************@o11g2000prd.googlegroups。 com>,

< li ***** @ hotmail.comwrote:


我的程序在以下代码崩溃,


> void lower(char * src,char * ret){
while(src){
char c = tolower( * src);
* ret = c; < -----------------------------在这里崩溃
++ ret;
++ src;
}
返回;
}


>这个函数的调用是这样的,


> char * s1 =" aBcDeFg";
char * s2 =" 1234567" ;;
lower(s1,s2);


>你看到了什么问题?



char * s2声明s2是指向字符的指针。


char * s2 =" 1234567"更进一步,并将指针初始化

到一个字符数组的地址

''1'''''''''''''''''''''' ''''''''''''''''''6'''''''和终止0(不是''0'')。


char * s2 =" 1234567"是*不*定义为分配一个区域

的内存大到足以容纳文字字符串,将文字

字符串复制到该内存中,并将指针存储到那个记忆

in s2"。相反,就C语言而言,

当你使用表格char * s2 =" 1234567"时,指向

的区域是-required- to具有静态存储持续时间,并且只能在只读内存中使用。


看来,在您的实现中,文字字符串确实是

存储在只读内存中,所以当你尝试在lower()中写入那个

字符串时,就会出现错误。在其他系统中,可能不会出现错误 - 但可能会产生副作用,即其他地方碰巧使用了1234567。 as literal

字符串可能会突然开始包含在lower()中指定的字符串。

所以不要这样做!

你能做什么改为使用


char s2 [] =" 1234567" ;;


我知道这看起来像它应该完全具有同样的效果


char * s2 =" 1234567" ;;


但是当你使用char s2 [] = literalstring表格时,C定义

来分配内存并复制文字字符串,留下你带有可以修改的字符数组的
。当您使用

char * s2 = literalstring表单时,您无法修改字符串。

-

所有都是虚荣。 - 传道书


Hi,
My program crashes on the following code,

void lower(char* src, char* ret){
while ( src ) {
char c = tolower(*src);
*ret = c; <----------------------------- crash here
++ret;
++src;
}
return;
}

the calling of this function is like this,

char* s1 = "aBcDeFg";
char* s2 = "1234567";
lower(s1, s2);

Do you see what is wrong?

解决方案

<li*****@hotmail.comwrote in message
news:11*********************@o11g2000prd.googlegro ups.com...

Hi,
My program crashes on the following code,

void lower(char* src, char* ret){
while ( src ) {
char c = tolower(*src);
*ret = c; <----------------------------- crash here
++ret;
++src;
}
return;
}

You continue your loop until the pointer assumes a null value. You want to
continue until the character pointed to by the pointer is a null value.

karl m


li*****@hotmail.com said:

Hi,
My program crashes on the following code,

void lower(char* src, char* ret){
while ( src ) {

This should be:

while(*src) {

That''s the first fix.

char c = tolower(*src);
*ret = c; <----------------------------- crash here
++ret;
++src;
}
return;
}

the calling of this function is like this,

char* s1 = "aBcDeFg";
char* s2 = "1234567";
lower(s1, s2);

Do you see what is wrong?

Yes, you''re trying to write to memory you don''t own. Here''s the second
fix:

char *s1 = "aBcDeFg";
char s2[] = "1234567";
lower(s1, s2);
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


In article <11*********************@o11g2000prd.googlegroups. com>,
<li*****@hotmail.comwrote:

My program crashes on the following code,

>void lower(char* src, char* ret){
while ( src ) {
char c = tolower(*src);
*ret = c; <----------------------------- crash here
++ret;
++src;
}
return;
}

>the calling of this function is like this,

> char* s1 = "aBcDeFg";
char* s2 = "1234567";
lower(s1, s2);

>Do you see what is wrong?

char* s2 declares s2 to be a pointer to a character.

char* s2 = "1234567" goes further and initializes that pointer
to the address of a character array that has
''1'' ''2'' ''3'' ''4'' ''5'' ''6'' ''7'' and a terminating 0 (not ''0'').

char* s2 = "1234567" is *not* defined as "allocate an area
of memory big enough to hold the literal string, copy the literal
string into that memory, and store the pointer to that memory
in s2". Instead, as far as the C language is concerned,
when you use the form char* s2 = "1234567", the area pointed
to is -required- to have static storage duration, and is
-permitted- to be in read-only memory.

It appears that in your implementation, literal strings are indeed
stored in read-only memory, so when you attempt to write to that
string in lower(), you get a fault. In other systems, there might
not be a fault -- but there might be the side effect that other
places that happened to also use "1234567" as literal
strings might suddenly start containing the string assigned in lower().
So Don''t Do That!
What you can do instead is use

char s2[] = "1234567";

I know this -looks- like it should have exactly the same effect as

char* s2 = "1234567";

but when you use the char s2[] = literalstring form, C is defined
to allocate memory and copy the literal string in, leaving you
with an array of characters that you can modify. When you use
the char* s2 = literalstring form, you cannot modify the string.
--
All is vanity. -- Ecclesiastes


这篇关于为什么这种去参考不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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