Hello !

I have a question to you.
I am unsing a self written List with a DataStructure and I always get an
AccessViloation exception when i would like to add a new Element.

Data Structures:
Code:

struct weinEintrag
{
CString mWeinName;
CString mWeinJahrgang;
CString mWeinPreis;
CString mWeinProduzent;
CString mWeinBewertung;
CString mWeinAlc;
CString mWeinNotiz;

struct weinEintrag *next;
};

struct weinListe
{
struct weinEintrag *start;
};




The following function takes the weinEintrag as Argument and i want to copy
the values of the Argument into a new WeinEintrag structure:

I am using the following code :

Code:

int weinListe_add( struct weinListe *wnLst, struct weinEintrag *wnE)
{
struct weinEintrag *we;

we = (struct weinEintrag *) malloc( sizeof(struct weinEintrag));

if(!we)
return 0;

we->mWeinAlc = wnE->mWeinAlc; //HERE I GET THE ACCESS VIOLATION
!!!!!
we->mWeinBewertung = wnE->mWeinBewertung;
we->mWeinJahrgang = wnE->mWeinJahrgang;
we->mWeinName = wnE->mWeinName;
we->mWeinNotiz = wnE->mWeinNotiz;
we->mWeinPreis = wnE->mWeinPreis;
we->mWeinProduzent = wnE->mWeinProduzent;

we->next = wnLst->start;
wnLst->start = we;

elementCounter++;
return 1;
}





Can you tell me what's wrong in this code ??


regards, martin

Re: Access Violation ! by Michael

Michael
Sat Jan 03 08:06:21 CST 2004

The code looks ok.

Does the wnE argument point to a properly initialized weinEintrag struct?
My guess is that it is either 1) a NULL pointer, 2) some other invalid
pointer value, or 3) a valid pointer but not to a weinEintrag struct, or one
whose mWeinAlc is not properly initialized.

