Initialize a struct handler

Hi, guys.

I'm a beginner C++ programmer. Imagine you have this data structure:

1
2
3
4
5
6
7
8
9
10
typedef struct {
	int num;
	double height;
	string name;
	} TD2;

typedef struct {
	int size;
	TD2 array[1];
	} TD1 **TD1Hdl;


I need to initialize a variable of type TD1Hdl. What do I have to do?

I searched on the forums, specially I tried to use what is stated here:
http://www.cplusplus.com/forum/general/10941/
and here:
http://www.cplusplus.com/forum/beginner/8199/

but I was not able to put it into practice in my problem...

Thank you so much,
Francisco
Last edited on
TD1Hdl is declared as a pointer to pointer to TD1, to initialize it you need a pointer to TD1 which needs an object of TD1
or you can initialize it to NULL

The above code is C. If you are using C++, write a constructor

Hi, Bazzy.

Thanks for your fast reply. I'm using C++, but this datatype is imported from LabView.

I cannot understand you clearly. Could you please post some code example?

I have modified the TD2 struct and added one class object:

1
2
3
4
5
6
7
8
9
10
11
typedef struct {
	int num;
	double height;
	string name;
        class1 object1;
	} TD2;

typedef struct {
	int size;
	TD2 array[1];
	} TD1 **TD1Hdl



My program is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Function(TD1Hdl Object1)

{

TD1Hdl Object2;

(*Object2)->size = (*Object1)->size;

for (int i=0; i<(*Object1)->size; i++)
      (*Object2)->array[i].num=(*Object1)->array[i].num;

// and continue with the rest of fields...

}


Object1 doesn't need to be initialized because its value is a parameter of the function that is called.

I get an exception error on the first line (when "size" takes its value). So I think the origin of the problem is that Object2 isn't initialized properly.

Thank you so much,
Francisco
Last edited on
Please, help... :(
I'm a beginner C++ programmer


Are you sure you're even learning C++? As Bazzy mentioned this looks more like C.



1
2
3
4
typedef struct {
	int size;
	TD2 array[1];
	} TD1 **TD1Hdl


My C is rusty... but would that even work? Don't you need a comma there?

1
2
3
typedef struct {
//...
} TD1, **TD1Hdl;



Anyway, pointers aren't worth anything unless they actually point to something.

1
2
3
4
5
TD1Hdl Object2;  // Object2 Doesn't point to anything

(*Object2)->size = //...
// so the above line is very, very bad because you're dereferencing a pointer that doesn't point to
//  anything. This compiles OK but will cause big, big, big problems in your program. 



The real question here is why do you even need a pointer to a pointer? Why can't you just use TD1's? What is the purpose of TD1Hdls?
Disch is right - you need a comma in there or it's not even going to compile.

Some examples of usage:

1
2
3
4
TD1     td1,td1_another;
TD1    *ptd1   = &td1;
TD1Hdl  td1hdl = &ptd1;
*td1hdl        = &td1_another;


My guess is, the OP may be dealing with a third party C library that. In the early days of C++, before references were added, handles were a way of changing a parameter pointer's value.

1
2
3
4
5
6
7
8
9
10
11
12
void newTD1( TD1Hdl hdl )
{
  *hdl = new TD1; 
}

void equivalentToPassPointerByRef()
{
  TD1* td1ptr;
  TD1Hdl  td1hdl = &td1ptr;
  newTD1( td1hdl );
  delete *td1hdl;
}


In other words, it's a trick to pass a pointer by reference, before there were references.

EDIT: Also, in the early days of the Mac OS, handles were used to deal with memory fragmentation. Double indirection allowed the OS to compact the memory. However, programmers had to be careful to manage the underlying pointers carefully, or face the risk of them becoming invalid by the time they were used. More notes here under fragmentation: http://en.wikipedia.org/wiki/Mac_OS_memory_management
Last edited on
Hi, Disch and kfmfe04. Thanks for your answers.

Well, I didn't go into the origin of this datatype, because I didn't want to confuse you. This is a data structure that is imported from LabView. In my application, I make LabView call a C++ dll, and I need to transfer the data from the first to the second. In LabView there are tools that convert a LabView data structure into a C/C++ data structure.

So, imagine this is the data structure I get from LabView:

1
2
3
4
5
6
7
8
9
10
typedef struct {
	int num;
	double height;
	string name;
	} TD2;

typedef struct {
	int size;
	TD2 array[1];
	} TD1 **TD1Hdl;


Without comma I don't get a compiler error. I think using typedef you make TD1 and **TDH1Hdl to be equivalent, so TD1Hdl is a handler of TD1.

When I call the entry point of the dll from LabView, I call a function that pass a parameter of type TD1Hdl.

void function(TD1Hdl LabViewData);

Like I said, both the data structure and the function prototype are generated from LabView.

Inside my C++ dll, I include this code parts provided. Then, I need to take the data belonging to the variable I get from LabView and copy it to another data structure I have to create in C++, similar to the first, but including more information, like classes. For example:

1
2
3
4
5
6
7
8
9
10
11
12
typedef struct {
	int num;
	double height;
	string name;
	class1* object1;
	class2 object2;
	} TD4;

typedef struct {
	int size;
	TD4 array[1];
	} TD3;


For example, I have to create a variable called DllData of type TD3, and copy all the information contained in variable LabViewData in the corresponding fields of the new structure.

Sorry for my English, and please, if you need me to be clearer, let me know, and I'll try to explain it better.

Thanks in advance.

Regards,
Francisco

Last edited on
I recommend that you look at the documentation for LabView.

Handles used this way are often "opaque" and not meant to be manipulated by the programmer. There may be an initializer function in the library which takes that handle as an argument. That's what I would look for if I were you. If you find an initializer, also look for some kind of release (like free()) type function for cleanup.
Topic archived. No new replies allowed.