473,888 Members | 1,662 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 1512
Mike Wahler wrote:
The difference between our assertions is that you qualified
with 'POD' (and I used 'automatic' instead of 'default').
Yes, without the qualification of POD types, your statement
is wrong. The defect in the language is that automatic POD
types are not default initalized when other non-POD types
would be.


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?)


Default initialization, i.e., those without explicit initializers.
Jul 22 '05 #21
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.


Right, there is no prohibition. The compiler is just not required
to initialize POD types in certain circumstances.

This whole initialization debacle, and the bandaids the standards
committee put on it with value initailziation is just odiferous slavish
following of the way things were always done in C. It causes more
problems than it solves, and if it were changed today, it would break
no code (although some people who rely on not using RAII might get a
little slower). If you want to skip default initialization, I think
that should be the explicit case.
Jul 22 '05 #22
Ron Natalie wrote:
Yes, without the qualification of POD types, your statement
is wrong. The defect in the language is that automatic POD
types are not default initalized when other non-POD types
would be.


Actually it is not a defect.
Check this message:

http://groups.google.com/groups?hl=e...rldnet.att.net

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #23

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
25301
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
11176
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
10772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
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...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7988
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
7144
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6012
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4638
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

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.