奇怪的指针错误 [英] Weird pointer error

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

问题描述

我看到这个程序的一些非常奇怪的输出(用GCC编译
Linux下的
3.3.2)。


#include< iostream>

使用命名空间std;


int main(){

char * s;

s =" ; test1" ;;

cout<< s =" << s<< "和& s =" << & s<< " \ n";


char * s2;

s2 =" foo bar";

cout< < s2 = << s2<< "和& s2 =" << & s2<< " \ n";

cout<< so s = << s<< " \ n";


int * i;

* i = 1;

cout<< i = << * i<< "和& i << & i<< " \\\
英寸; //本来可以用

i代替& i


int j;

j = 4;

cout<< j = << j<< "和& j << & j<< " \ n";


返回0;

}


---


BTW:我知道我可以初步定义。


程序的输出是:

s = test1和& s = 0xbfffe354

s2 = foo bar和& s2 = 0xbfffe350

所以s = test1

分段错误(还有什么,哈哈)


-

我写了这个程序,因为我想看看当我没有'b
$ b时我会发生什么事情初始化它们。如果我在s2部分评论

,那么该程序可以工作(没有段错误)。


为什么s2会出现这个问题?


哦,就像我说的,如果我删除s2部分它工作正常并且j'的地址

在我(指针)之前是4字节。所以编译器按顺序分配内存

?或者这个平台是否依赖?


提前致谢,

cmad

解决方案

< blockquote>

" Chris Mantoulidis" <厘米**** @ yahoo.com>在消息中写道

新闻:a8 ************************** @ posting.google.c om ...

我看到这个程序的一些非常奇怪的输出(在Linux下用GCC编译的3.3.2)。

#include< iostream>
使用命名空间std;

{{*> char * s;
s =" test1";
cout<< s =" << s<< "和& s =" << & s<< " \ n";

char * s2;
s2 =" foo bar";
cout<< s2 = << s2<< "和& s2 =" << & s2<< " \ n";
cout<< so s = << s<< " \ n";

int * i;
* i = 1;
cout<< i = << * i<< "和& i << & i<< " \\\
英寸; //本来可以用
我代替& i

int j;
j = 4;
cout<< j = << j<< "和& j << & j<< " \ n";

返回0;
}

---

顺便说一句:我知道我可以初始化定义。

程序的输出是:
s = test1和& s = 0xbfffe354
s2 = foo bar和& s2 = 0xbfffe350
所以s = test1
分段错误(还有什么,哈哈)

-
我写了这个程序是因为我想看看当我为指针分配值时会发生什么不要初始化它们。
所以你现在同意指针是邪恶的:-)(我实际上不是)。

如果我评论
s2部分,那么该程序可以工作(没有段错误)。
这不是你的计划中的麻烦。它是指向一些

垃圾地址。

s和s2是const字符串文字。

为什么s2会出现这个问题?

哦,就像我说的,如果我删除了s2部分,它工作正常,j'的地址
在我(指针)之前是4字节。所以编译器按顺序分配内存
?或者这个平台是否依赖?



hmm..well你应该预期堆栈上的地址会减少

order..right?


-Sharad


Chris Mantoulidis写道:

我看到这节目的一些非常奇怪的输出(在Linux下用GCC编译
3.3.2)。

#include< iostream>
使用命名空间std;

int main(){
char * s;
s =" test1" ;;


不应使用上述转换。 " TEST1"是类型const

char [7],你让一个指向非const char的指针指向它。那是'b $ b危险的,因为看起来好像你可以改变字面值,实际上你不能实现


cout<< s =" << s<< "和& s =" << & s<< " \ n";

char * s2;
s2 =" foo bar";
cout<< s2 = << s2<< "和& s2 =" << & s2<< " \ n";
cout<< so s = << s<< " \ n";

int * i;
* i = 1;


* i是指针指向的整数。但指针不是初始化的
。它指向空旷的天空。上面分配的

的行为是未定义的,因为它试图写入

一些可能甚至不属于你的程序的随机内存位置。

cout<< i = << * i<< "和& i << & i<< " \\\
英寸; //本来可以用
我代替& i

int j;
j = 4;
cout<< j = << j<< "和& j << & j<< " \ n";

返回0;
}

---

顺便说一句:我知道我可以初始化定义。

程序的输出是:
s = test1和& s = 0xbfffe354
s2 = foo bar和& s2 = 0xbfffe350
所以s = test1
分段错误(还有什么,哈哈)

-
我写了这个程序是因为我想看看当我为指针分配值时会发生什么不要初始化它们。


你没有指定指针。你尝试分配它指向的内容。

如果我注释掉s2部分,那么程序可以工作(没有段错误)。

为什么s2会出现这个问题?


你不应该在乎。上面的程序的行为是未定义的,所以根据C ++标准,
,一切都会发生。您观察到的内容上面的
只是未定义行为的一个实例。请注意,

结果可能取决于任何事情。它可能在不同的b / b $ b编译器或不同操作系统或硬件上的相同编译器上有所不同,或者即使在具有不同编译器设置的同一系统上也可能是
或更新/>
版本的相同编译器。当你调用未定义的行为时,不值得尝试找出为什么会发生特定的事情。

哦,正如我所说的,如果我删除s2部分它的工作原理罚款和j的地址
在我(指针)之前是4字节。所以编译器按顺序分配内存
?或者这个平台是否依赖?




