🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages Started by pnunn Thu, 04 May 2006 13:33
Basic C++ Question...
#3746
Author: pnunn
Date: Thu, 04 May 2006 13:33
45 lines
2342 bytes
Folks.. I'm stuck on a problem that should be easy to sort out but I 
can't get to work.

I have a base class defined that has a pointer to a structure in it.

struct stateTable *table;

and a public function setTable that takes a passed in pointer and sets 
table to it...

In the derived class I have (in the constructor)

    struct stateLine binTable[] =
    {
	   //current state, A0, A1, next state
	   {1, High, Low, 2},
	   {1, High, High, 4},
	   {1, Low, High, 5},
	   {2, Low, Low, 1},
	   {2, High, High, 4},
	   {4, High, Low, 3},
	   {3, Low, Low, 1},
	   {3, Low, High, 4},
	   {5, High, High, 4},
	   {5, Low, Low, 1}
    };

    setTable(binTable);

Now.. when I go to use table in other functions.. table points to 
nothing useful.. and I guess its because as binTable goes out of scope, 
so does the memory its using... so I guess I can see why its not 
working, but, I can't work out how to make it work.

If I have a stateTable* in the derived class, how do I dynamically make 
the table and pass it to the base class?

I'd really rather build the table as the above code shows, but then how 
do I pass that to the base class and have it still be there?

I know this is basic C++, but any help greatly appreciated.

Ta.

Peter.
Re: Basic C++ Question...
#3747
Author: Pete Fraser
Date: Thu, 04 May 2006 13:48
51 lines
2545 bytes
I think we need to see the code in the base class that sets the table ptr.
Also, do you have a *table in the derived classes?
Rgds Pete

"pnunn" <pnunn@infoteq.com.au> wrote in message 
news:445a02bf$1@newsgroups.borland.com...
> Folks.. I'm stuck on a problem that should be easy to sort out but I can't 
> get to work.
>
> I have a base class defined that has a pointer to a structure in it.
>
> struct stateTable *table;
>
> and a public function setTable that takes a passed in pointer and sets 
> table to it...
>
> In the derived class I have (in the constructor)
>
>    struct stateLine binTable[] =
>    {
>    //current state, A0, A1, next state
>    {1, High, Low, 2},
>    {1, High, High, 4},
>    {1, Low, High, 5},
>    {2, Low, Low, 1},
>    {2, High, High, 4},
>    {4, High, Low, 3},
>    {3, Low, Low, 1},
>    {3, Low, High, 4},
>    {5, High, High, 4},
>    {5, Low, Low, 1}
>    };
>
>    setTable(binTable);
>
> Now.. when I go to use table in other functions.. table points to nothing 
> useful.. and I guess its because as binTable goes out of scope, so does 
> the memory its using... so I guess I can see why its not working, but, I 
> can't work out how to make it work.
>
> If I have a stateTable* in the derived class, how do I dynamically make 
> the table and pass it to the base class?
>
> I'd really rather build the table as the above code shows, but then how do 
> I pass that to the base class and have it still be there?
>
> I know this is basic C++, but any help greatly appreciated.
>
> Ta.
>
> Peter. 
Re: Basic C++ Question...
#3748
Author: Alan Bellingham
Date: Thu, 04 May 2006 14:17
56 lines
2640 bytes
pnunn <pnunn@infoteq.com.au> wrote:

>Folks.. I'm stuck on a problem that should be easy to sort out but I 
>can't get to work.
>
>I have a base class defined that has a pointer to a structure in it.
>
>struct stateTable *table;
>
>and a public function setTable that takes a passed in pointer and sets 
>table to it...
>
>In the derived class I have (in the constructor)
>
>    struct stateLine binTable[] =
>    {
>	   //current state, A0, A1, next state
>	   {1, High, Low, 2},
>	   {1, High, High, 4},
>	   {1, Low, High, 5},
>	   {2, Low, Low, 1},
>	   {2, High, High, 4},
>	   {4, High, Low, 3},
>	   {3, Low, Low, 1},
>	   {3, Low, High, 4},
>	   {5, High, High, 4},
>	   {5, Low, Low, 1}
>    };
>
>    setTable(binTable);

Now, if the definition of this function is

  void setTable(stateTable * newTable)
  {
     table = newTable;
  }

