Problem with the function that takes some_object** as the argument

K

Krzysztof

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<foo;i++){
.....
obj=new some_class(tmp);
cout<<(*obj).ret_seq()<<endl; //LINE A
}
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<i;j++)
{
cout<<(*objs).ret_seq()<<endl; //LINE B
}
....

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++
 
A

Artie Gold

Krzysztof said:
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
 
H

Howard

My first suggestion? Use the string class, not char* pointers. But read
on...
Hi!

I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
...
}

Need a semi-colon here.
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.
}

char* some_class:ret_seq(){

That shoud have two colons, not one! (The above should not have compiled!)
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?
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???
....
obj=new some_class(tmp);


Just guessing, but is the "..." above where tmp gets allocated and filled
out?
cout<<(*obj).ret_seq()<<endl; //LINE A
}
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<i;j++)
{
cout<<(*objs).ret_seq()<<endl; //LINE B
}


Why use (*objs)? Why not use objs-> instead?
...

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
 
V

Victor Bazarov

Krzysztof said:
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 said:
}

char* some_class:ret_seq(){
static char* sequ;
sequ=se;
return sequ;

What's that for? Can't you just do

return se;

?
}
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++){
....
obj=new some_class(tmp);
cout<<(*obj).ret_seq()<<endl; //LINE A
}
return foo;
}

and when I want to get the seq values for all objects in the array:
main()


int main()
{
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j<i;j++)
{
cout<<(*objs).ret_seq()<<endl; //LINE B
}
...

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
 
K

Krzysztof

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top