 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Krzysztof Guest
|
Posted: Tue Oct 28, 2003 5:11 pm Post subject: Problem with the function that takes some_object** as the ar |
|
|
Hi!
I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
....
}
some_class::some_class(char* se){
seq=new char[strlen(se)];
seq=se;
}
char* some_class:ret_seq(){
static char* sequ;
sequ=se;
return sequ;
}
and than
I have the function witch should modify the array of the objectc of
the some_class:
int func(some_class** obj){
....
char* tmp;
for(int i=0;i
.....
obj[i]=new some_class(tmp);
cout<<(*obj[i]).ret_seq()<
}
return foo;
}
and when I want to get the seq values for all objects in the array:
main()
{
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j
{
cout<<(*objs[i]).ret_seq()<
}
....
and the problem is that the results that I obtain from LINE A and LINE
B are not the same.
If you have any idea why please help. I would be very thankful
Greetings
PS: I have just started to programm in C++
|
|
| Back to top |
|
 |
Artie Gold Guest
|
Posted: Tue Oct 28, 2003 5:24 pm Post subject: Re: Problem with the function that takes some_object** as th |
|
|
Krzysztof wrote:
| Quote: | Hi!
I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
...
}
some_class::some_class(char* se){
seq=new char[strlen(se)];
|
In the above line, you've allocated almost enough memory for the
C-style string (hopefully) pointed to by `se' (you haven't allocated
enough to hold the trailing null char).
Now you immediately set `seq' to something else -- in this case
whatever the passed argument `se' happened to be. Also, since you've
lost the original pointer to the allocated buffer, a memory leak has
been introduced.
So whaddya expect? ;-)
[snip]
Fix the above -- and next time please post well formatted *minimal*
*compilable* code that exhibits whatever problem you've encountered.
That will greatly enhance your chances of getting whatever help you
need.
Cheers and HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Tue Oct 28, 2003 5:29 pm Post subject: Re: Problem with the function that takes some_object** as th |
|
|
"Krzysztof" <kwicher (AT) yahoo (DOT) com> wrote
My first suggestion? Use the string class, not char* pointers. But read
on...
| Quote: | Hi!
I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
...
}
|
Need a semi-colon here.
| Quote: | some_class::some_class(char* se){
seq=new char[strlen(se)];
|
This allocates an array as long as the string you want to hold. But I
assume you'll be using null-terminated strings here, so you need to add one
to that:
seq = new char[strlen(se)+1];
This does not copy the data in the array. It merely overwrites the seq
pointer with the value in se, which makes seq point to the same memory as
se.
| Quote: | }
char* some_class:ret_seq(){
|
That shoud have two colons, not one! (The above should not have compiled!)
| Quote: | static char* sequ;
sequ=se;
|
Again, this just copies the pointer contents, not the array contents.
Except for one other serious problem: there IS NO se variable in this
function (or class)! Did you mean "seq"? If so, why are you returning a
copy of the pointer, and why use a static variable to hold that copy?
| Quote: | return sequ;
}
and than
I have the function witch should modify the array of the objectc of
the some_class:
int func(some_class** obj){
...
char* tmp;
for(int i=0;i<foo;i++){
|
What's foo???
| Quote: | ....
obj[i]=new some_class(tmp);
|
Just guessing, but is the "..." above where tmp gets allocated and filled
out?
| Quote: | cout<<(*obj[i]).ret_seq()<
}
return foo;
}
and when I want to get the seq values for all objects in the array:
main()
{
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j
{
cout<<(*objs[i]).ret_seq()<
}
|
Why use (*objs[i])? Why not use objs[i]-> instead?
| Quote: | ...
and the problem is that the results that I obtain from LINE A and LINE
B are not the same.
If you have any idea why please help. I would be very thankful
Greetings
PS: I have just started to programm in C++
|
This code won't compile as is. Can you post what you really have, or at
least a compilable sub-set of your original code?
My suggestion, again, would be to use the string class, and avoid all the
pointers. But it looks like your main problem is trying to use the
assignment operator (=) to copy array data, which does not work. You need
to use strcpy, or memcpy, or manually copy the data, not assign the pointers
as you've done.
-Howard
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Oct 28, 2003 5:40 pm Post subject: Re: Problem with the function that takes some_object** as th |
|
|
"Krzysztof" <kwicher (AT) yahoo (DOT) com> wrote...
| Quote: | I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
...
}
some_class::some_class(char* se){
seq=new char[strlen(se)];
seq=se;
|
Now, this is a HUGE error. You allocate 'strlen(se)' chars,
obtain the address of that newly allocated memory, and then
BAM! immediately override the value with the parameter. This
is known as a "memory leak".
The error is probably due to your missing the fact that arrays
cannot be copied using the assignment operator, and due to your
confusing arrays and pointers. No biggie, it's one of the most
difficult areas for beginners.
You made two mistakes (logical, not syntax) in this function.
First, if what you pass is a C-string, you need to (a) allocate
one more character than the length to store the null terminator,
and (b) use 'strcpy' to copy the array:
some_class::some_class(char* se) {
seq = new char[strlen(se) + 1];
strcpy(seq, se);
}
Now, that said, you'd be MUCH BETTER OFF if you used 'string'
class from <string>.
| Quote: | }
char* some_class:ret_seq(){
static char* sequ;
sequ=se;
return sequ;
|
What's that for? Can't you just do
return se;
?
| Quote: | }
and than
I have the function witch should modify the array of the objectc of
the some_class:
int func(some_class** obj){
...
char* tmp;
for(int i=0;i
....
obj[i]=new some_class(tmp);
cout<<(*obj[i]).ret_seq()<
}
return foo;
}
and when I want to get the seq values for all objects in the array:
main()
|
int main()
| Quote: | {
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j
{
cout<<(*objs[i]).ret_seq()<
}
...
and the problem is that the results that I obtain from LINE A and LINE
B are not the same.
If you have any idea why please help. I would be very thankful
Greetings
PS: I have just started to programm in C++
|
Visit alt.comp.lang.learn.c-c++ newsgroup, it might be more
beneficial for you as a beginner.
Victor
|
|
| Back to top |
|
 |
Krzysztof Guest
|
Posted: Thu Nov 06, 2003 8:39 pm Post subject: Re: Problem with the function that takes some_object** as th |
|
|
Thankks a lot.
Now I have got what is up and now it works perfect... but will wisit
the newgroup that has been suggested
Krzysztof
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|