473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer problem

Hi there,

I've got a class with an array of pointers to objects of the same class. I
thought of doing it like that:
class test {
test **array_var;
...
};
void method::test(test object)
{
children = (Baum**) realloc(children, sizeof(Baum**));
children[elements] = (Baum*) malloc(sizeof(Baum*));
children[elements] = &obj;
}

test a;
test b;
a.method(b);

I tried to access b like:
a.children[0].var_of_b;
but it did not work. So how can I manage it?
Thanks for help

Thomas

Jul 19 '05 #1
2 2211
Well if you really want to...
But look into the STL containers instead...

#include <iostream>
class X{
public:
X():number(10){};
int number;
};
typedef X* XPtr;

int main(int argc, char* argv[])
{

XPtr* Arr = new XPtr[10];

Arr[0] = new X;

std::cout << "this should say 10 : " << Arr[0]->number << std::endl;
return 0;
}


"Thomas Baier" <th****@tho-bai.de> wrote in message
news:3f**********************@newsread2.arcor-online.net...
Hi there,

I've got a class with an array of pointers to objects of the same class. I
thought of doing it like that:
class test {
test **array_var;
...
};
void method::test(test object)
{
children = (Baum**) realloc(children, sizeof(Baum**));
children[elements] = (Baum*) malloc(sizeof(Baum*));
children[elements] = &obj;
}

test a;
test b;
a.method(b);

I tried to access b like:
a.children[0].var_of_b;
but it did not work. So how can I manage it?
Thanks for help

Thomas

Jul 19 '05 #2
Thomas Baier wrote:
Hi there,

I've got a class with an array of pointers to objects of the same class.
This does not seem to be what you have at all, so either your
description is off or your code is. I'm not entirely sure which.
I
thought of doing it like that:
class test {
test **array_var;
This is, of course, not an array at all, but a pointer. Now, if I assume
the description you gave is accurate, then I don't think you want
'array_var' to be of type 'test **', but rather of type 'test *'.
...
};
void method::test(test object)
You haven't given any information about this 'method' class (or namespace?).
{
children = (Baum**) realloc(children, sizeof(Baum**));
A few problems here:

1) This is an unsafe way of using realloc. If the reallocation fails,
you lose the original memory. The only safe way to use realloc is to
store the resulting value in a new temporary pointer, then assign it to
the original pointer if and only if the allocation succeeds.

2) Please don't use realloc in C++. It's dangerous, error-prone, won't
work correctly for non-POD types, and there's a better alternative:
std::vector.

Also, having no idea what 'children' or 'Baum' are, I can't properly
diagnose problems with this code.
children[elements] = (Baum*) malloc(sizeof(Baum*));
children[elements] = &obj;
One of the problems here should be obvious. On one line you store a
pointer to some new memory. On the next you overwrite that pointer. You
have leaked the memory you allocated.

But again, I can't properly diagnose problems due to lack of information.
}

test a;
test b;
a.method(b);
More stuff I don't know about: the 'method' member of test.

I tried to access b like:
a.children[0].var_of_b;
And again: the 'children' member of test.
but it did not work. So how can I manage it?


I really don't know what you are doing, so I can't say. Honestly, I
suspect the code you posted is far from the code you are actually
attempting to use. It kind of looks like you've changed some variable
names and transposed some identifiers.

Post real code, a minimum complete program, and we can probably help.
With what you've given us... there's basically nothing we can do.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

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: 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
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.