then you are rightly screwed on two accounts. The first is that your
table in the constructor is on the stack, and the moment the constructor
completes, that stack memory gets used by other things.

Secondly, you are passing a pointer to stateLine to a function that
expects a pointer to stateTable. Given that this compiles, I expect that
a stateLine is derived from a stateTable. This is fine, ONLY so long as
you are passing a pointer to a single element, because with a single
element, you know better than to try indexing on that pointer. In your
case, I expect the users of table expect it to be a pointer to an array.
Bad news, because the pointer arithmetic assumes the base class.

Oh, failure three - how does the code know how many elements in the
array?

Alan Bellingham
-- 
ACCU Conference 2007 - venue to be determined.
Re: Basic C++ Question...
#3755
Author: pnunn
Date: Fri, 05 May 2006 04:18
77 lines
3370 bytes
Alan Bellingham wrote:
> pnunn <pnunn@infoteq.com.au> wrote:
> 
> 
>>Folks.. I'm stuck on a problem that should be easy to sort out but I 
>>can't get to work.
>>
>>I have a base class defined that has a pointer to a structure in it.
>>
>>struct stateTable *table;
>>
>>and a public function setTable that takes a passed in pointer and sets 
>>table to it...
>>
>>In the derived class I have (in the constructor)
>>
>>   struct stateLine binTable[] =
>>   {
>>	   //current state, A0, A1, next state
>>	   {1, High, Low, 2},
>>	   {1, High, High, 4},
>>	   {1, Low, High, 5},
>>	   {2, Low, Low, 1},
>>	   {2, High, High, 4},
>>	   {4, High, Low, 3},
>>	   {3, Low, Low, 1},
>>	   {3, Low, High, 4},
>>	   {5, High, High, 4},
>>	   {5, Low, Low, 1}
>>   };
>>
>>   setTable(binTable);
> 
> 
> Now, if the definition of this function is

Yes, thats basically what the function does.
> 
>   void setTable(stateTable * newTable)
>   {
>      table = newTable;
>   }
> 
> then you are rightly screwed on two accounts. The first is that your
> table in the constructor is on the stack, and the moment the constructor
> completes, that stack memory gets used by other things.

Ok, that's the conclusion I came to last night.

> 
> Secondly, you are passing a pointer to stateLine to a function that
> expects a pointer to stateTable. Given that this compiles, I expect that
> a stateLine is derived from a stateTable. This is fine, ONLY so long as
> you are passing a pointer to a single element, because with a single
> element, you know better than to try indexing on that pointer. In your
> case, I expect the users of table expect it to be a pointer to an array.
> Bad news, because the pointer arithmetic assumes the base class.

Bugger, missed that one.. No, its not a derived class at all, its an 
array of stateLine elements.  Wonder why it compiled at all.

> 
> Oh, failure three - how does the code know how many elements in the
> array?

Ah, I used sizeof(table)/sizeof(*table);

> 
> Alan Bellingham

So... what should I be doing?  Guess I've got to use new to make the 
table and the elements, then pass this pointer. Is that right?  How 
should I go about this?

Ta.

Peter.
Re: Basic C++ Question...
#3756
Author: pnunn
Date: Fri, 05 May 2006 04:20
67 lines
2843 bytes
Hi Pete,

Pete Fraser wrote:
> I think we need to see the code in the base class that sets the table ptr.
> Also, do you have a *table in the derived classes?
> Rgds Pete

