Access violation with AnsiStrings

P

paulrc_25

I'm working with C++ Builder 5.0 Enterprise Edition, and I got this
message:

Project CntSch.exe raised exception class EAccessViolation with message
'Access violation at address 40004B66 in module VCL50.BPL...

____________________

My code is this:

// I define some types:

typedef AnsiString tipoEsquema;

struct tnodo {
tipoEsquema squema;
int cntEsq;
struct tnodo *siguiente;
};

typedef struct tnodo tipoNodo;

-----
// Then I try to use them in a code segment:

tipoEsquema *estosEsq =
(tipoEsquema*)malloc(cuantosEsq*sizeof(tipoEsquema));

lonCad = someConstant;
for (i = 0; i < cuantosEsq; i++)

// *** I got the message error in next line

estosEsq = AnsiString::StringOfChar('0',lonCad);

-----
/*
I commented the part of the code that calls that, and later in code, I try
this function:
*/

tipoNodo* creaNodo(tipoNodo *valor)
{
tipoNodo *nnodo = (tipoNodo *)malloc(sizeof(tipoNodo));

if (nnodo != NULL) {

// **** I got that error back here

nnodo->squema = valor->squema;
nnodo->cntEsq = valor->cntEsq;
nnodo->siguiente = NULL;
}

return nnodo;
}

----

I will appreciate any help.

Thanks

Paul RC
 
J

John Harrison

paulrc_25 said:
I'm working with C++ Builder 5.0 Enterprise Edition, and I got this
message:

Project CntSch.exe raised exception class EAccessViolation with message
'Access violation at address 40004B66 in module VCL50.BPL...

____________________

My code is this:

// I define some types:

typedef AnsiString tipoEsquema;

struct tnodo {
tipoEsquema squema;
int cntEsq;
struct tnodo *siguiente;
};

typedef struct tnodo tipoNodo;

-----
// Then I try to use them in a code segment:

tipoEsquema *estosEsq =
(tipoEsquema*)malloc(cuantosEsq*sizeof(tipoEsquema));

This is wrong, try this

tipoEsquema *estosEsq = new tipoEsquema[cuantosEsq];

Don't use malloc, free or realloc in a C++ program. There don't work on
classes like AnsiString. Use new and delete instead. Or use a vector.

john
 
L

Larry Brasfield

paulrc_25 said:
I'm working with C++ Builder 5.0 Enterprise Edition, and I got this
message:

Project CntSch.exe raised exception class EAccessViolation with message
'Access violation at address 40004B66 in module VCL50.BPL...

____________________

My code is this:

// I define some types:

typedef AnsiString tipoEsquema;

struct tnodo {
tipoEsquema squema;
int cntEsq;
struct tnodo *siguiente;
};

typedef struct tnodo tipoNodo;

-----
// Then I try to use them in a code segment:

tipoEsquema *estosEsq =
(tipoEsquema*)malloc(cuantosEsq*sizeof(tipoEsquema));

lonCad = someConstant;
for (i = 0; i < cuantosEsq; i++)

// *** I got the message error in next line

estosEsq = AnsiString::StringOfChar('0',lonCad);


You have made the mistake of using operator= upon an
object that was never properly constructed. Your use
of malloc is the most direct problem here. The problem
goes deeper, however. You need to learn fundamental
C++ before using C++ classes, such as AnsiString.

Here is the order of steps I would recommend:
1. Learn what C++ constructors and destructors are for.
2. Learn what operator= is for a class and how it differs
from the copy constructor.
3. Begin to use already-defined (in a library) C++ classes.

To cure the immediate problem, you could get closer
by using this:
tipoEsquema *estosEsq = new topoEsquema[cuantosEsq];
instead of your malloc call.
-----
/*
I commented the part of the code that calls that, and later in code, I try
this function:
*/

tipoNodo* creaNodo(tipoNodo *valor)
{
tipoNodo *nnodo = (tipoNodo *)malloc(sizeof(tipoNodo));

if (nnodo != NULL) {

// **** I got that error back here

nnodo->squema = valor->squema;
nnodo->cntEsq = valor->cntEsq;
nnodo->siguiente = NULL;
}

return nnodo;
}

----

I will appreciate any help.

Thanks

Good luck. You will need plenty of it if you plow into
C++ usage without learning the fundamentals first.
 
R

Ron Natalie

Larry said:
You have made the mistake of using operator= upon an
object that was never properly constructed. Your use
of malloc is the most direct problem here. The problem
goes deeper, however. You need to learn fundamental
C++ before using C++ classes, such as AnsiString.
Actually, the reverse is true. He should learn fundamental
C++ classes before dealing with advanced concepts like
malloc().

There needs to be some C unlearning before doing anything
in C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top