By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Templates, syntax and allocation!

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Mike wrote:
// 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!


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;

Jul 22 '05 #2

P: n/a
Uzytkownik "Mike" <mi*******@hotmail.com> napisal w wiadomosci
news:68**************************@posting.google.c om...
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;
}


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

Jul 22 '05 #3

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

What you really meant was
(*array)[i] = 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
Jul 22 '05 #4

P: n/a
> *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).


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

Marcin
Jul 22 '05 #5

P: n/a
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message news:<c8**********@korweta.task.gda.pl>...
*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).


Of course Jacques Labuschagne is right, array[i] 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
Jul 22 '05 #6

P: n/a
On 1 Jun 2004 09:05:23 -0700, mi*******@hotmail.com (Mike) wrote:
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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.