为什么这不起作用? (指针) [英] Why doesn't this work? (pointers)

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

问题描述

我编写了以下程序来演示我在C ++中发现的奇怪现象。


#include< iostream>

using namespace std;

int main()

{int * p,* q;

p = q;

q = new int;

* q = 12;

cout<< * p;

系统(暂停);

返回0;

}

您的编译器将当你尝试运行这个程序时,要么给出一个胡言乱语的答案或崩溃。

这是我不明白的:p和q指向相同的记忆

地址。话虽这么说,为什么你不能使用

p和q来访问动态变量?

如果用p创建一个新变量会怎么样?他们不是指向同一个记忆的
吗?他们会互相覆盖吗?


谢谢。


- == Kensu == -

我真的来了在使用自制链表的同时使用它。

你可以想象这是多么令人沮丧...

解决方案

On太阳报,2004年10月10日22:22:00 GMT,Chris Schumacher< ke ***** @ hotmail.com>写道:

q = new int;




这里new为int分配空间,并返回新的位置

对象,因此该赋值会覆盖q的任何先前值。而且,你从
永远不会初始化旧的q,所以即使在这行之前它也充满了垃圾。


你必须找到一个存放int的地方(使用new或变量),然后设置

p和q指向它。在新之后使用


p = q;


执行此操作。


打开编译器中的警告,它可能会检测到使用

未初始化的变量。


-

Ben Caradoc-Davies< be*@wintersun.org>
http://wintersun.org/


Chris Schumacher写道:

我写了以下程序演示我在C ++中发现的奇怪现象。

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

int main()
{int * p,* q;


这里p和q被初始化,因此指向随机。记忆中的区域。


p = q;


这里的随机存储在q中的存储器地址被分配给p。

q = new int;



这里在免费商店中创建一个int,并将其地址分配给

q指针变量。

* q = 12;



这里存储在q中的地址被解除引用,而int指向的是

赋值12.

cout<< * p;



在这里你取消引用随机在

开头分配给p的内存值,你读了一些随机的程序的内存部分,

调用未定义的行为。


-

Ioannis Vranos

http://www23.brinkster.com/noicys


Chris Schumacher写道:

我编写了以下程序来演示我在C ++中发现的奇怪现象。

#include< iostream>
using namespace std;

int main()
{int * p,* q;


1.两个指针变量都没有初始化。

p = q;


2.你将其中一个分配给另一个。

q = new int;


3.你正在分配一大块内存,q存储指向

位置的指针。


* q = 12;


4.您将特定值存储在q指向的内存中。


cout<< * p;




5.(3)和(4)仍然不会影响p。 p仍未

未初始化并取消引用它会导致UB。


正如Ben已经指出的那样,你可以做到

在为q分配内存之后'ass ='$ p $ q'。否则它无论如何也无济于事。


-

Karthik。 http://akktech.blogspot.com

''从我的电子邮件中删除_nospamplz给我发邮件。 ''


I wrote the following program to demonstrate an oddity I found in C++.

#include <iostream>
using namespace std;
int main()
{int *p, *q;
p = q;
q = new int;
*q = 12;
cout << *p;
system("pause");
return 0;
}
Your compiler will either give a gibberish answer or crash when you try
to run this program.
Here''s what I don''t understand: p and q are pointing to the same memory
address. That being said, why can''t you access the dynamic variable using
p as well as q?
What would happen if you created a new variable using p too? Aren''t they
both pointing to the same memory? Would they overwrite each other?

Thanks.

-==Kensu==-
I actually came across this while working with a homemade linked list.
You can imagine how frustrating THAT was...

解决方案

On Sun, 10 Oct 2004 22:22:00 GMT, Chris Schumacher <ke*****@hotmail.com> wrote:

q = new int;



Here new allocates space for an int, and returns the location of the new
object, so this assignment overwrites any previous value of q. And besides, you
never initialised the old q, so it was full of garbage even before this line.

You must find a place to store an int (using new or a variable), then set both
p and q to point to it. Use

p = q;

after the "new" line to do this.

Turn on warnings in your compiler and it will likely detect the use of
uninitialised variables.

--
Ben Caradoc-Davies <be*@wintersun.org>
http://wintersun.org/


Chris Schumacher wrote:

I wrote the following program to demonstrate an oddity I found in C++.

#include <iostream>
using namespace std;
int main()
{int *p, *q;
Here p and q are initialised and thus point to "random" areas in memory.

p = q;
Here the "random" memory address stored in q is assigned to p.
q = new int;

Here an int is created in the free store and its address is assigned to
q pointer variable.
*q = 12;

Here the address stored in q is dereferenced, and the int pointed is
assigned the value 12.
cout << *p;


Here you dereference the "random" memory value assigned to p in the
beginning, and you read some "random" memory portion of the program,
invoking undefined behaviour.

--
Ioannis Vranos

http://www23.brinkster.com/noicys


Chris Schumacher wrote:

I wrote the following program to demonstrate an oddity I found in C++.

#include <iostream>
using namespace std;
int main()
{int *p, *q;
1. Both the pointer variables are not initialized.
p = q;
2. You are assigning one of them to the other.
q = new int;
3. You are allotting a chunk of memory and q stores the pointer to that
location.

*q = 12;
4. You are storing a particular value in the memory pointed to , by q.

cout << *p;



5. (3) and (4) still do not affect p anyway. p is still
uninitialized and dereferencing it results in UB.

As Ben had already pointed out, you can do
the assignemnt ''p = q'' after allocating memory for q.
Otherwise it does not help anyway.

--
Karthik. http://akktech.blogspot.com .
'' Remove _nospamplz from my email to mail me. ''


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

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