memory allocation problem

From: chandra sekhar (ccadmin_at_sisl.co.in)
Date: 02/18/04


Date: Wed, 18 Feb 2004 17:18:13 +0530

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-



Relevant Pages

  • Re: 2D array of structures
    ... programming habits. ... int str_len; ... // Declaring of menu items, necessary for later reuse in array ... // it's time to allocate memory on the fly, ...
    (comp.lang.c)
  • Problem in compiling a C code with MSVC++6.00
    ... When I save the file as a cpp file, it compiles and runs ... int p1; ... void checkfile; ... printf("cannot allocate memory for elements!"); ...
    (comp.lang.c)
  • Re: Basic Pointer Question...
    ... I coded a function to allocate memory and i am passing a pointer to the function. ... In addition, as malloc() is hidden within a function, freecan be easily forgotten. ...
    (comp.lang.c)
  • Re: I am sorry for this...
    ... > int main ... > is it legal or does the temporary get destroyed at the 'f's closing brace? ... introduced to allocate memory for the result of a function for the ... return path the function returns a valid reference, while in another, ...
    (comp.lang.cpp)
  • SetBkMode Transparent takes no effekt
    ... i'm developing a program to print out some richtext with pictures. ... public int FormatRange(bool measureOnly, PrintPageEventArgs e, int charFrom, ... Marshal.StructureToPtr(fr, lParam, false); ...
    (microsoft.public.win32.programmer.gdi)