473,387 Members | 1,486 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,387 software developers and data experts.

Pointer Problem

Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and works:

ClassA instance1;

int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}

Is there something wrong with my code here? Is the problem in doFuntion?
Any help would be greatly appreciated. Sorry for the horrible
function/class names. Using VC++

Thanks
Connell
Jul 22 '05 #1
9 1809
Connell Gauld wrote:
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
All public? Any constructors? Any reference members? We may be
looking at a POD, then again we may not. Not enough information to
know for sure.
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and
works:

ClassA instance1;

int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}

Is there something wrong with my code here? Is the problem in doFuntion?
The only thing that I can see here that may make a difference is that
the global object 'instance1' is filled with 0s before initialising
to whatever, but when you say

new ClassA;

the object _may_ be left uninitialised, and that means that the value
of 'myVar' is whatever came with the memory 'new' is giving you. Try
replacing it with

new ClassA();

and see if there is a difference. Otherwise, make sure you initialise
all members to something by adding a default constructor to ClassA.
Any help would be greatly appreciated. Sorry for the horrible
function/class names. Using VC++


VC++ or not shouldn't matter. If you, however, find that your code
works fine if compiled by another compiler, and doesn't if compiled
by VC++, then you should ask in microsoft.public.vc.language for
assistance, perhaps it's a bug. But if not, then there is no
difference what compiler you're using.

V
Jul 22 '05 #2
On Thu, 22 Jul 2004 22:42:02 +0100, Connell Gauld
<co*****@freebreakfast.co.uk> wrote:
Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and
works:

ClassA instance1;

int main()
{
//...
I'm guesing that you missed out

ClassA instance1;

here.
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}

Is there something wrong with my code here?
No
Is the problem in doFuntion?
Maybe
Any help would be greatly appreciated.


This sort of wierd bug, where slight, seemingly unimportant, changes make
the difference between you code working and not working is a sure sign
that your code is bugged. Unfortunately it tells you very little about
where the bug is. There is nothing wrong with the code you've posted.

VC++ has an excellent debugger. The best suggestion I can make is that you
learn how to use it. Alternatively reduce your program in size until it is
small enough to post here and then post the entire program.

john
Jul 22 '05 #3
John Harrison wrote:
On Thu, 22 Jul 2004 22:42:02 +0100, Connell Gauld
<co*****@freebreakfast.co.uk> wrote:
Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area,
a variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which
takes a pointer to a ClassB as a parameter. Now, the following
compiles and works:

ClassA instance1;

int main()
{
//...

I'm guesing that you missed out

ClassA instance1;


I'm guessing that you missed it a few lines up, in the global scope.

And it does make a lot of difference. Read my reply to the OP.

here.
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}
[...]


V
Jul 22 '05 #4
On Thu, 22 Jul 2004 17:59:34 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
John Harrison wrote:
I'm guessing that you missed it a few lines up, in the global scope.

And it does make a lot of difference. Read my reply to the OP.


Right, sorry about that.

john
Jul 22 '05 #5
Thanks guys! I used the debug facility (which I should have done before,
not enough coffee today) and found the problem deep inside the the
structure, an uninitialised int.

Thanks again,
Connell
Jul 22 '05 #6
jmh
Victor Bazarov wrote:
John Harrison wrote:

.. . .
I'm guesing that you missed out

ClassA instance1;

I'm guessing that you missed it a few lines up, in the global scope.

And it does make a lot of difference. Read my reply to the OP.


Just to clarify for someone who is just learning, if
ClassA instance1; is placed in main(){ ... }
it doesn't get initialized but when places in
the global scope it does?

jmh

Jul 22 '05 #7
"Connell Gauld" <co*****@freebreakfast.co.uk> wrote in message
news:cd**********@newsg3.svr.pol.co.uk...
Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and works:
ClassA instance1;

int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);


Shouldn't this be
instance2.doFunction(ptrInstance1->myVar);

--
Gary
Jul 22 '05 #8
"jmh" <j_***@cox.net> wrote...
Victor Bazarov wrote:
John Harrison wrote:

. . .
I'm guesing that you missed out

ClassA instance1;

I'm guessing that you missed it a few lines up, in the global scope.

And it does make a lot of difference. Read my reply to the OP.


Just to clarify for someone who is just learning, if
ClassA instance1; is placed in main(){ ... }
it doesn't get initialized but when places in
the global scope it does?


Yes. An object with static storage duration is different than
one with automatic storage duration if the object is a POD. The
storage for static objects is initialised with 0 before program
begins.

V
Jul 22 '05 #9
On Thu, 22 Jul 2004 19:25:48 -0400, jmh <j_***@cox.net> wrote:
Victor Bazarov wrote:
John Harrison wrote:

. . .
I'm guesing that you missed out

ClassA instance1;

I'm guessing that you missed it a few lines up, in the global scope.
And it does make a lot of difference. Read my reply to the OP.


Just to clarify for someone who is just learning, if
ClassA instance1; is placed in main(){ ... }
it doesn't get initialized but when places in
the global scope it does?


Anything with a constructor will get initialised where ever it is placed.
The difference is with constructorless types like ints and pointers. At
global scope they get initialised to zero, at local scope they are garbage.

john
Jul 22 '05 #10

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

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.