Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with pointers

Status
Not open for further replies.

johncp

Technical User
Aug 28, 2005
47
0
6
GB
I have a class which declares a pointer to an String (aka Borland AnsiString).
The object ufo is OK, it is used everywhere.
no_dlls is a valid non const positive integer
If I assign passivedllpaths with new I get an exception.

Why can't I assign a pointer with new, to a pointer in a class object ?

class comps
{
public:
. . .
String* passivedllpaths ;
. . .
} ;

comps* ufo = new comps(..,..) ; //constructor takes args

//and at runtime

ufo->passivedllpaths = new String[no_dlls]; //get exception


If I declare passivedllpaths outside the class I don't get an exception

String* passivedllpaths ;
passivedllpaths = new String[no_dlls] ; //Is OK

TIA johnp
 
Hmmm, I tried the code and didn't get any exception...What's the exception? maybe I didn't write your exact code...
 
Hi ctoma2005

Thanx for the reply. It is interesting that you do not
get the same problem. Hmmmmm

The exception message is:-

exception class C0000005 with message
"access violation at 0x0040B85A"
write of address 0x0003F908
proccess stopped, use step or run to continue

The exception persists each time I bug run

The CPU pane shows the above to be true :)
0040B85A mov[edx+0x00003F908],eax
As I recall this is an indexed move from
reg eax to loc 0x00003F908
eax contains 0x0
So nothing helpful in the CPU pane

This is the code
//-----------------------------------
//declared in graph1.h
class comps
{
public:
comps(){} ; //Required by new, ?but is a default constructor created
comps(int , int ) ; //automatic, even if the user creates a non empty constructor
String compval[NUMCOMPS][3] ; //Row, Col
String plotval[NUMPLOTS][5] ;

String* passivedllpaths ;

~comps(){} ;
} ;

//++++++++++++++++++++++++
//instantiated as object ufo in graph1.cpp
//create comps object
#ifndef UFO
#define UFO
comps* ufo = new comps(NUMCOMPS, NUMPLOTS) ; //
#endif //UFO
//----------------------------------------------------
//Comps Contructor to set up the String arrays of compvals and plotvals
comps::comps(int NUMCOMPS, int NUMPLOTS)
{
for(int j=0;j<3;j++)
{ for(int i=0;i<NUMCOMPS;i++) compval[j]= "FF" ; }
for(int j=0;j<5;j++)
{ for(int i=0;i<NUMPLOTS;i++) plotval[j]= "FF" ; }
}
//--------------------------------------------------
//this funct gathers the paths of the dlls found in the dll dir.
int checkfordlls()
{
String dllpath = ".\\dlls\\*.dll" ;
int no_dlls = 0 ;
TSearchRec current_dll ; //TSearchRec is in SysUtils
if(FindFirst(dllpath, 0x04, current_dll)==0)
{
do no_dlls++ ;
while(FindNext(current_dll)==0) ;
}
if (no_dlls == 0)
{
ShowMessage("No DLLs Found") ;
return 0 ;
}
else
{
String files = IntToStr(no_dlls);
ShowMessage(String(files + " Filters Found."));
ufo->passivedllpaths = new String[no_dlls] ; //EXCEPTION OCCURS HERE !!
FindFirst(dllpath, 0x04, current_dll) ; //resets current_dll to 1st
int i = 0 ;
do
{
*(ufo->passivedllpaths + i) = current_dll.Name ;
i++ ;
}
while(FindNext(current_dll)==0) ;
return no_dlls ;
}
}

TIA johnp
 
Correction ! I made a typo re the CPU pane.

0040B85A mov[edx+0x00003F908],eax
eax is 0x00DD2768
edx is 0x0

Sorry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top