Like Tree3Likes
  • 1 Post By jimblumberg
  • 2 Post By laserlight

Trying to code a C-style function as a C++ one

This is a discussion on Trying to code a C-style function as a C++ one within the C++ Programming forums, part of the General Programming Boards category; Good morning. I'm trying to convert this into a C++ style program: Code: #include <iostream> #include <string> const int MAXSIZE ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Trying to code a C-style function as a C++ one

    Good morning. I'm trying to convert this into a C++ style program:

    Code:
     
    #include <iostream> 
    #include <string> 
    
    const int MAXSIZE = 15;
    
    using namespace std; 
    
    unsigned int c_in_str(const char *str, char ch);
    
    int main(void) 
    { 
      char mmm[MAXSIZE] = "minimum";
      char *wail = "ululate";
      
      unsigned int ms = c_in_str(mmm, 'm');
      unsigned int us = c_in_str(wail, 'u');
      
      cout << ms << " m characters in " << mmm << endl; 
      cout << us << " u characters in " << wail << endl;      
        
      return 0;     
    }     
    
    unsigned int c_in_str(const char *str, char ch) 
    {
       int count = 0; 
       
       while(*str) 
       { 
         if(*str == ch) 
          count++; 
         str++; 
       }
       return count;                   
    }
    g++ complains I'm using char * in this case

    Code:
     
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    unsigned int c_in_str(const string str, char ch);
    
    int main(void) 
    { 
      string mmm  = "minimum";
      string wail = "ululate";
      
      unsigned int ms = c_in_str(mmm, 'm');
      unsigned int us = c_in_str(wail, 'u');
      
      cout << ms << " m characters in " << mmm << endl; 
      cout << us << " u characters in " << wail << endl;      
        
      return 0;     
    }     
    
    unsigned int c_in_str(const string str, char ch) 
    {
       int count = 0; 
       int i;
       while(str[i] != '\0') 
       { 
         if(str[i] == ch) 
          count++; 
         str[i++]; 
       }
       return count;                   
    }
    in this case, valgrind complains I'm not setting a size to mmm and wail.

    Code:
     
    thames@semaht ~/C++/Strings $ valgrind ./strgfun
    ==3259== Memcheck, a memory error detector
    ==3259== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
    ==3259== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
    ==3259== Command: ./strgfun
    ==3259== 
    ==3259== Use of uninitialised value of size 8
    ==3259==    at 0x400E58: c_in_str(std::string, char) (strgfun.cpp:26)
    ==3259==    by 0x400CA2: main (strgfun.cpp:13)
    ==3259== 
    ==3259== Use of uninitialised value of size 8
    ==3259==    at 0x400E19: c_in_str(std::string, char) (strgfun.cpp:28)
    ==3259==    by 0x400CA2: main (strgfun.cpp:13)
    ==3259== 
    ==3259== Use of uninitialised value of size 8
    ==3259==    at 0x400E58: c_in_str(std::string, char) (strgfun.cpp:26)
    ==3259==    by 0x400CD5: main (strgfun.cpp:14)
    ==3259== 
    ==3259== Use of uninitialised value of size 8
    ==3259==    at 0x400E19: c_in_str(std::string, char) (strgfun.cpp:28)
    ==3259==    by 0x400CD5: main (strgfun.cpp:14)
    ==3259== 
    3 m characters in minimum
    2 u characters in ululate
    ==3259== 
    ==3259== HEAP SUMMARY:
    ==3259==     in use at exit: 0 bytes in 0 blocks
    ==3259==   total heap usage: 2 allocs, 2 frees, 64 bytes allocated
    ==3259== 
    ==3259== All heap blocks were freed -- no leaks are possible
    ==3259== 
    ==3259== For counts of detected and suppressed errors, rerun with: -v
    ==3259== Use --track-origins=yes to see where uninitialised values come from
    ==3259== ERROR SUMMARY: 30 errors from 4 contexts (suppressed: 2 from 2)

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    3,263
    In the first code the following needs to be changed:
    Code:
      char *wail = "ululate";
      // To:
      const char *wail = "ululate";
    You need to match the const of the char pointer to the const of the string.

    In the second code the following line:
    Code:
    unsigned int c_in_str(const string str, char ch)
    The const on the string parameter is actually not required since you are sending a copy of the string to the function. However I would change the function to send a const string reference for the string instead of the copy.

    Code:
    unsigned int c_in_str(const string& str, char ch)
    {
       int count = 0;
       int i;
       while(str[i] != '\0')
    Now in the above code you never initialize i so when you use it in the while loop you get undefined behavior. This what valgrind is complaining about.

    Also since this is a std::string, I'm not sure the use of the end of string character is guaranteed to work. I suggest changing to a for loop for this and use the size of the string.
    Code:
       for(int i = 0; i < str.length(); ++i)
       {
          if(str[i] == ch)
             count++; 
        }

    Jim
    thames likes this.

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Many thanks Jim. I fixed the code.

    Code:
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    unsigned int c_in_str(const string& str, char ch);
    
    int main(void) 
    { 
      const string mmm  = "minimum";
      const string wail = "ululate";
      
      unsigned int ms = c_in_str(mmm, 'm');
      unsigned int us = c_in_str(wail, 'u');
      
      cout << ms << " m characters in " << mmm << endl; 
      cout << us << " u characters in " << wail << endl;      
        
      return 0;     
    }     
    
    unsigned int c_in_str(const string& str, char ch) 
    {
       int count = 0; 
       unsigned int i;
       for(i = 0; i < str.length(); i++) 
        if(str[i] == ch) 
         ++count;
       return count;                   
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    You could do without that c_in_str function by using std::count from <algorithm>, e.g.,
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main(void)
    {
      const string mmm  = "minimum";
      const string wail = "ululate";
    
      unsigned int ms = count(mmm.begin(), mmm.end(), 'm');
      unsigned int us = count(wail.begin(), wail.end(), 'u');
    
      cout << ms << " m characters in " << mmm << endl;
      cout << us << " u characters in " << wail << endl;
    
      return 0;
    }
    thames and iMalc like this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    You could do without that c_in_str function by using std::count from <algorithm>, e.g.
    cool! I didn't know that. Many thanks laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  2. iterator vs old-style c-code
    By curos in forum C++ Programming
    Replies: 4
    Last Post: 07-04-2005, 03:23 PM
  3. Fixing old C-style code
    By tunafish in forum C Programming
    Replies: 6
    Last Post: 07-01-2005, 07:24 AM
  4. Code in MLA style papers
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-14-2003, 07:08 AM
  5. reinterpret_cast, C-style cast or function-style cast
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2002, 10:07 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21