what is bug in this code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: what is bug in this code

  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Lightbulb what is bug in this code

    see it there is a bug in returning from function because pointer value in function and main is different

    #include<iostream>
    #include<cstring>
    using namespace std;
    int* space(char[]);
    char* answer(char[]);
    int main(void)
    {
    char arraym[100];
    cout<<"Input string(max 100 character and max 10spaces):\n";
    cin.getline(arraym,100,'\n');
    int *pa=space(arraym);
    for(int i=0;i<10;i++)
    cout<<"space location"<<(pa[i])<<endl;
    return 0;
    }
    int* space(char arrays[])
    {
    int p=0;
    int ans[10]={0};
    int* pans=ans;
    cout<<arrays<<endl;
    // int tmp=sizeof(arrays)/sizeof(arrays[0]);
    for(unsigned int i=0;i<(strlen(arrays));i++)
    {
    cout<<"\n*****"<<" ";
    if(arrays[i]==' ')
    {
    cout<<"SPACE FOUND AT"<<i<<"th LOCATION"<<endl;
    pans[p]=i;
    p++;
    }
    }
    for(int i=0;i<10;i++)
    {
    cout<<"result in function body"<<pans[i]<<endl;
    }
    return pans;
    }

    in simple words my question is why value of pans and pa is different give any suggestion to make their value same.

    I am making a program which returns the number of words seprately on every next call. It is half of that program .
    What is problem in this program? when i compile and run value given by pans in function is different from the pa but i think these should same since function return pans which is transfered to pan.

    Thanks for replying.
    Last edited by vkash; May 25th, 2011 at 04:55 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: what is bug in this code

    After 38 posts you should know how to use code tags for posting code.

    Code:
    int ans[10]={0};
    int* pans=ans;
    .....
    
    return pans;
    'ans' is created on the stack of the function. This stack is deleted as soon as the function ends. So the address that 'pans' is pointing to doesn't exist anymore after the function is done.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: what is bug in this code

    You ahs been already told two day ago:
    Quote Originally Posted by Paul McKenzie View Post
    After over 30 posts, you should already know how to use code tags.

    That is not a code tag. You should have seen that the tags do not work when you preview your message.
    Why do you ignore what other people tell you?
    And how do you expect any further help after that?
    Victor Nijegorodov

  4. #4
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: what is bug in this code

    Quote Originally Posted by VictorN View Post
    You ahs been already told two day ago:Why do you ignore what other people tell you?
    And how do you expect any further help after that?
    OK now i will keep that in my mind there is no need to reply more to this thread

  5. #5
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Wink Re: what is bug in this code

    problem solved see it
    #include<iostream>
    #include<cstring>
    using namespace std;
    int* space(char[]);
    char* answer(char[]);
    int main(void)
    {
    char arraym[100];
    cout<<"Input string(max 100 character and max 10spaces):\n";
    cin.getline(arraym,100,'\n');
    int *pa=space(arraym);
    for(int i=0;i<10;i++)
    cout<<"space location"<<(pa[i])<<endl;
    delete pa;
    return 0;
    }
    int* space(char arrays[])
    {
    int p=0;
    //int ans[10]={0};
    int *ans=new int[10];
    int* pans=(ans);
    cout<<arrays<<endl;
    // int tmp=sizeof(arrays)/sizeof(arrays[0]);
    for(unsigned int i=0;i<(strlen(arrays));i++)
    {
    cout<<"\n*****"<<" ";
    if(arrays[i]==' ')
    {
    cout<<"SPACE FOUND AT"<<i<<"TH LOCATION"<<endl;
    pans[p]=i;
    p++;
    }
    }
    for(int i=0;i<10;i++)
    {
    cout<<"result in function body"<<pans[i]<<endl;
    }
    return pans;
    }

    special thanks to victorN and skizmo they help me to focus my problem any further suggestion will be veryyy helpfull.
    still some value of pa are abnormal i will try to remove those with 0.
    once again thanks.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: what is bug in this code


    And how about this part of the discussion:
    Quote Originally Posted by VictorN View Post
    You have been already told two day ago:
    Quote Originally Posted by Paul McKenzie
    After over 30 posts, you should already know how to use code tags.

    That is not a code tag. You should have seen that the tags do not work when you preview your message.
    Why do you ignore what other people tell you?
    And how do you expect any further help after that?
    Quote Originally Posted by vkash View Post
    OK now i will keep that in my mind there is no need to reply more to this thread
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,427

    Re: what is bug in this code

    Quote Originally Posted by vkash View Post
    problem solved see it
    First, use code tags when posting code. I believe I already told you about that.

    Second, your program uses wrong form of delete. I mentioned to that to you also.

    Third, your program will fail miserably if p > 9:
    Code:
         int *ans=new int[10];
         int* pans= ans;
    //...
         for(unsigned int i=0;i<(strlen(arrays));i++)
         {
             cout<<"\n*****"<<" ";
             if(arrays[i]==' ')
             {
                 cout<<"SPACE FOUND AT"<<i<<"TH LOCATION"<<endl;
                 pans[p]=i;  // error if p >= 9
                 p++;
             }
         }
    So your problem is not "solved". Instead of this error-prone form of programming, why not learn how to use std::vector and std::string?

    Edit: To show you that your program is still no good, what if the arrays had more than 10 space characters?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 25th, 2011 at 05:24 AM.

  8. #8
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Exclamation Re: what is bug in this code

    Quote Originally Posted by VictorN View Post

    And how about this part of the discussion:
    are you telling this thing to do
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
     int* space(char[]);
     char* answer(char[]);
     int main(void)
     {
         char arraym[100];
         cout<<"Input string(max 100 character and max 10spaces):\n";
         cin.getline(arraym,100,'\n');
         int *pa=space(arraym);
         for(int i=0;i<10;i++)
         cout<<"space location"<<(pa[i])<<endl;
         delete []pa;
         return 0;
     }
     int* space(char arrays[])
     {
         int p=0;
         //int ans[100]={0};
         int *ans=new int[10];
         int* pans=(ans);
         cout<<arrays<<endl;
        // int tmp=sizeof(arrays)/sizeof(arrays[0]);
         for(unsigned int i=0;i<(strlen(arrays));i++)
         {
             if(arrays[i]==' ')
             {
                 cout<<"SPACE FOUND AT"<<i<<"TH LOCATION"<<endl;
                 pans[p]=i;
                 p++;
             }
         }
         for(int i=0;i<10;i++)
         {
             cout<<"result in function body"<<pans[i]<<endl;
         }
         return pans;
     }
    if you are telling something different then explain that
    i think i have also solved mentioned bug by Paul
    Last edited by vkash; May 25th, 2011 at 05:58 AM.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: what is bug in this code

    Quote Originally Posted by vkash View Post
    are you telling this thing to do
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
     int* space(char[]);
     char* answer(char[]);
     int main(void)
     {
        ...
     }
    Exactly!
    Sure you see the difference between code snippets in your OP and in the last one! Don't you?
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 1999
    Posts
    27,427

    Re: what is bug in this code

    Quote Originally Posted by vkash View Post
    i think i have also solved mentioned bug by Paul
    I don't see where you fixed this bug. If someone were to enter a string with more than 10 spaces, your program continues to go on as if nothing is wrong.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: what is bug in this code

    I don't see where you fixed this bug. If someone were to enter a string with more than 10 spaces, your program continues to go on as if nothing is wrong.
    ups sorry i edit that i forget to upload edited code that is below
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
     int* space(char[]);
     char* answer(char[]);
     int main(void)
     {
         char arraym[100];
         cout<<"Input string(max 100 character and max 10spaces):\n";
         cin.getline(arraym,100,'\n');
         int *pa=space(arraym);
         for(int i=0;i<100;i++)
         {
             if(pa[i]>110)
             break;
             cout<<"space location"<<(pa[i])<<endl;
    
         }
         delete []pa;
         return 0;
     }
     int* space(char arrays[])
     {
         int p=0;
         int *ans=new int[100];
         int* pans=(ans);
         for(unsigned int i=0;i<(strlen(arrays));i++)
         {
             if(arrays[i]==' ')
             {
                 cout<<"SPACE FOUND AT  "<<i<<" th LOCATION"<<endl;
                 pans[p]=i;
                 p++;
             }
         }
        return pans;
     }
    are all bugs removed?
    I have tried it. It gives even more than 10 space count.
    actually the full question was

    5. (Advanced) Write a function that, when passed a string consisting of words separated by single
    spaces, returns the fi rst word; calling it again with an argument of NULL returns the second word,
    and so on, until the string has been processed completely, when NULL is returned. This is a
    simplifi ed version of the way the native C++ run - time library routine strtok() works. So, when
    passed the string ‘one two three ' , the function returns you ‘one' , then ’two' , and fi nally
    ’ three' . Passing it a new string results in the current string being discarded before the function
    starts on the new string.


    I try it for 8 hours i reach to the above code that was not full. Finally i download solution from wrox and that is
    Code:
    // Soln5_5.cpp
    #include <iostream>
    #include <cstring>
    using std::cin;
    using std::cout;
    using std::endl;
     
    char* parse(const char* str)
    {
       static char* pStr(nullptr);
       static size_t len(0);
       static size_t start(0);
       size_t pos(0);
       char* pReturn(nullptr);
    
       // First time through, save the string
       if (str)
       {
          delete[] pStr;   // in case it was allocated
          len = strlen(str);
          pStr = new char[len+1];
          strcpy_s(pStr, len+1, str);
       }
    
       if (start >= len)
          return nullptr;
    
       // Walk the string from 'start' till we find a blank or the end
       for (pos = start; pStr[pos] != ' ' && pStr[pos] != '\0'; pos++);
    
       // Copy the string if we've a word to return, otherwise return nullptr
       if (pos != start)
       {
          pReturn = new char[pos - start + 2];
          size_t i(0);
          for (size_t j=start; j<pos; i++,j++)
             pReturn[i] = pStr[j];
          pReturn[i] = '\0';
          start = pos+1;
          return pReturn;
       }
       else
       {
    	  delete[] pStr;             // We are done with the string
    	  pStr = nullptr;
          return nullptr;
       }
    }
    
    int main()
    {
       char s1[] = "seventy-one fruit balls, please Doris";
       cout << "string is '" << s1 << "'\n\nParsing...\n";
       char* p = parse(s1);
    
       while (p)
       {
          cout << p << endl;
    	  delete[] p;                // Delete the string that was returned
          p = parse(nullptr);
       }
    
       return 0;
    }
    thats all story of this program.
    Last edited by vkash; May 25th, 2011 at 08:39 PM.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,292

    Re: what is bug in this code

    Quote Originally Posted by vkash View Post
    I try it for 8 hours i reach to the above code that was not full. Finally i download solution from wrox and that is
    So now go through your code and the code from Wrox press line by line and see what the differences are. Then modify your program to work correctly. If you do this, not only will your program work correctly but you'll understand why it works correctly.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,427

    Re: what is bug in this code

    are all bugs removed?
    Why didn't you just change the loop?
    Code:
     int* space(char arrays[])
     {
         int p=0;
         int *ans=new int[10];
         int* pans = ans;
         int len = strlen(arrays);
         for(unsigned int i=0; i < len && p < 10;i++)
         {
             if(arrays[i]==' ')
             {
                 cout<<"SPACE FOUND AT  "<<i<<" th LOCATION"<<endl;
                 pans[p]=i;
                 p++;
             }
         }
        return pans;
     }
    5. (Advanced) Write a function that, when passed a string consisting of words separated by single spaces, returns the first word; calling it again with an argument of NULL returns the second word,
    and so on, until the string has been processed completely, when NULL is returned. This is a simplifi ed version of the way the native C++ run - time library routine strtok() works.

    So, when passed the string ‘one two three ' , the function returns you ‘one' , then ’two' , and finally’ three' . Passing it a new string results in the current string being discarded before the function starts on the new string.
    I guess that book's purpose is to have you reinventing wheels instead of showing how to use the standard library to write robust C++ programs.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center