Connecting Tech Pros Worldwide Forums | Help | Site Map

Templates, syntax and allocation!

Mike
Guest
 
Posts: n/a
#1: Jul 22 '05
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[i] = 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

Jacques Labuschagne
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Templates, syntax and allocation!


Mike wrote:[color=blue]
> // 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[i] = ptr; <-- SEGMENTATION_FAULT![/color]

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[i] = ptr;
means
*(array + i) = ptr;

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

Marcin Kalicinski
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Templates, syntax and allocation!


Uzytkownik "Mike" <mike_4766@hotmail.com> napisal w wiadomosci
news:68fec103.0405170105.737f6639@posting.google.c om...[color=blue]
> 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[i] = ptr; <-- SEGMENTATION_FAULT!
> }
>
> printf("Exit ...\n");
> return 0;
> }
>[/color]

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[i] = 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





John Harrison
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Templates, syntax and allocation!


[snip]
[color=blue]
> 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[i] = ptr;
> means
> *(array + i) = ptr;
>
> What you really meant was
> (*array)[i] = ptr;
>[/color]

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


Marcin Kalicinski
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Templates, syntax and allocation!


> *array[i] = ptr;[color=blue]
>
> 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[/color]
undefined[color=blue]
> behavior, because you dereference invalid pointer (unless the constructor[/color]
of[color=blue]
> theArray<double *> initializes stored pointers to some valid values, which[/color]
I[color=blue]
> doubt).[/color]

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

Marcin


Mike
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Templates, syntax and allocation!


"Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message news:<c8a115$iq9$1@korweta.task.gda.pl>...[color=blue][color=green]
> > *array[i] = 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[/color]
> undefined[color=green]
> > behavior, because you dereference invalid pointer (unless the constructor[/color]
> of[color=green]
> > theArray<double *> initializes stored pointers to some valid values, which[/color]
> I[color=green]
> > doubt).[/color]
>
> Of course Jacques Labuschagne is right, array[i] never invokes operator[] of
> theArray, so my resolution of problem was completely wrong. Sorry.
>
> Marcin[/color]

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
tom_usenet
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Templates, syntax and allocation!


On 1 Jun 2004 09:05:23 -0700, mike_4766@hotmail.com (Mike) wrote:
[color=blue]
>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???[/color]

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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Closed Thread