Templates, syntax and allocation!

Discussion in 'C++' started by Mike, May 17, 2004.

  1. Mike

    Mike Guest

    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
    Mike, May 17, 2004
    #1
    1. Advertising

  2. 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 = 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;
    Jacques Labuschagne, May 17, 2004
    #2
    1. Advertising

  3. Uzytkownik "Mike" <> napisal w wiadomosci
    news:...
    > 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
    Marcin Kalicinski, May 17, 2004
    #3
  4. [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
    John Harrison, May 17, 2004
    #4
  5. > *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
    Marcin Kalicinski, May 17, 2004
    #5
  6. Mike

    Mike Guest

    "Marcin Kalicinski" <> wrote in message news:<c8a115$iq9$>...
    > > *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
    Mike, Jun 1, 2004
    #6
  7. Mike

    tom_usenet Guest

    On 1 Jun 2004 09:05:23 -0700, (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
    tom_usenet, Jun 1, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    454
  2. Ken
    Replies:
    24
    Views:
    3,842
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
  4. recover
    Replies:
    2
    Views:
    789
    recover
    Jul 25, 2006
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,170
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page