CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2013
    Posts
    42

    parsing a line for words...

    Hi,
    I'm getting this when I compile my program. Then, when the program runs I get an error. I assume it is because I have the warning for this problem. How can get around this error.

    warning C4172: returning address of local variable or temporary

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    
    char* parse_words(char* pstr, int strp);
    
    
    int main()
    {
    	char* strin("This is the string to be parsed");
    	cout << " String is: " << strin << " Length is: " << strlen(strin) << "\n\nParsing...\n";
    
    	/* cout << "Please enter a string to parse word-by-word: \n";
    	cin >> strin;
    	cout << "This is the string entered: " << strin << " \n.";
    	*/
    	//char * p(nullptr);
    	char* p = parse_words(strin, strlen(strin));
    	//char* p = parse(s1);
    	while (p)
    	{
    		std::cout << p << "\n.";
    		delete [] p;
    		p = parse_words(nullptr, strlen(strin));
    	}
    	return 0;
    
    }
    char* parse_words(char* pstr, int strp)
    {
    	static int start(0);
    	static int len(strp);
    	cout << "Value of len: " << len << '\n';
    	int lenpm(len);
    	int pos(0);
    	int x;
    	char* pReturn(nullptr);
    	int holdlen(20);
    	char holdarea[20];
    
    	cout << "Start start loop\n";
    	for (start; start <= len; start++)
    	{
    		cout << "Start loop: \n";
    		for (x = 0; x <= holdlen; x++)
    		{ 
    			holdarea[x] = ' ';
    		}
    		if (*pstr != ' ')
    			while (*pstr != ' ')
    			{
    				holdarea[x] = *pstr;
    				x++;
    				*pstr++;
    				start++;
    			}
    		return holdarea;
    	}
    
    	return pReturn;
    
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: parsing a line for words...

    Typically you'd pass in holdarea as an output parameter.

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

    Re: parsing a line for words...

    Quote Originally Posted by FenixRising View Post
    Hi,
    I'm getting this when I compile my program. Then, when the program runs I get an error. I assume it is because I have the warning for this problem. How can get around this error.

    warning C4172: returning address of local variable or temporary
    That isn't the only problem.

    First, it is undefined behaviour to return the address or reference to a local variable. The reason is why the variable is called local. When the function returns, that local variable no longer exists -- so what are you pointing to in main() when that function returns? Garbage.

    Second, why are you doing this?
    Code:
    delete [] p;
    Where is there a call to new[] in your program, and then assigning "p" to that value? You only call delete[] when you use new[]. Calling delete[] on an area of memory that was not dynamically allocated is again, undefined behaviour.

    Why don't you just use strtok() to parse the string?
    http://www.cplusplus.com/reference/cstring/strtok/

    In addition, there are other safer ways to parse a string in C++ for individual words. The first way is to get rid of this "C" type of coding and just call string::find_first_of() or string::find_first_not_of() in a loop.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 21st, 2013 at 09:09 PM.

  4. #4
    Join Date
    Apr 2013
    Posts
    42

    Re: parsing a line for words...

    First, delete [] p; was left in the program by mistake. Secondly, I am reading two books. I have read 550 pages in one and 450 in another. Both books were written and released in late 2012. The "C" type of coding you speak of comes from these books. And there is only a small amount of information on strings or maybe I missed it. If you know of a book that focuses just on C++ without the comparisons to "C" let me know. I'll be the first one in line to buy it.

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

    Re: parsing a line for words...

    Quote Originally Posted by FenixRising View Post
    Secondly, I am reading two books. I have read 550 pages in one and 450 in another. Both books were written and released in late 2012. The "C" type of coding you speak of comes from these books.
    Then those books aren't teaching C++. They are teaching you 'C'.
    And there is only a small amount of information on strings or maybe I missed it.
    So after 500 pages, there is no mention of std::string? Are you sure you are reading C++ books?
    If you know of a book that focuses just on C++ without the comparisons to "C" let me know.
    Accelerated C++ by Koenig & Moo.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2013
    Posts
    42

    Re: parsing a line for words...

    Ok, I have ordered the book. It will be here Wednesday.

    Regards,
    Bob

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: parsing a line for words...

    Which two books are you reading?

    I know one is c++ Primer Plus from a previous post which is considered quite good but doesn't really get into c++ proper until about chapter 10 when it introduces classes, and string and the other STL elements are not introduced until chapter 16.

    What's the other?

    You might also like to have a look at

    Exploring c++
    http://www.amazon.co.uk/Exploring-Th...loring+c%2B%2B

    Note that both Exploring c++ and Accelerated c++ don't cover the new parts of the c++11 standard.
    Last edited by 2kaud; September 22nd, 2013 at 11:24 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Apr 2013
    Posts
    42

    Re: parsing a line for words...

    The other book is Beginning Visual C++ 2012. Actually, I like both books. But, everyone seems to think I should be using strings. And, I am a little bit confused about that.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: parsing a line for words...

    In c++ there are effectively two types of 'strings'. There are the 'c' style strings which are an array of char terminated by the null character. This is what you have been using.

    Then there is the c++ string class that is part of the Standard Template LIbrary (STL). This is much more powerful and easier to use than the 'c' string. Unless there is a good reason not to, developers use the c++ string class.

    Learning to program in c++ using 'c' strings is learning a way of doing things which later may have to be 'unlearnt'. There are 'c' ways of doing things (which work with c++ mainly) and there are the 'c++' ways of doing things. Professional c++ developers tend to want programmers to learn the 'c++' way rather than the 'c' way. Just as you are using cin and cout (the c++ way) as opposed to using printf(), scanf() etc (which is the 'c' way) use type string (c++ way) as opposed to char* (c way).

    Many books, however, - even the good ones - tend to teach the 'c' way with some aspects of the language before showing the c++ way (ie 'c' style strings before the c++ strings). So when code is posted using the 'c' way of doing things (as opposed to the c++ way) this can be commented upon.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: parsing a line for words...

    Quote Originally Posted by FenixRising View Post
    But, everyone seems to think I should be using strings.
    Including the author of the C++ language himself:

    http://www.stroustrup.com/new_learning.pdf

    Regards,

    Paul McKenzie

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: parsing a line for words...

    You need to be comfortable with C style null terminated strings as they're still commonly used in a lot of APIs, including the Windows API. Use strings where you can, but you'll need to know both.

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