Accessing an element of an array of list of user-defined structure,gives Access Violation

Discussion in 'C++' started by PSN, Jun 17, 2011.

  1. PSN

    PSN Guest

    Hallo Everyone,
    I have been struggling with issue since 2 days. I hope soemone can
    point me here in the right direction. I have the following situation:

    struct stTemp
    {
    int a;
    int b;
    stTemp() {...}
    stTemp(int _a, int _b) {...}
    };

    typedef std::list<stTemp> lstTemp;

    class myClass
    {
    private:
    int format;
    int var;
    lstTemp listarrays[10];

    public:
    myClass(int _a, int _b) : format(_a), var(_b) {}
    PSN, Jun 17, 2011
    #1
    1. Advertising

  2. PSN

    PSN Guest

    Re: Accessing an element of an array of list of user-definedstructure, gives Access Violation

    More Info: I also tried re-modelling the myClass as in here, but with
    no success:

    class myClass
    {
    private:
    int format;
    int var;
    lstTemp* listarrays[10];
    public:
    myClass(int _a, int _b) : format(_a), var(_b)
    {
    for (int i=0; i<10; i++)
    {
    lstTemp *plstTemp = new lstTemp;
    listarrays = plstTemp;
    }
    }
    PSN, Jun 17, 2011
    #2
    1. Advertising

  3. PSN

    Paul Guest

    Re: Accessing an element of an array of list of user-defined structure, gives Access Violation

    "PSN" <> wrote in message
    news:...
    > Hallo Everyone,
    > I have been struggling with issue since 2 days. I hope soemone can
    > point me here in the right direction. I have the following situation:
    >
    > struct stTemp
    > {
    > int a;
    > int b;
    > stTemp() {...}
    > stTemp(int _a, int _b) {...}
    > };
    >
    > typedef std::list<stTemp> lstTemp;
    >
    > class myClass
    > {
    > private:
    > int format;
    > int var;
    > lstTemp listarrays[10];
    >
    > public:
    > myClass(int _a, int _b) : format(_a), var(_b) {}
    > .
    > .
    > std::list<stTemp> GetListElement(int index) { return
    > listarrays[index]; }
    > .
    > .
    > };
    >
    >
    > CreateListArray()
    > {
    > myClass *pObj = new myClass();


    There is no default constructor in myClass that takes no arguments
    <snip>
    Paul, Jun 17, 2011
    #3
  4. PSN

    Balog Pal Guest

    Re: Accessing an element of an array of list of user-defined structure, gives Access Violation

    how about fetching the FAQ and reading the "how to post code" section?
    Balog Pal, Jun 17, 2011
    #4
  5. PSN

    PSN Guest

    Re: Accessing an element of an array of list of user-definedstructure, gives Access Violation

    On Jun 17, 4:45 pm, "Balog Pal" <> wrote:
    > how about fetching the FAQ and reading the "how to post code" section?

    I'm sorry. I looked every where for the FAQ. I couldnt find it. A link
    would be helpful.

    @Paul: It was a typo there. The line should read as below. Please note
    that it is a proxy code of my implementation.
    myClass *pObj = new myClass(1,2);

    Thanks,
    Prakash
    PSN, Jun 17, 2011
    #5
  6. Re: Accessing an element of an array of list of user-definedstructure, gives Access Violation

    On Jun 17, 6:47 am, PSN <> wrote:
    > Hallo Everyone,
    > I have been struggling with issue since 2 days. I hope soemone can
    > point me here in the right direction. I have the following situation:

    [...]

    > typedef std::list<stTemp> lstTemp;
    >
    > class myClass
    > {
    >    private:
    >        int format;
    >        int var;
    >        lstTemp listarrays[10];
    >
    >    public:
    >        myClass(int _a, int _b) : format(_a), var(_b) {}
    >        .
    >        .
    >        std::list<stTemp> GetListElement(int index) { return
    > listarrays[index]; }


    Why not reuse the lstTemp typedef for the return type of this
    function?

    I suspect that you may want this function to return by reference.
    (and probably to add a const version that returns by const-reference)

    Not enough info to tell if that will fix your problem. But unless you
    have other, not-shown functions in myClass that modify the elements of
    listarrays, it will not ever contain anything aside from ten empty
    lists.

    - Kevin
    Kevin McCarty, Jun 17, 2011
    #6
  7. Re: Accessing an element of an array of list of user-definedstructure, gives Access Violation

    On Jun 17, 6:53 am, PSN <> wrote:
    > More Info: I also tried re-modelling the myClass as in here, but with
    > no success:


    This does almost exactly the same thing as your original
    implementation (except that the elements of listarray are allocated on
    the heap rather than the stack, and now you also need to define an
    explicit copy ctor, assignment op, and dtor which are hopefully in the
    code you do not show) so no surprise there.

    > class myClass
    > {
    >    private:
    >        int format;
    >        int var;
    >        lstTemp* listarrays[10];


    Don't do this without a really good reason, it is not exception-safe
    and it is non-trivial to do correctly

    >    public:
    >        myClass(int _a, int _b) : format(_a), var(_b)
    >        {
    >           for (int i=0; i<10; i++)
    >           {
    >              lstTemp *plstTemp = new lstTemp;
    >              listarrays = plstTemp;
    >           }
    >        }


    Look up the Rule of Three:
    http://en.wikipedia.org/wiki/Rule_of_three_(C _programming)

    >        .
    >        .
    >        std::list<stTemp> GetListElement(int index) { return
    > *(listarrays[index]); }


    This is still returning by value (i.e. by copy) rather than by
    reference, which I still think may be (one of) the troubles.

    - Kevin
    Kevin McCarty, Jun 17, 2011
    #7
  8. PSN

    Paul Guest

    Re: Accessing an element of an array of list of user-defined structure, gives Access Violation

    "PSN" <> wrote in message
    news:...
    On Jun 17, 4:45 pm, "Balog Pal" <> wrote:
    > how about fetching the FAQ and reading the "how to post code" section?

    --I'm sorry. I looked every where for the FAQ. I couldnt find it. A link
    --would be helpful.

    --@Paul: It was a typo there. The line should read as below. Please note
    --that it is a proxy code of my implementation.
    --myClass *pObj = new myClass(1,2);


    Each array element contains an empty list. Then you copy these empty lists
    to a locallist object, which is a pointless excercise.
    The code you have posted does not seem to replicate the error you have
    described as present in your original code.
    Paul, Jun 17, 2011
    #8
  9. PSN

    Paul N Guest

    Re: Accessing an element of an array of list of user-definedstructure, gives Access Violation

    On Jun 17, 3:56 pm, PSN <> wrote:
    > I'm sorry. I looked every where for the FAQ. I couldnt find it. A link
    > would be helpful.


    You can find the FAQ at http://www.parashift.com/c -faq-lite/ or if
    you forget that it is the first hit if you type "c++ faq" into Google.
    Hope you find it useful.
    Paul N, Jun 18, 2011
    #9
    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. Kyle Teague
    Replies:
    3
    Views:
    660
    David White
    Nov 26, 2003
  2. Daniel

    Access Violation in linked list

    Daniel, Oct 18, 2004, in forum: C Programming
    Replies:
    7
    Views:
    411
    Herbert Rosenau
    Oct 20, 2004
  3. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  4. dbuser
    Replies:
    4
    Views:
    435
    Thomas J. Gritzan
    Oct 12, 2005
  5. Arne Mertz
    Replies:
    12
    Views:
    721
    Arne Mertz
    Apr 13, 2009
Loading...

Share This Page