473,857 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why doesn't this work? (pointers)

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...
Jul 22 '05
22 1510
Mike Wahler wrote:
That is not a 'fact'. The fact is that objects of automatic
storage duration are not automatically initialized. Their
type doesn't matter. Objects with static storage duration
are automatically initialized (to 0).

Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.
Jul 22 '05 #11
Ron Natalie wrote:
Mike Wahler wrote:
That is not a 'fact'. The fact is that objects of automatic
storage duration are not automatically initialized. Their
type doesn't matter. Objects with static storage duration
are automatically initialized (to 0).

Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.


Are required to not be? Or not required to be?


Brian Rodenborn
Jul 22 '05 #12

"Default User" <fi********@boe ing.com.invalid > wrote in message
news:I5******** @news.boeing.co m...
Ron Natalie wrote:
Mike Wahler wrote:
That is not a 'fact'. The fact is that objects of automatic
storage duration are not automatically initialized. Their
type doesn't matter. Objects with static storage duration
are automatically initialized (to 0).

Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.


Are required to not be? Or not required to be?


I see neither a requirement for nor a prohibition of
it in the standard, so I presume the latter. Which
of course would mean that code intended to be portable
should not depend upon default initialization of such
objects.

-Mike
Jul 22 '05 #13
"Mike Wahler" <mk******@mkwah ler.net> writes:
"Default User" <fi********@boe ing.com.invalid > wrote in message
news:I5******** @news.boeing.co m...
Ron Natalie wrote:
Mike Wahler wrote:

> That is not a 'fact'. The fact is that objects of automatic
> storage duration are not automatically initialized. Their
> type doesn't matter. Objects with static storage duration
> are automatically initialized (to 0).
>
Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.


Are required to not be? Or not required to be?


I see neither a requirement for nor a prohibition of
it in the standard, so I presume the latter. Which
of course would mean that code intended to be portable
should not depend upon default initialization of such
objects.


100% ACK. IMHO it is the best way to initialisize everything by
hand. Different compilers would solve the problem of default
initialzation with different ways.

Kind regards,
Nicolas
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tug raz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #14
I believe you are misunderstandin g the meaning of p = q in the pointer
world.

source code reformated
int main()
{
int *p, *q; Now p and q are created (not initialized)
----- -----
| p |--->o | q |
----- pointing to a -----
random place | pointing to another
V random place
o p = q; Now both p and q point to the same random place (the place where q
points to)

----- -----
| p | | q |
----- -----
| |
| V
\----------------------->o
q = new int; Now q points to a valid place where an int should be placed. Note that
p still points to the random place q used to point

----- ----- -----
| p | | q |------>| |
----- ----- -----
|
|
\----------------------->o
*q = 12; Now 12 is copied to the new int storage create by new and pointed by q
----- ----- -----
| p | | q |------>| 12|
----- ----- -----
|
|
\----------------------->o cout << *p; Now it should be clear why the output is garbage or crash. UB as
others have pointed out. system("pause") ;
return 0;
}


Good luck

Marcelo Pinto
Jul 22 '05 #15

"Default User" <fi********@boe ing.com.invalid > wrote in message
news:I5******** @news.boeing.co m...
Ron Natalie wrote:
Mike Wahler wrote:
That is not a 'fact'. The fact is that objects of automatic
storage duration are not automatically initialized. Their
type doesn't matter. Objects with static storage duration
are automatically initialized (to 0).

Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.


Are required to not be? Or not required to be?


To be or not to be. :-)

-Mike
Jul 22 '05 #16
Default User wrote:
Ron Natalie wrote:

Mike Wahler wrote:

That is not a 'fact'. The fact is that objects of automatic
storage duration are not automatically initialized. Their
type doesn't matter. Objects with static storage duration
are automatically initialized (to 0).


Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.

Are required to not be? Or not required to be?


Are not required to be.
Jul 22 '05 #17
Default User wrote:
Ron Natalie wrote:

Mike Wahler wrote:

That is not a 'fact'. The fact is that objects of automatic
storage duration are not automatically initialized. Their
type doesn't matter. Objects with static storage duration
are automatically initialized (to 0).


Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.

Are required to not be? Or not required to be?


The point I was trying to make is that Mike's statement is wrong
as well. Objects of non-POD class type are required to be default
initalized. Those of POD type are not. It's one of those gross
stupidities of the C++ language that initializations are conveniently
omitted just because C was similarly defective.
Jul 22 '05 #18
"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...
Default User wrote:
Ron Natalie wrote:

Mike Wahler wrote:
That is not a 'fact'.
Mike:
The fact is that objects of automatic
storage duration are not automatically initialized.

I (apparently unfortunately) used the term 'automatically'
instead of 'default'.
Theirtype doesn't matter. Objects with static storage duration
are automatically initialized (to 0).

Francis:
Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.

The difference between our assertions is that you qualified
with 'POD' (and I used 'automatic' instead of 'default').


Are required to not be? Or not required to be?


The point I was trying to make is that Mike's statement is wrong
as well.

Is it really 'wrong', or merely 'incomplete'?
Objects of non-POD class type are required to be default
initalized.

I think you're confusing me. What about such an object
initialized via an argument? (OR do you mean only those
declarations lacking an initializer?)

-Mike
Jul 22 '05 #19
Ron Natalie wrote:
Default User wrote:
Ron Natalie wrote:
Actually the "fact" is that POD objects of automatic storage
duration are not default initialized.

Are required to not be? Or not required to be?


The point I was trying to make is that Mike's statement is wrong
as well.

I was actually asking a genuine question, although I was pretty sure of
the answer. I get burned every now and then with "things in C++ what is
different than C". I didn't think there was a prohibition, and as I
recall some compilers do that in debug mode.

Brian Rodenborn
Jul 22 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1462
by: Mika Vainio | last post by:
hi everybody, i'm working on the following problem: i need to build a 26-nary tree to save a data dictionary. every letter of the words is represented by a cell (cell has a pointer-vector cell* children). the last letter is a node (node:cell, additional property "pagenr"). now: i understood the principals of polymorph objects and it worked fine with the ususal examples. but in my case it does not! there is no compilation or linking error...
5
2039
by: nick | last post by:
I have the following code: var ocevent = function(v) { alert('javascript event: u clicked '+v); return false; };
149
25298
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
8
1219
by: Rob | last post by:
I have a vector of a class type and when I create an object inside a function and return that and add it to the vector, it doesn't properly keep the data inside it. So I have a few questions: 1. Is this because it loses scope since it wasn't created with "new"? 2. If I do create it with new, but the vector holds objects not pointers, will the vector's "delete" operator function still handle deleting all those pointers? CODE 1:
0
9923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11081
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7939
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5773
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4189
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3214
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.