473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Weird pointer issue

Hi

I'm having the following very rare problem with abstarct class
pointers.

I have, let't say, the abstract class A and the concrete class C that
implements A.
The problem is that when I was trying to make a collection of A objects
and did:

A * a = new C;
colectionOfAs[n] = a;

and realized that all a's in the array were pointing to the same place
in memory,
Then I tried:

A *a, *b;
a = new C;
b = new C;

and to my surprise a == b was true!

Any ideas?
isn't "new" supoused to give me a NEW pointer?

Thank in advance.

Aug 1 '06 #1
6 1173

"p1r0" <lu***************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Hi

I'm having the following very rare problem with abstarct class
pointers.

I have, let't say, the abstract class A and the concrete class C that
implements A.
The problem is that when I was trying to make a collection of A objects
and did:

A * a = new C;
colectionOfAs[n] = a;

and realized that all a's in the array were pointing to the same place
in memory,
Then I tried:

A *a, *b;
a = new C;
b = new C;

and to my surprise a == b was true!
(From this second example, I'll assume that n varied in the first example,
correct?)

There's no obvious reason that a and b should have the same value, from just
what you've shown. Perhaps it's simply how you're viewing/outputting the
values? If you could post a complete, compilable, but minimal example of
code that exhibits this behavior, we'll gladly look at it.

-Howard
Aug 1 '06 #2
p1r0 wrote:
I'm having the following very rare problem with abstarct class
pointers.

I have, let't say, the abstract class A and the concrete class C that
implements A.
The problem is that when I was trying to make a collection of A
objects and did:

A * a = new C;
colectionOfAs[n] = a;
What's 'colectionOfAs'? How is it declared?
and realized that all a's in the array were pointing to the same place
in memory,
Huh?
Then I tried:

A *a, *b;
a = new C;
b = new C;

and to my surprise a == b was true!
Huh?
Any ideas?
I think this falls under FAQ 5.8.
isn't "new" supoused to give me a NEW pointer?
Well, yes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #3
p1r0 posted:
A *a, *b;
a = new C;
b = new C;

and to my surprise a == b was true!

Perhaps that could be possible if you had:

A *p1, *p2;

p1 = new(nothrow) C;
p2 = new(nothrow) C;

if(p1 == p2)
{
if(!p1)
{
/* Null pointers, that explains something */
}
else
{
/* I'm a flawed compiler! */
}
}

--

Frederick Gotham
Aug 1 '06 #4
Thank you all for the raplies!

Howard:

"If you could post a complete, compilable, but minimal example of
code that exhibits this behavior, we'll gladly look at it."

I'm currently not at home and don't have the code with me. The code is
kind of big for posting but I'll see if I can come up with a short
version of it.
Frederick Gotham:

I'm shure that a and b are not NULL

Aug 1 '06 #5
>
A *a, *b;
a = new C;
b = new C;

and to my surprise a == b was true!

Any ideas?
isn't "new" supoused to give me a NEW pointer?
Operator new is indeed supposed to give a new pointer. There are
exceptions though, like when class C overloads the operator new...
Thank in advance.
Ben

Aug 2 '06 #6
benben wrote:
>
Operator new is indeed supposed to give a new pointer. There are
exceptions though, like when class C overloads the operator new...
The operator new function overload isn't an implementation of the
new operator (despite it's unfortunate name) but the memory allocator
used by that operator. While it's possible that the allocator could
return the same value twice, it's hard to see how that would be useful
(and would certainly take some collusion in the constructor and even
then if there are virtual functions things may get dicey).
Aug 2 '06 #7

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

Similar topics

1
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different...
11
by: Anon Email | last post by:
Hey people, This looks really weird. I can't make sense of it. Is this a mistake? Can anyone help? IStack const & GetStack () const; Cheers,
10
by: Chris Mantoulidis | last post by:
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 << "...
5
by: Naveen Parihar | last post by:
I've a binary that runs on most of our servers but segfaults on one of the servers. Further, even on this specific machine, the binary runs successfully sometimes but segfaults most of the time....
8
by: XYZ | last post by:
I need to pass the address of a variableto some win32 API functions like RTLMoveMemory I noticed some weird behavior when pinning objects and using the handle's pointer to write data to the...
4
by: jedi200581 | last post by:
Hi, I'm new at python as I just started to learn it, but I found out something weird. I have wrote a little program to compute Mersenne number: # Snipet on def is_prime n: for i in range(2,...
1
by: Frederick Dean | last post by:
Hi, guys! Please look at the very simple code below: //---------------code begin------------------------ void f() { cout << "f function " << endl; } void (*pf)() = &f;
12
by: MQ.john | last post by:
//Working Example: #include <stdio.h> #include <time.h> int main () { time_t rawtime; /* define rawtime as time_t */ time ( &rawtime );
4
by: harsh.murari | last post by:
In the example below, a single object (Network) is implementing 2 interfaces (INetworkA and INetworkB). Both of these interfaces derive from the IBase interface (similar to the IUnknown interface...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.