Note that if you use new instead of malloc to allocate these structs, then
the CString will be automatically constructed/initialized by the compiler
(at least for class, not certain about struct, but seems like it should be.
If it doesn't, change struct to class).

--

Michael Salamone [eMVP]
Entrek Software, Inc.
www.entrek.com


"Martin Koch" <kook80@gmx.at> wrote in message
news:ecrXL2f0DHA.2460@TK2MSFTNGP10.phx.gbl...
> Hello !
>
> I have a question to you.
> I am unsing a self written List with a DataStructure and I always get an
> AccessViloation exception when i would like to add a new Element.
>
> Data Structures:
> Code:
>
> struct weinEintrag
> {
> CString mWeinName;
> CString mWeinJahrgang;
> CString mWeinPreis;
> CString mWeinProduzent;
> CString mWeinBewertung;
> CString mWeinAlc;
> CString mWeinNotiz;
>
> struct weinEintrag *next;
> };
>
> struct weinListe
> {
> struct weinEintrag *start;
> };
>
>
>
>
> The following function takes the weinEintrag as Argument and i want to
copy
> the values of the Argument into a new WeinEintrag structure:
>
> I am using the following code :
>
> Code:
>
> int weinListe_add( struct weinListe *wnLst, struct weinEintrag *wnE)
> {
> struct weinEintrag *we;
>
> we = (struct weinEintrag *) malloc( sizeof(struct weinEintrag));
>
> if(!we)
> return 0;
>
> we->mWeinAlc = wnE->mWeinAlc; //HERE I GET THE ACCESS
VIOLATION
> !!!!!
> we->mWeinBewertung = wnE->mWeinBewertung;
> we->mWeinJahrgang = wnE->mWeinJahrgang;
> we->mWeinName = wnE->mWeinName;
> we->mWeinNotiz = wnE->mWeinNotiz;
> we->mWeinPreis = wnE->mWeinPreis;
> we->mWeinProduzent = wnE->mWeinProduzent;
>
> we->next = wnLst->start;
> wnLst->start = we;
>
> elementCounter++;
> return 1;
> }
>
>
>
>
>
> Can you tell me what's wrong in this code ??
>
>
> regards, martin
>
>



Re: Access Violation ! by Martin

Martin
Sat Jan 03 08:35:55 CST 2004

Do I also have to allocate memory for the structure that i get as parameter
in my function ??

regards, martin

"Michael J. Salamone [eMVP]" <mikesa#at#entrek#dot#com> schrieb im
Newsbeitrag news:#i$czLg0DHA.1304@TK2MSFTNGP10.phx.gbl...
> The code looks ok.
>
> Does the wnE argument point to a properly initialized weinEintrag struct?
> My guess is that it is either 1) a NULL pointer, 2) some other invalid
> pointer value, or 3) a valid pointer but not to a weinEintrag struct, or
one
> whose mWeinAlc is not properly initialized.
>
> Note that if you use new instead of malloc to allocate these structs, then
> the CString will be automatically constructed/initialized by the compiler
> (at least for class, not certain about struct, but seems like it should
be.
> If it doesn't, change struct to class).
>
> --
>
> Michael Salamone [eMVP]
> Entrek Software, Inc.
> www.entrek.com
>
>
> "Martin Koch" <kook80@gmx.at> wrote in message
> news:ecrXL2f0DHA.2460@TK2MSFTNGP10.phx.gbl...
> > Hello !
> >
> > I have a question to you.
> > I am unsing a self written List with a DataStructure and I always get an
> > AccessViloation exception when i would like to add a new Element.
> >
> > Data Structures:
> > Code:
> >
> > struct weinEintrag
> > {
> > CString mWeinName;
> > CString mWeinJahrgang;
> > CString mWeinPreis;
> > CString mWeinProduzent;
> > CString mWeinBewertung;
> > CString mWeinAlc;
> > CString mWeinNotiz;
> >
> > struct weinEintrag *next;
> > };
> >
> > struct weinListe
> > {
> > struct weinEintrag *start;
> > };
> >
> >
> >
> >
> > The following function takes the weinEintrag as Argument and i want to
> copy
> > the values of the Argument into a new WeinEintrag structure:
> >
> > I am using the following code :
> >
> > Code:
> >
> > int weinListe_add( struct weinListe *wnLst, struct weinEintrag
*wnE)
> > {
> > struct weinEintrag *we;
> >
> > we = (struct weinEintrag *) malloc( sizeof(struct
weinEintrag));
> >
> > if(!we)
> > return 0;
> >
> > we->mWeinAlc = wnE->mWeinAlc; //HERE I GET THE ACCESS
> VIOLATION
> > !!!!!
> > we->mWeinBewertung = wnE->mWeinBewertung;
> > we->mWeinJahrgang = wnE->mWeinJahrgang;
> > we->mWeinName = wnE->mWeinName;
> > we->mWeinNotiz = wnE->mWeinNotiz;
> > we->mWeinPreis = wnE->mWeinPreis;
> > we->mWeinProduzent = wnE->mWeinProduzent;
> >
> > we->next = wnLst->start;
> > wnLst->start = we;
> >
> > elementCounter++;
> > return 1;
> > }
> >
> >
> >
> >
> >
> > Can you tell me what's wrong in this code ??
> >
> >
> > regards, martin
> >
> >
>
>



Re: Access Violation ! by Michael

Michael
Sat Jan 03 11:14:52 CST 2004

Your description of the function is that it makes a copy of the wnE
parameter and puts the copy in the list. And that's what the code looks
like it does. That indicates that the function must receive a properly
allocated and initialized wnE parameter. It can be allocated on the stack,
in global memory, or in heap memory. But regardless, it is the
responsibility of the caller to make sure it's properly allocated and
initialized.

So no, the function does not need to allocate/initialize the wnE parameter.
The caller does.

--

Michael Salamone [eMVP]
Entrek Software, Inc.
www.entrek.com


"Martin Koch" <kook80@gmx.at> wrote in message
news:e7zVnbg0DHA.2568@TK2MSFTNGP09.phx.gbl...
> Do I also have to allocate memory for the structure that i get as
parameter
> in my function ??
>
> regards, martin
>
> "Michael J. Salamone [eMVP]" <mikesa#at#entrek#dot#com> schrieb im
> Newsbeitrag news:#i$czLg0DHA.1304@TK2MSFTNGP10.phx.gbl...
> > The code looks ok.
> >
> > Does the wnE argument point to a properly initialized weinEintrag
struct?
> > My guess is that it is either 1) a NULL pointer, 2) some other invalid
> > pointer value, or 3) a valid pointer but not to a weinEintrag struct, or
> one
> > whose mWeinAlc is not properly initialized.
> >
> > Note that if you use new instead of malloc to allocate these structs,
then
> > the CString will be automatically constructed/initialized by the
compiler
> > (at least for class, not certain about struct, but seems like it should
> be.
> > If it doesn't, change struct to class).
> >
> > --
> >
> > Michael Salamone [eMVP]
> > Entrek Software, Inc.
> > www.entrek.com
> >
> >
> > "Martin Koch" <kook80@gmx.at> wrote in message
> > news:ecrXL2f0DHA.2460@TK2MSFTNGP10.phx.gbl...
> > > Hello !
> > >
> > > I have a question to you.
> > > I am unsing a self written List with a DataStructure and I always get
an
> > > AccessViloation exception when i would like to add a new Element.
> > >
> > > Data Structures:
> > > Code:
> > >
> > > struct weinEintrag
> > > {
> > > CString mWeinName;
> > > CString mWeinJahrgang;
> > > CString mWeinPreis;
> > > CString mWeinProduzent;
> > > CString mWeinBewertung;
> > > CString mWeinAlc;
> > > CString mWeinNotiz;
> > >
> > > struct weinEintrag *next;
> > > };
> > >
> > > struct weinListe
> > > {
> > > struct weinEintrag *start;
> > > };
> > >
> > >
> > >
> > >
> > > The following function takes the weinEintrag as Argument and i want to
> > copy
> > > the values of the Argument into a new WeinEintrag structure:
> > >
> > > I am using the following code :
> > >
> > > Code:
> > >
> > > int weinListe_add( struct weinListe *wnLst, struct weinEintrag
> *wnE)
> > > {
> > > struct weinEintrag *we;
> > >
> > > we = (struct weinEintrag *) malloc( sizeof(struct
> weinEintrag));
> > >
> > > if(!we)
> > > return 0;
> > >
> > > we->mWeinAlc = wnE->mWeinAlc; //HERE I GET THE ACCESS
> > VIOLATION
> > > !!!!!
> > > we->mWeinBewertung = wnE->mWeinBewertung;
> > > we->mWeinJahrgang = wnE->mWeinJahrgang;
> > > we->mWeinName = wnE->mWeinName;
> > > we->mWeinNotiz = wnE->mWeinNotiz;
> > > we->mWeinPreis = wnE->mWeinPreis;
> > > we->mWeinProduzent = wnE->mWeinProduzent;
> > >
> > > we->next = wnLst->start;
> > > wnLst->start = we;
> > >
> > > elementCounter++;
> > > return 1;
> > > }
> > >
> > >
> > >
> > >
> > >
> > > Can you tell me what's wrong in this code ??
> > >
> > >
> > > regards, martin
> > >
> > >
> >
> >
>
>



Re: Access Violation ! by Chris

Chris
Sat Jan 03 18:45:48 CST 2004

> I am unsing a self written List with a DataStructure and I always get an
> AccessViloation exception when i would like to add a new Element.
> ...
> Data Structures:
> ... [CString objects inside a struct] ...
>
> The following function takes the weinEintrag as Argument and i want to
copy
> the values of the Argument into a new WeinEintrag structure:
> ...
> Code:
>...
> we = (struct weinEintrag *) malloc( sizeof(struct weinEintrag));
>
> if(!we)
> return 0;
>
> we->mWeinAlc = wnE->mWeinAlc; //HERE I GET THE ACCESS
VIOLATION
> !!!!!

You allocated the storage for "we" using malloc, which does not initialize
the memory block. Then you invoked CString::operator=(const CString&) to
copy the string. That will crash because you didn't initialize the "we"
block before trying to use the objects it contains.

The simplest (and normal) thing to do would be to allocate the block like
this:

we = new weinEintrag;

Then the compiler runs the constructor for your weinEintrag object. Even
though you didn't explicitly declare one, your object contains a bunch of
CString objects which have constructors, and therefore the compiler
generates a default constructor for your weinEintrag object. Your use of
malloc skipped running the constructor.