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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krzysztof

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

    Hi!

    I have a class:
    some_class{
    private:
    char* seq;
    public:
    some_class(char * se);
    char* ret_seq();
    ....
    }
    some_class::som e_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[i]=new some_class(tmp) ;
    cout<<(*obj[i]).ret_seq()<<en dl; //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[i]).ret_seq()<<en dl; //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++
  • Howard

    #2
    Re: Problem with the function that takes some_object** as the argument


    "Krzysztof" <kwicher@yahoo. com> wrote in message
    news:c5e551a2.0 310280911.3abf1 ba6@posting.goo gle.com...

    My first suggestion? Use the string class, not char* pointers. But read
    on...
    [color=blue]
    > Hi!
    >
    > I have a class:
    > some_class{
    > private:
    > char* seq;
    > public:
    > some_class(char * se);
    > char* ret_seq();
    > ...
    > }[/color]

    Need a semi-colon here.
    [color=blue]
    > some_class::som e_class(char* se){
    > seq=new char[strlen(se)];[/color]

    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];
    [color=blue]
    > seq=se;[/color]

    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.
    [color=blue]
    > }
    >
    > char* some_class:ret_ seq(){[/color]

    That shoud have two colons, not one! (The above should not have compiled!)
    [color=blue]
    > static char* sequ;
    > sequ=se;[/color]

    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?
    [color=blue]
    > 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++){[/color]

    What's foo???
    [color=blue]
    > ....
    > obj[i]=new some_class(tmp) ;[/color]

    Just guessing, but is the "..." above where tmp gets allocated and filled
    out?
    [color=blue]
    > cout<<(*obj[i]).ret_seq()<<en dl; //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[i]).ret_seq()<<en dl; //LINE B
    > }[/color]

    Why use (*objs[i])? Why not use objs[i]-> instead?
    [color=blue]
    > ...
    >
    > 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++[/color]

    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





    Comment

    • Artie Gold

      #3
      Re: Problem with the function that takes some_object** as the argument

      Krzysztof wrote:[color=blue]
      > Hi!
      >
      > I have a class:
      > some_class{
      > private:
      > char* seq;
      > public:
      > some_class(char * se);
      > char* ret_seq();
      > ...
      > }
      > some_class::som e_class(char* se){
      > seq=new char[strlen(se)];[/color]

      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).
      [color=blue]
      > seq=se;[/color]

      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.

      Comment

      • Victor Bazarov

        #4
        Re: Problem with the function that takes some_object** as the argument

        "Krzysztof" <kwicher@yahoo. com> wrote...[color=blue]
        > I have a class:
        > some_class{
        > private:
        > char* seq;
        > public:
        > some_class(char * se);
        > char* ret_seq();
        > ...
        > }
        > some_class::som e_class(char* se){
        > seq=new char[strlen(se)];
        > seq=se;[/color]

        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::som e_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>.
        [color=blue]
        > }
        >
        > char* some_class:ret_ seq(){
        > static char* sequ;
        > sequ=se;
        > return sequ;[/color]

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

        return se;

        ?
        [color=blue]
        > }
        > 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[i]=new some_class(tmp) ;
        > cout<<(*obj[i]).ret_seq()<<en dl; //LINE A
        > }
        > return foo;
        > }
        >
        > and when I want to get the seq values for all objects in the array:
        > main()[/color]

        int main()
        [color=blue]
        > {
        > some_class** objs=new some_class*[100];
        > int i;
        > i=func(objs);
        > for(int j=0;j<i;j++)
        > {
        > cout<<(*objs[i]).ret_seq()<<en dl; //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++[/color]

        Visit alt.comp.lang.l earn.c-c++ newsgroup, it might be more
        beneficial for you as a beginner.

        Victor


        Comment

        • Krzysztof

          #5
          Re: Problem with the function that takes some_object** as the argument

          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

          Comment

          Working...