它取决于平台。


星期二,10 2004年2月19:21:50 +0530 in comp.lang.c ++,Sharad Kala

< no ***************** @ yahoo.com>据称写了:

我写这个程序是因为我想看看当我没有初始化时为指针赋值时会发生什么他们。


所以你现在同意指针是邪恶的:-)(我实际上不是)。




未初始化的指针总是邪恶。


I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).

#include <iostream>
using namespace std;

int main() {
char *s;
s = "test1";
cout << "s = " << s << " and &s = " << &s << "\n";

char *s2;
s2 = "foo bar";
cout << "s2 = " << s2 << " and &s2 = " << &s2 << "\n";
cout << "so s = " << s << "\n";

int *i;
*i = 1;
cout << "i = " << *i << " and &i " << &i << "\n"; //could have used
i instead of &i

int j;
j = 4;
cout << "j = " << j << " and &j " << &j << "\n";

return 0;
}

---

BTW: I know I can initialize on definition.

Output of the program is:
s = test1 and &s = 0xbfffe354
s2 = foo bar and &s2 = 0xbfffe350
so s = test1
Segmentation fault (what else, lol)

--
I wrote this program because i wanted to see what happens when I
assign values to pointers when I don''t initialize them. If I comment
out the s2 part, then the program works (no segfault).

Why would s2 make this problem?

Oh, as I said, if I remove the s2 part it works fine and j''s address
is 4bytes before i (the pointer). So the compiler allocates memory
with decreasing order? Or is this platform dependent?

Thanks in advance,
cmad

解决方案


"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google.c om...

I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).

#include <iostream>
using namespace std;

int main() {
char *s;
s = "test1";
cout << "s = " << s << " and &s = " << &s << "\n";

char *s2;
s2 = "foo bar";
cout << "s2 = " << s2 << " and &s2 = " << &s2 << "\n";
cout << "so s = " << s << "\n";

int *i;
*i = 1;
cout << "i = " << *i << " and &i " << &i << "\n"; //could have used
i instead of &i

int j;
j = 4;
cout << "j = " << j << " and &j " << &j << "\n";

return 0;
}

---

BTW: I know I can initialize on definition.

Output of the program is:
s = test1 and &s = 0xbfffe354
s2 = foo bar and &s2 = 0xbfffe350
so s = test1
Segmentation fault (what else, lol)

--
I wrote this program because i wanted to see what happens when I
assign values to pointers when I don''t initialize them. So you agree now that pointers are evil :-) (I actually don''t).
If I comment
out the s2 part, then the program works (no segfault). It''s not s2 that''s the trouble in your program. It''s i which is pointing to some
garbage address.
s and s2 are const string literals.
Why would s2 make this problem?

Oh, as I said, if I remove the s2 part it works fine and j''s address
is 4bytes before i (the pointer). So the compiler allocates memory
with decreasing order? Or is this platform dependent?


hmm..well you should expect addresses on stack to be in decreasing
order..right?.

-Sharad


Chris Mantoulidis wrote:

I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).

#include <iostream>
using namespace std;

int main() {
char *s;
s = "test1";
The above conversion shouldn''t be used. "test1" is of type const
char[7], and you let a pointer to non-const char point to it. That''s
dangerous, because it will seem as if you can change the literal, which
you actually can''t.
cout << "s = " << s << " and &s = " << &s << "\n";

char *s2;
s2 = "foo bar";
cout << "s2 = " << s2 << " and &s2 = " << &s2 << "\n";
cout << "so s = " << s << "\n";

int *i;
*i = 1;
*i is the integer which the pointer i points to. But the pointer isn''t
initialized. It points just into the open sky. The behaviour of the
above assignment is undefined, because it is an attempt to write to
some random memory location that might not even belong to your program.
cout << "i = " << *i << " and &i " << &i << "\n"; //could have used
i instead of &i

int j;
j = 4;
cout << "j = " << j << " and &j " << &j << "\n";

return 0;
}

---

BTW: I know I can initialize on definition.

Output of the program is:
s = test1 and &s = 0xbfffe354
s2 = foo bar and &s2 = 0xbfffe350
so s = test1
Segmentation fault (what else, lol)

--
I wrote this program because i wanted to see what happens when I
assign values to pointers when I don''t initialize them.
You don''t assign to the pointer. You try to assign to what it points to.
If I comment out the s2 part, then the program works (no segfault).

Why would s2 make this problem?
You shouldn''t care. The behaviour of your above program is undefined, so
according to the C++ standard, everything can happen. What you observe
above is just one instance of "undefined behaviour". Note that the
result can depend on anything. It might be different on different
compilers or on the same compiler on a different OS or hardware, or
even on the same system with different compiler settings or a newer
version of the same compiler. It isn''t worth trying to find out why
something specific happens when you invoke undefined behaviour.
Oh, as I said, if I remove the s2 part it works fine and j''s address
is 4bytes before i (the pointer). So the compiler allocates memory
with decreasing order? Or is this platform dependent?



It is platform dependant.


On Tue, 10 Feb 2004 19:21:50 +0530 in comp.lang.c++, "Sharad Kala"
<no*****************@yahoo.com> was alleged to have written:

I wrote this program because i wanted to see what happens when I
assign values to pointers when I don''t initialize them.


So you agree now that pointers are evil :-) (I actually don''t).



Uninitialized pointers are always evil.


这篇关于奇怪的指针错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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