CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    Is there anything wrong with the code?

    The code in the following, could you point out what is wrong? Why is it wrong? Thanks for your inputs.

    Code:
    void reverseString(const char* str1, char* str2)
    {
    	int len = strlen(str1);
    
    	str2 = new char[len+1];
    
    	char* p = const_cast<char*>(str1) + len - 1;
    
    	while(p>=str1)
    	{
    		*str2++ = *p--;
    	}
    
    	*str2 = '\0';
    
    }
    
    int  main()
    {
    
    	const char* s1 = "Tony";
    
    	char* s2;
    
    	reverseString(s1, s2);
    
    	cout<<s2<<endl;
    
    	return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there anything wrong with the code?

    Quote Originally Posted by dullboy
    The code in the following, could you point out what is wrong? Why is it wrong? Thanks for your inputs.
    Is this a homework question?

    I say that since there are obvious things wrong with it, but you haven't specified even one of the many things wrong with the code.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Is there anything wrong with the code?

    No, this is not a homework question. Sorry I didin't specify where it is wrong because I thought if you run this program, you will find out. Basically, there is an exception saying that "s2 is being used without being defined.". But I thought s2 is pointed to str2 which is allocated memory within function reverseString. Thanks for your inputs.
    Quote Originally Posted by Paul McKenzie
    Is this a homework question?

    I say that since there are obvious things wrong with it, but you haven't specified even one of the many things wrong with the code.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Is there anything wrong with the code?

    You pass char* s2; to your function reverseString() by value; that function overwrites your passed-in value with a pointer to newly allocated memory (that will leak at the end of that function); calling party is not aware of this action - the value of its s2 did not change and still is uninitialized..
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Is there anything wrong with the code?

    1) You do a "new[]" , but no "delete[]"

    2) You pass s2 by value to reverseString. In reverseString you change
    its value (when you do the new[]), but that will not be seen back in main()

    You need to pass : char ** or char *&

    3) Once you do the "new[]" , you should not change what str2 points to.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Is there anything wrong with the code?

    Quote Originally Posted by dullboy
    No, this is not a homework question. Sorry I didin't specify where it is wrong because I thought if you run this program, you will find out. Basically, there is an exception saying that "s2 is being used without being defined.". But I thought s2 is pointed to str2 which is allocated memory within function reverseString. Thanks for your inputs.
    Not sure what you mean by exception. The code didn't crash for me or throw any exceptions. It didn't work but it didn't crash either. I did get a warning that s2 is used prior to being set, which is true. You don't initialize the pointer to 0 prior to passing it to the function. Manipulating the pointers is a bad idea. At the end, s2 will be null because you keep moving the address pointed to by it and it will ultimately end up pointing directly to the NULL. You need to leave the pointers where they are at and just use array notation so that you are modifying the array without changing what the char* is pointing to.

  7. #7
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Is there anything wrong with the code?

    Thanks for VladimirF, Philip and Kempofighter. You guys' opinions are very helpful. Now I didn't change the value s2 points to so I need to pass char** s2 or char* &s2. But I still don't understand why s2 needs to be initailized before I pass it to reverseString? Thanks for your inputs.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there anything wrong with the code?

    Quote Originally Posted by dullboy
    No, this is not a homework question.
    Well one of the obvious things is that you used new[] without using delete[]. This is, or I thought would be the most glaring thing in the program that should have been noticed by anyone with familiarity with C++.

    That's why the original question smacked of a homework question.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there anything wrong with the code?

    Quote Originally Posted by dullboy
    Thanks for VladimirF, Philip and Kempofighter. You guys' opinions are very helpful. Now I didn't change the value s2 points to so I need to pass char** s2 or char* &s2. But I still don't understand why s2 needs to be initailized before I pass it to reverseString?
    It does not need to be initialized. Note that the uninitialized variable is just a warning, it isn't an error.

    Anytime you pass anything by value, and you haven't initialized what you passed, the compiler will give you a warning that you're passing a variable that is not initialized. Regardless of whether that entity is an int, a double, a char, a pointer, a pointer to a pointer, it doesn't matter as all of these are passed by value. If the entity isn't initialized, you get the warning.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Jul 2008
    Location
    dalian, China
    Posts
    36

    Re: Is there anything wrong with the code?

    The design of the function have some problem!

    First, you have new the heap memory in the function , the motion didn't gave any hint to the user!Any one who use that function didn't know he had secretly new a memory!He will forget to delete the memory!
    You are that kind!

    Second, the parament table also seem to weird!
    At the same time , the function can't process the new's exception!If the new is error, str = 0;It point to position of Managed by the OS!
    while execute the code as this :
    Code:
    *str2++ = *p--;
    Third, using const_cast often means a bad design!

    The design is the function design!
    Cigagou,Cogitou!

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Is there anything wrong with the code?

    Quote Originally Posted by active2volcano
    Third, using const_cast often means a bad design!
    Exacerbated by const_cast being unnecessary, since *p is not modified.
    Last edited by Graham; July 11th, 2008 at 05:15 AM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  12. #12
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Is there anything wrong with the code?

    As somebody will undoubtedly mention eventually, if you can use the STL then it all becomes dead simple.

    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int  main()
    {
    	const string s1("Tony");
    
            string s2(s1);
    
            reverse(s2.begin(), s2.end());
    
    	cout << s2 << endl;
    
    	return 0;
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  13. #13
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Is there anything wrong with the code?

    But if you run my original code in visual studio 2005, it will CRASH. So it looks like here uninitailized s2 is more than a warning. Thanks for your inputs.
    Quote Originally Posted by Paul McKenzie
    It does not need to be initialized. Note that the uninitialized variable is just a warning, it isn't an error.

    Anytime you pass anything by value, and you haven't initialized what you passed, the compiler will give you a warning that you're passing a variable that is not initialized. Regardless of whether that entity is an int, a double, a char, a pointer, a pointer to a pointer, it doesn't matter as all of these are passed by value. If the entity isn't initialized, you get the warning.

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Is there anything wrong with the code?

    Quote Originally Posted by dullboy
    But if you run my original code in visual studio 2005, it will CRASH. So it looks like here uninitailized s2 is more than a warning. Thanks for your inputs.
    It will most likely crash under ANY studio because you dereference uninitialized pointer.
    You are mixing two things:
    1. You have a compiler warning that you are using unitialized variable:
    warning C4700: uninitialized local variable 's2' used
    You only get ONE such warning per variable.
    2. You have a run-time error:
    Run-Time Check Failure #3 - The variable 's2' is being used without being initialized.
    when the run-time actually determins that you are using that uninitialized variable. You get this for every occurance. Consider this fragment:
    Code:
    	char* s2;
    	char* s3 = s2;
    	char* s4 = s2;
    Although you get two run-time failures, there is no crash (yet).
    However, this
    Code:
    line cout<<s2<<endl;
    will crash (most likely).
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Is there anything wrong with the code?

    Regarding 2), if I pass char*, the program still works. Here is the updated code.

    Code:
    int reverseString(const char* str1, char* str2)
    {
    	char* p;
    	int len;
    
    	len = strlen(str1);
    
    	p =const_cast<char*>(str1)+len-1;
    	
    	while(p>=str1)
    	{
    		*str2++ = *p--;
    	}
    
    	*str2 = '\0';
    	return 1;
    }
    
    int main()
    {
    	const char* str = "My name is dullboy";
    
    	char* str2;
     
    
    	str2 = new char[100];
    
    	reverseString(str, str2);
    
    	cout<<str2<<endl;
    
    	delete[] str2;
    	return 0;
    }
    Check the defintion of reverseString,
    int reverseString(const char* str1, char* str2). I passed char* not char** or char* & and I still get correct str2. Thanks for your inputs.
    Quote Originally Posted by Philip Nicoletti
    1) You do a "new[]" , but no "delete[]"

    2) You pass s2 by value to reverseString. In reverseString you change
    its value (when you do the new[]), but that will not be seen back in main()

    You need to pass : char ** or char *&

    3) Once you do the "new[]" , you should not change what str2 points to.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured