Connecting Tech Pros Worldwide Help | Site Map

Pointer Problem

  #1  
Old July 22nd, 2005, 05:46 PM
Connell Gauld
Guest
 
Posts: n/a
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
  #2  
Old July 22nd, 2005, 05:46 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Pointer Problem


Connell Gauld wrote:[color=blue]
> 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;
> // ...[/color]

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.
[color=blue]
> }
>
> 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?[/color]

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.
[color=blue]
> Any help would be greatly appreciated. Sorry for the horrible
> function/class names. Using VC++[/color]

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
  #3  
Old July 22nd, 2005, 05:46 PM
John Harrison
Guest
 
Posts: n/a

re: Pointer Problem


On Thu, 22 Jul 2004 22:42:02 +0100, Connell Gauld
<connell@freebreakfast.co.uk> wrote:
[color=blue]
> 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()
> {
> //...[/color]

I'm guesing that you missed out

ClassA instance1;

here.
[color=blue]
> 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?[/color]

No
[color=blue]
> Is the problem in doFuntion?[/color]

Maybe
[color=blue]
> Any help would be greatly appreciated.[/color]

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
  #4  
Old July 22nd, 2005, 05:46 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Pointer Problem


John Harrison wrote:
[color=blue]
> On Thu, 22 Jul 2004 22:42:02 +0100, Connell Gauld
> <connell@freebreakfast.co.uk> wrote:
>[color=green]
>> 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()
>> {
>> //...[/color]
>
>
> I'm guesing that you missed out
>
> ClassA instance1;[/color]

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.
[color=blue]
>
> here.
>[color=green]
>> ClassC instance2;
>> instance2.doFunction(&instance1.myVar);
>> //...
>> }
>> [...][/color][/color]

V
  #5  
Old July 22nd, 2005, 05:46 PM
John Harrison
Guest
 
Posts: n/a

re: Pointer Problem


On Thu, 22 Jul 2004 17:59:34 -0400, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:
[color=blue]
> John Harrison wrote:
>[/color]
[color=blue]
> 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.
>[/color]

Right, sorry about that.

john
  #6  
Old July 22nd, 2005, 05:46 PM
Connell Gauld
Guest
 
Posts: n/a

re: Pointer Problem


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
  #7  
Old July 22nd, 2005, 05:46 PM
jmh
Guest
 
Posts: n/a

re: Pointer Problem


Victor Bazarov wrote:[color=blue]
> John Harrison wrote:[/color]
.. . .[color=blue][color=green]
>> I'm guesing that you missed out
>>
>> ClassA instance1;[/color]
>
>
> 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.[/color]

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

  #8  
Old July 22nd, 2005, 05:46 PM
Gary Labowitz
Guest
 
Posts: n/a

re: Pointer Problem


"Connell Gauld" <connell@freebreakfast.co.uk> wrote in message
news:cdpbsn$u9q$1@newsg3.svr.pol.co.uk...[color=blue]
> 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[/color]
works:[color=blue]
>
> 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);[/color]

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

--
Gary


  #9  
Old July 22nd, 2005, 05:46 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Pointer Problem


"jmh" <j_m_h@cox.net> wrote...[color=blue]
> Victor Bazarov wrote:[color=green]
> > John Harrison wrote:[/color]
> . . .[color=green][color=darkred]
> >> I'm guesing that you missed out
> >>
> >> ClassA instance1;[/color]
> >
> >
> > 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.[/color]
>
> 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?[/color]

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


  #10  
Old July 22nd, 2005, 05:47 PM
John Harrison
Guest
 
Posts: n/a

re: Pointer Problem


On Thu, 22 Jul 2004 19:25:48 -0400, jmh <j_m_h@cox.net> wrote:
[color=blue]
> Victor Bazarov wrote:[color=green]
>> John Harrison wrote:[/color]
> . . .[color=green][color=darkred]
>>> I'm guesing that you missed out
>>>
>>> ClassA instance1;[/color]
>> 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.[/color]
>
> 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?
>[/color]

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
POINTER to POINTER Problem Nosnibor answers 11 November 2nd, 2007 05:38 PM
Multidimensional array pointer problem c19h28o2 answers 6 September 28th, 2006 04:05 AM
Pointer problem: compiles but crashes Rui Maciel answers 2 February 13th, 2006 11:35 PM
interesting pointer problem? Davy answers 12 November 15th, 2005 03:03 AM
interesting pointer problem? Davy answers 12 September 7th, 2005 02:25 AM