char* pointer in class member

Working in VC++ v.7(.NET  2003).< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Cannot work with char* pointer in class member.

 

My example:

 

class ABC

{

char *aP;

 

public:

 

 

      ABC (void);

      ~ ABC (void);

 

      int myFunction(void);

 

};

 

Then when in my function

 

int ABC::myFunction(){

      aP=new char [15]; // s Here I always get an error “Access violation”.

}

 

 

Here on the “new” operation, or any other operation I get:

“Unhandled exception at … in myProg.exe :…: Access violation writing location …”


A line 
this->aP=new char [15];
doesn't help as well.


This was not a problem for other C++ compilers, (even with VC++ v.5 and v.6).

What is the problem

 

Maybe some project configuration or definitions. Where

Please help.

 

Thanks in advance.



Answer this question

char* pointer in class member

  • Zach7

    Opps...
    You are right.
    Childish error.

    Sorry guys, I waste your time.
    Thank you very much.


    I use a few complicated class initialization functions in constructor,  
    one of which calls pABC->myFunction()
    ... right from ABC constructor :)))   Funny.




  • northernmonkey

    Show us how you create or use the instance of the class ABC.
    The class itself is not the problem.

    If you just write something like:

    ABC *pABC;
    pABC->myFunction();

    It will cause a crash because you are using not a valid object. You are using a pointer pointing to nothing.
    Look into the debugger and see were this points to!

  • magnetsRookie

    ABC class creates very simply:
    on main window creation:

    #include "ABC.h"

    ABC *pABC=NULL;  // pABC is a gloabl variable, declared after "#include <...>"


    case WM_CREATE:
        pABC = new ABC;
    break;


    Then I use this pointer in some independent function,
    which is inside the file where pABC was declared:

    int Function1(){
       ...
       pABC->myFunction();
       ...
    }

    Class is described in other file "ABC.h" and
    declared via "#include"


  • Mark1974

    You are sure that the global function is called after your WM_CREATE

    Launch the program in the debugger, and let it crash. When the program crashes, what is the contents of the this pointer



  • char* pointer in class member