Templates, syntax and allocation!

M

Mike

Hey!

I've started to use templates for storing arrays of doubles.
The template itself will then be passed on to a linked-list.

My problem is that Im not 100% familiar with how templates
are handeled. What I want to do is to allocate it on the heap
then pass it to my linked-list as a part of another object.
(I dont want it to go out of scope!!!)

I tried to following example without success!

....

#include "theArray.h"
#include <stdio.h>

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

// typedef
typedef theArray<double*> TheArray;

// fill
int num = 20;

// vars
TheArray *array
= new TheArray(num);

for (int i=0; i<num; i++)
{
double *value;
double *ptr;

double *data
= new double[4];

ptr = data;
*array = ptr; <-- SEGMENTATION_FAULT!
}

printf("Exit ...\n");
return 0;
}

....

Could somebodu point in the right direction on how templates are handeled
on the heap and how to pass them on as a pointer another object.

Examples or links are more than welcome!

Thanks!

// Mike
 
J

Jacques Labuschagne

Mike said:
// typedef
typedef theArray<double*> TheArray;

// fill
int num = 20;

// vars
TheArray *array
= new TheArray(num);

for (int i=0; i<num; i++)
{
double *value;
double *ptr;

double *data
= new double[4];

ptr = data;
*array = ptr; <-- SEGMENTATION_FAULT!


This is not a template issue. Here's a question: which binds more
tightly, the dereference (*) or array subscript operator ([])?
The answer is operator[]. You've allocated a single TheArray object, but
you're trying to dereference the i-th instance.

*array = ptr;
means
*(array + i) = ptr;

What you really meant was
(*array) = ptr;
 
M

Marcin Kalicinski

Uzytkownik "Mike said:
Hey!

I've started to use templates for storing arrays of doubles.
The template itself will then be passed on to a linked-list.

My problem is that Im not 100% familiar with how templates
are handeled. What I want to do is to allocate it on the heap
then pass it to my linked-list as a part of another object.
(I dont want it to go out of scope!!!)

I tried to following example without success!

...

#include "theArray.h"
#include <stdio.h>

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

// typedef
typedef theArray<double*> TheArray;

// fill
int num = 20;

// vars
TheArray *array
= new TheArray(num);

for (int i=0; i<num; i++)
{
double *value;
double *ptr;

double *data
= new double[4];

ptr = data;
*array = ptr; <-- SEGMENTATION_FAULT!
}

printf("Exit ...\n");
return 0;
}


First, you haven't attached definition of theArray class template, so most
of what can be said about your problem is a guess. For example, your array
implementation might be flat out wrong. Anyway, from

typedef theArray<double*> TheArray

I deduce that your array stores pointers to doubles. From

*array = ptr;

I see that operator [] returns a pointer to stored value, a pointer to a
pointer to double. If this is true, then the above line of code is undefined
behavior, because you dereference invalid pointer (unless the constructor of
theArray<double *> initializes stored pointers to some valid values, which I
doubt).

Second, your programming style looks to be much Java-like. Why do you create
TheArray object via operator new? In the above code a simple:

TheArray array(num);

would suffice, and save you from an apparent memory leak, because you never
delete 'array' in your program (standard C++ has no automatic garbage
collection). Furthermore, your program has another leak, because you never
delete 'data' allocated inside for loop.

Once again, all above is only a guess. Please attach definition of theArray.

Cheers,
Marcin
 
J

John Harrison

[snip]
This is not a template issue. Here's a question: which binds more
tightly, the dereference (*) or array subscript operator ([])?
The answer is operator[]. You've allocated a single TheArray object, but
you're trying to dereference the i-th instance.

*array = ptr;
means
*(array + i) = ptr;

What you really meant was
(*array) = ptr;


To which one could add, why allocate array at all? Why not just

TheArray array(num);

There's no reason in the posted code not to do this. Of course the OP may
have reasons that aren't in the posted code, but it is a common newbie trait
to needlessly allocate memory dynamically, and to use pointers where they're
not required. I guess some people just like making life difficult for
themselves.

john
 
M

Marcin Kalicinski

*array = ptr;
I see that operator [] returns a pointer to stored value, a pointer to a
pointer to double. If this is true, then the above line of code is undefined
behavior, because you dereference invalid pointer (unless the constructor of
theArray<double *> initializes stored pointers to some valid values, which I
doubt).

Of course Jacques Labuschagne is right, array never invokes operator[] of
theArray, so my resolution of problem was completely wrong. Sorry.

Marcin
 
M

Mike

Marcin Kalicinski said:
*array = ptr;

I see that operator [] returns a pointer to stored value, a pointer to a
pointer to double. If this is true, then the above line of code is undefined
behavior, because you dereference invalid pointer (unless the constructor of
theArray<double *> initializes stored pointers to some valid values, which I
doubt).


Of course Jacques Labuschagne is right, array never invokes operator[] of
theArray, so my resolution of problem was completely wrong. Sorry.

Marcin


thanks for all answers, I finally figured out how to solve the
problem, sorry I did not post the array definition.

about dynamic memory allocation, templates are never deleted? even if
they go out of scope???

thanks

// mike
 
T

tom_usenet

thanks for all answers, I finally figured out how to solve the
problem, sorry I did not post the array definition.

about dynamic memory allocation, templates are never deleted? even if
they go out of scope???

No, just like any dynamically allocated memory. e.g.

{
int* i = new int[10];
//do something with i
}
//memory leaked - should have written delete[] i;

Any memory you allocate with new, you must destroy with delete (and
new[] pairs with delete[]). Generally it is safer to use containers
rather than raw arrays, and smart pointers rather than plain ones.
e.g.

{
std::vector<int> i(10:
//do something with i
}
//no memory leaked - vector's destructor did the clean-up.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top