memory allocation problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chandra sekhar

    memory allocation problem

    Hi All,
    I am trying to allocate memory for member variable in a static
    method belongs to same class.. the allocation is o.k and when try to
    access the member variable I find that it is a null pointer.

    Here is the code ...

    #include <iostream.h>


    class A
    {
    public:
    A():i(0){}
    ~A(){ if(i) { delete i; i = 0;}}
    static void create(A** obj)
    {
    *obj = new A();
    int* ii = 0;
    (*obj)->get_i(ii);
    ii = new int;
    }
    void get_i(int*& g_out)
    {
    g_out = i;
    }
    private:
    int* i;
    };

    int main()
    {
    A* ob = 0;
    A::create(&ob);
    int* i = 0;
    ob->get_i(i);
    try
    {
    cout << " value " << *i << endl;
    }catch(...)
    {
    // thows the null pointer exception
    }
    if( ob ) { delete ob; ob = 0;}
    return 0;
    }

    Any suggestions in this regard is welcome.

    Thanks
    Chandra-

  • John Harrison

    #2
    Re: memory allocation problem


    "chandra sekhar" <ccadmin@sisl.c o.in> wrote in message
    news:403350FD.D A178334@sisl.co .in...[color=blue]
    > Hi All,
    > I am trying to allocate memory for member variable in a static
    > method belongs to same class.. the allocation is o.k and when try to
    > access the member variable I find that it is a null pointer.
    >
    > Here is the code ...
    >
    > #include <iostream.h>
    >
    >
    > class A
    > {
    > public:
    > A():i(0){}
    > ~A(){ if(i) { delete i; i = 0;}}
    > static void create(A** obj)
    > {
    > *obj = new A();
    > int* ii = 0;
    > (*obj)->get_i(ii);
    > ii = new int;
    > }
    > void get_i(int*& g_out)
    > {
    > g_out = i;
    > }
    > private:
    > int* i;
    > };
    >
    > int main()
    > {
    > A* ob = 0;
    > A::create(&ob);
    > int* i = 0;
    > ob->get_i(i);
    > try
    > {
    > cout << " value " << *i << endl;
    > }catch(...)
    > {
    > // thows the null pointer exception
    > }
    > if( ob ) { delete ob; ob = 0;}
    > return 0;
    > }
    >
    > Any suggestions in this regard is welcome.
    >
    > Thanks
    > Chandra-
    >[/color]

    Nowhere in the code above do you assign to the member variable i, so it
    stays NULL. Where did you think you were changing member variable i?

    john


    Comment

    • chandra sekhar

      #3
      Re: memory allocation problem

      To change the contents of the variable memory for the variable should
      exists.. I am checking the memory existance by calling get_i in the main()
      Here the problem is the memory for the variable is not existing inspite of
      allocation in the static method.

      Thanks
      Chandra

      John Harrison wrote:
      [color=blue]
      > "chandra sekhar" <ccadmin@sisl.c o.in> wrote in message
      > news:403350FD.D A178334@sisl.co .in...[color=green]
      > > Hi All,
      > > I am trying to allocate memory for member variable in a static
      > > method belongs to same class.. the allocation is o.k and when try to
      > > access the member variable I find that it is a null pointer.
      > >
      > > Here is the code ...
      > >
      > > #include <iostream.h>
      > >
      > >
      > > class A
      > > {
      > > public:
      > > A():i(0){}
      > > ~A(){ if(i) { delete i; i = 0;}}
      > > static void create(A** obj)
      > > {
      > > *obj = new A();
      > > int* ii = 0;
      > > (*obj)->get_i(ii);
      > > ii = new int;
      > > }
      > > void get_i(int*& g_out)
      > > {
      > > g_out = i;
      > > }
      > > private:
      > > int* i;
      > > };
      > >
      > > int main()
      > > {
      > > A* ob = 0;
      > > A::create(&ob);
      > > int* i = 0;
      > > ob->get_i(i);
      > > try
      > > {
      > > cout << " value " << *i << endl;
      > > }catch(...)
      > > {
      > > // thows the null pointer exception
      > > }
      > > if( ob ) { delete ob; ob = 0;}
      > > return 0;
      > > }
      > >
      > > Any suggestions in this regard is welcome.
      > >
      > > Thanks
      > > Chandra-
      > >[/color]
      >
      > Nowhere in the code above do you assign to the member variable i, so it
      > stays NULL. Where did you think you were changing member variable i?
      >
      > john[/color]

      Comment

      • Patrik Stellmann

        #4
        Re: memory allocation problem

        >>> static void create(A** obj)[color=blue][color=green][color=darkred]
        >>> {
        >>> *obj = new A();
        >>> int* ii = 0;
        >>> (*obj)->get_i(ii);
        >>> ii = new int;
        >>> }
        >>> void get_i(int*& g_out)
        >>> {
        >>> g_out = i;
        >>> }[/color][/color][/color]
        you functions writes the value of i to g_out but what you need is i
        itself (or a pointer to it). So something (extremly 'ugly') like
        int** get_i()
        {
        return &i;
        }
        static void create(A** obj)
        {
        *obj = new A();
        int** ii = (*obj)->get_i(ii);
        *ii = new int;
        }

        would probably do. This is - of course - very bad design since you pass
        out a pointer to a member variable and there are most likely better ways
        to achieve what you're looking for. Maybe a set_i function but the
        decision also depends on what else you're doing with this class...

        Comment

        • Rolf Magnus

          #5
          Re: memory allocation problem

          chandra sekhar wrote:
          [color=blue]
          > Hi All,
          > I am trying to allocate memory for member variable in a static
          > method belongs to same class.. the allocation is o.k and when try to
          > access the member variable I find that it is a null pointer.
          >
          > Here is the code ...
          >
          > #include <iostream.h>
          >
          >
          > class A
          > {
          > public:
          > A():i(0){}
          > ~A(){ if(i) { delete i; i = 0;}}
          > static void create(A** obj)
          > {
          > *obj = new A();
          > int* ii = 0;
          > (*obj)->get_i(ii);
          > ii = new int;
          > }
          > void get_i(int*& g_out)
          > {
          > g_out = i;
          > }
          > private:
          > int* i;
          > };
          >
          > int main()
          > {
          > A* ob = 0;
          > A::create(&ob);
          > int* i = 0;
          > ob->get_i(i);
          > try
          > {
          > cout << " value " << *i << endl;
          > }catch(...)
          > {
          > // thows the null pointer exception
          > }
          > if( ob ) { delete ob; ob = 0;}
          > return 0;
          > }
          >
          > Any suggestions in this regard is welcome.[/color]

          Your above code is extremely obfuscated. However, you never set the i
          member of your object and neither the local i variable in main() to a
          valid memory address, so it still is a null pointer in the cout line.

          Comment

          • John Harrison

            #6
            Re: memory allocation problem


            "chandra sekhar" <ccadmin@sisl.c o.in> wrote in message
            news:403353BF.5 9176919@sisl.co .in...[color=blue]
            > To change the contents of the variable memory for the variable should
            > exists.. I am checking the memory existance by calling get_i in the main()
            > Here the problem is the memory for the variable is not existing inspite of
            > allocation in the static method.
            >
            > Thanks
            > Chandra[/color]

            Nothing in the static method allocates memory and assigns it to i.

            I cannot understand why you think it should.

            You need to do something like this

            static void create(A** obj)
            {
            *obj = new A();
            (*obj)-> i = new int; // allocate some memory and assign it to i
            }

            john


            Comment

            Working...