473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

C++: Pointer to a string in a class

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
Jul 22 '05 #1
5 1514
* 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.

--
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?
Jul 22 '05 #2

"Alf P. Steinbach" <al***@start.no> schrieb im Newsbeitrag
news:41****************@news.individual.net...
*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
Jul 22 '05 #3

"Tomas" <th*****@uni-bremen.de> wrote in message
news:cj**********@f40-3.zfn.uni-bremen.de...
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
Jul 22 '05 #4
> > When I try something like

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


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)?
Jul 22 '05 #5
* Method Man:
When I try something like

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


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)?


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

--
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?
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
6
by: Itay_k | last post by:
Hello, I want a member in my class that will save pointer to pointer to System::Drawing::Image class. When I write on my class code: System::Drawing::Image **bmp; I get this error message:...
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
6
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
15
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.