C++: Pointer to a string in a class

T

Tomas

Hi NG.

I have a problem with strings.
I have defined this class:

class wort_cl
{
int haeufigkeit;
int length;
string wort;

public:
wort_cl();
wort_cl(string wort_neu, int haeufigkeit_neu);
void init(string wort_neu, int haeufigkeit_neu);
int getHaeufig();
int getLength();
string getWort();
void flush();
};

Now I want to implement a list with these elements:

struct list_element_t
{
wort_cl *daswort;
struct list_element_t *next;
};

The list elements are dynamically created using malloc();

void insertList(list_handle_t *h, void *d,int length) {
struct list_element_t *l;
l = (struct list_element_t *)malloc(sizeof(struct list_element_t));
...

*d is a pointer to a wort_cl object. Now I can't figure out how to
initialise the element l->daswort so that it contains the same value
as d.
When I try something like

*(l->daswort)=*((wort_cl*)d);

I get an access violantion.
maybe someone understands my problem and can help me ;)

thomas
 
A

Alf P. Steinbach

* Tomas:
I have a problem with strings.
I have defined this class:

class wort_cl
{
int haeufigkeit;
int length;
string wort;

public:
wort_cl();
wort_cl(string wort_neu, int haeufigkeit_neu);
void init(string wort_neu, int haeufigkeit_neu);

Design: avoid reinitialization of an object.

int getHaeufig();
int getLength();
string getWort();
void flush();
};

Now I want to implement a list with these elements:

struct list_element_t
{
wort_cl *daswort;
struct list_element_t *next;
};

Style: using 'struct' as a type prefix is a C'ism, not necessary
and generally not a good idea in C++.


The list elements are dynamically created using malloc();

Principle: 'malloc' is a C'ism and not just stylewise: use type-safe
C++ operator 'new'.

void insertList(list_handle_t *h, void *d,int length) {
struct list_element_t *l;

Style: using 'struct' as a type prefix is a C'ism, not necessary
and generally not a good idea in C++.

l = (struct list_element_t *)malloc(sizeof(struct list_element_t));

Style: using 'struct' as a type prefix is a C'ism, not necessary
and generally not a good idea in C++.

Style: don't use the lowercase letter 'l' as a name.

Principle: 'malloc' is a C'ism and not just stylewise: use type-safe
C++ operator 'new'.

...

*d is a pointer to a wort_cl object

Why have you declared it as 'void*' then? Don't do that. Smack,
smack, smack!

Now I can't figure out how to
initialise the element l->daswort so that it contains the same value
as d.

That's because you have declared 'd' as 'void*'.


When I try something like

*(l->daswort)=*((wort_cl*)d);

Don't use C style casts.

Don't use casts.

I get an access violantion.

The 'list_element_t' that 'l' points to is not initialized so 'daswort'
inside it is not initialized so dereferencing 'daswort' gives an access
violation because you were lucky -- it's formally undefined ubehavior.
 
T

Tomas

*snip*
Style: using 'struct' as a type prefix is a C'ism, not necessary
and generally not a good idea in C++. *snip*
Principle: 'malloc' is a C'ism and not just stylewise: use type-safe
C++ operator 'new'. *snip*
Style: don't use the lowercase letter 'l' as a name. *snip*
The 'list_element_t' that 'l' points to is not initialized so 'daswort'
inside it is not initialized so dereferencing 'daswort' gives an access
violation because you were lucky -- it's formally undefined ubehavior.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Thank you for your fast answer :)
Ok, I'll work on my style and try to wipe out the C'isms. I just left them
in because i imported the list-handling code from another project.

Thomas
 
J

John Harrison

Tomas said:
Hi NG.

I have a problem with strings.
I have defined this class:

class wort_cl
{
int haeufigkeit;
int length;
string wort;

public:
wort_cl();
wort_cl(string wort_neu, int haeufigkeit_neu);
void init(string wort_neu, int haeufigkeit_neu);
int getHaeufig();
int getLength();
string getWort();
void flush();
};

Now I want to implement a list with these elements:

struct list_element_t
{
wort_cl *daswort;
struct list_element_t *next;
};

The list elements are dynamically created using malloc();

void insertList(list_handle_t *h, void *d,int length) {
struct list_element_t *l;
l = (struct list_element_t *)malloc(sizeof(struct list_element_t));
...

*d is a pointer to a wort_cl object. Now I can't figure out how to
initialise the element l->daswort so that it contains the same value
as d.
When I try something like

*(l->daswort)=*((wort_cl*)d);

I get an access violantion.
maybe someone understands my problem and can help me ;)

I depends what you mean by the same value. Do you mean the same pointer?
That would just be

l->daswort = (wort_cl*)d;

But if you mean the same value that d is pointing at, then your previous
code is correct EXCEPT that you forgot to allocate some memory to copy the
value to.

l->daswort = malloc(sizeof(wort_cl));
*l->daswort=*(wort_cl*)d;

But this might not be what you really want to do, you should consider
improving the style and design of your code. For instance is there anything
wrong with this

struct list_element_t
{
wort_cl daswort;
struct list_element_t *next;
};

Does daswort have to be a pointer? I suspect not, and making it not a
pointer would remove the allocation step above, and you could just write.

l->daswort=*(wort_cl*)d;

which is simpler and more efficient.

john
 
M

Method Man

When I try something like
Don't use C style casts.

Don't use casts.

So how could he go about doing it _without_ casting 'd' (if 'd' is void* or
void)?
 
A

Alf P. Steinbach

* Method Man:
So how could he go about doing it _without_ casting 'd' (if 'd' is void* or
void)?

Not using casting means, among other things, not using 'void*'.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top