The funciton basically takes a pointer and sets the derived class value 
to that (I know that's not going to do it now).

I don't have a *table in the dervide classes.

Pete.


> 
> "pnunn" <pnunn@infoteq.com.au> wrote in message 
> news:445a02bf$1@newsgroups.borland.com...
> 
>>Folks.. I'm stuck on a problem that should be easy to sort out but I can't 
>>get to work.
>>
>>I have a base class defined that has a pointer to a structure in it.
>>
>>struct stateTable *table;
>>
>>and a public function setTable that takes a passed in pointer and sets 
>>table to it...
>>
>>In the derived class I have (in the constructor)
>>
>>   struct stateLine binTable[] =
>>   {
>>   //current state, A0, A1, next state
>>   {1, High, Low, 2},
>>   {1, High, High, 4},
>>   {1, Low, High, 5},
>>   {2, Low, Low, 1},
>>   {2, High, High, 4},
>>   {4, High, Low, 3},
>>   {3, Low, Low, 1},
>>   {3, Low, High, 4},
>>   {5, High, High, 4},
>>   {5, Low, Low, 1}
>>   };
>>
>>   setTable(binTable);
>>
>>Now.. when I go to use table in other functions.. table points to nothing 
>>useful.. and I guess its because as binTable goes out of scope, so does 
>>the memory its using... so I guess I can see why its not working, but, I 
>>can't work out how to make it work.
>>
>>If I have a stateTable* in the derived class, how do I dynamically make 
>>the table and pass it to the base class?
>>
>>I'd really rather build the table as the above code shows, but then how do 
>>I pass that to the base class and have it still be there?
>>
>>I know this is basic C++, but any help greatly appreciated.
>>
>>Ta.
>>
>>Peter. 
> 
> 
> 
Re: Basic C++ Question...
#3757
Author: pnunn
Date: Fri, 05 May 2006 07:45
74 lines
3085 bytes
OK, still struggling with this one..

Here's the latest.

In base class..  struct stateLine *table;

In derived class axleTable = new struct stateLine[noLines];
    axleTable[0] = new stateLine(1 , High, Low, 2);
    axleTable[1] = new stateLine(1 , Low, High, 7);
    axleTable[2] = new stateLine(1 , Low, Low, 5);
    axleTable[3] = new stateLine(2 , Any, High, 3);

Then the idea is to send this pointer to the base class so it can be 
used there.

However,
This fails to compile with a couple of errors.
1. No default constructor for stateLine..

So, I provide one of these, then we get errors saying that you can't 
convert stateLine to stateLine *.

What am I doing wrong here?  Any help please.

ta.

Peter.

pnunn wrote:
> Folks.. I'm stuck on a problem that should be easy to sort out but I 
> can't get to work.
> 
> I have a base class defined that has a pointer to a structure in it.
> 
> struct stateTable *table;
> 
> and a public function setTable that takes a passed in pointer and sets 
> table to it...
> 
> In the derived class I have (in the constructor)
> 
>    struct stateLine binTable[] =
>    {
>        //current state, A0, A1, next state
>        {1, High, Low, 2},
>        {1, High, High, 4},
>        {1, Low, High, 5},
>        {2, Low, Low, 1},
>        {2, High, High, 4},
>        {4, High, Low, 3},
>        {3, Low, Low, 1},
>        {3, Low, High, 4},
>        {5, High, High, 4},
>        {5, Low, Low, 1}
>    };
> 
>    setTable(binTable);
> 
> Now.. when I go to use table in other functions.. table points to 
> nothing useful.. and I guess its because as binTable goes out of scope, 
> so does the memory its using... so I guess I can see why its not 
> working, but, I can't work out how to make it work.
> 
> If I have a stateTable* in the derived class, how do I dynamically make 
> the table and pass it to the base class?
> 
> I'd really rather build the table as the above code shows, but then how 
> do I pass that to the base class and have it still be there?
> 
> I know this is basic C++, but any help greatly appreciated.
> 
> Ta.
> 
> Peter.
Re: Basic C++ Question...
#3758
Author: Alan Bellingham
Date: Fri, 05 May 2006 09:11
30 lines
2005 bytes
pnunn <pnunn@infoteq.com.au> wrote:

>Bugger, missed that one.. No, its not a derived class at all, its an 
>array of stateLine elements.  Wonder why it compiled at all.

Better check your code - it looks like something is seriously confused,
then.

>> Oh, failure three - how does the code know how many elements in the
>> array?
>
>Ah, I used sizeof(table)/sizeof(*table);

'table' is your pointer - size 4 bytes. Isn't it?

>So... what should I be doing?  Guess I've got to use new to make the 
>table and the elements, then pass this pointer. Is that right?  How 
>should I go about this?

I'd use a std::vector<>, myself. I really don't trust raw ownership
pointers. Sooner or later, you write some code somewhere that forgets to
clean up correctly. Or that doesn't handle an exception at a most
embarrassing time. Better to use components that always clean themselves
up.

Oh, and the std::vector<> *knows* its size.

Alan Bellingham
-- 
ACCU Conference 2007 - venue to be determined.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads