Thread: Tiny thing in a simple parser - C++

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by HelpMeC
    At the end of such method - do I have to delete manually the smallerOne variable before the return?
    Yes, you do. I would delete as soon as it is no longer used.

    Quote Originally Posted by HelpMeC
    I thought that at the end of the method - everything is destroyed, including instances of the related class.
    Am I wrong?
    All non-static local variables would be destroyed, and that includes class instances. But again, what you have here is an ordinary pointer that is the non-static local variable. So, the pointer will be destroyed, but not what it points to, unless you manually do that yourself.

    This is why we use smart pointers like std::unique_ptr instead: when the unique_ptr is destroyed because it falls out of scope, what it points to is also destroyed, so we don't have to manually destroy it.
    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

  2. #32
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Got it.
    What did you mean by these extra words: "and that includes class instances"? Can you maybe give an example to emphasize what that means?

    Thank you so much!!!

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by HelpMeC
    What did you mean by this extra words: "and that includes class instances"? Can you maybe give an example to emphasize what that means?
    See:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string s{"hello world!"};
        std::cout << s << std::endl;
        // no need to clean up s here
    }
    Contrast with:
    Code:
    #include <iostream>
    #include <string>
     
    int main()
    {
        auto p = new std::string{"hello world!"};
        std::cout << *p << std::endl;
        delete p;
    }
    Last edited by laserlight; 01-06-2020 at 08:09 AM.
    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

  4. #34
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Umm... yea, the difference between defining s here and defining smallerOne in my code -
    Here we construct s with known-type in compile-time - everything is occurred in the current stack-frame at run-time.

    In my code - the type is determined only in run-time, and that what leads us to use a clone, and actually to allocate a memory on the heap, and not on the current stack-frame. Thus, I have to delete it manually.

    You're a real C++ witch Thank you!!!


    As for std::string -

    When I only declare some variable this way:

    Code:
    std::string s;


    Some memory has been allocated for that at all? I guess no, it's only a declaration...

    BTW, why the compiler warns about declaring const like that:

    Code:
    const std::string invalidErr = "Invalid input\n";

    Compiler: Some exception can be thrown because static duration...
    What's the matter?

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by HelpMeC
    Some memory has been allocated for that at all? I guess no, it's only a declaration...
    It depends on context. If you're talking about the declaration of a local variable, then that declaration is a definition, so indeed memory has been allocated for it.

    Quote Originally Posted by HelpMeC
    BTW, why the compiler warns about declaring const like that:
    Post the smallest and simplest program that you expect to compile without any warnings, but which demonstrates this warning. Also, post the exact warning message corresponding to this simplified program.
    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

  6. #36
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Declaration - right, it has its required memory allocated in compile-time.

    Compiler warning - not sure how to do it right now so I won't waste your time and mine too as I must already do now something which is not programming (A lot of CALCULUS and Discrete Math *Double Facepalm*).

    Thank you again!

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by HelpMeC
    Declaration - right, it has its required memory allocated in compile-time.
    The memory allocation happens at run time, of course, but the compiler determines the amount of memory to be allocated for the size of the object at compile time, and then when the object is actually created, the constructor may arrange for the further dynamic allocation of memory.
    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

  8. #38
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight - Thanks for the correction.
    BTW, if I want to consider lines starting with some white-space as invalid input. Can I change something elegantly at the final version of the parser, or should I have to add a condition in the beginning: if the first char of the line is a whitespace -> invalid input?

    Thank you!

  9. #39
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Quote Originally Posted by john.c View Post
    blank_lines:
    I wouldn't say that *nix (the operating system) adds a newline at the end of a text file, although it is standard practice in *nix text editors to ensure that the last line ends in a newline. This tends to mean that if you supply a newline at the end of the last line then you will get an extra newline (a blank line) at the end. I believe that Windows doesn't do this, but I'm not sure.
    According to the relevant standard (POSIX), a text file contains zero or more lines of text; each line of text is zero or more characters followed by a single newline character (\n). Put simply, every line in a text file ends with a newline character, or in other words, the newline character is a line terminator. A two-line text file in Unix might look like this:

    Code:
    One\nTwo\n
    Windows, on the other hand, often treats the newline character* as a line separator, which is actually less desirable than treating the newline character as a line terminator.** A two-line text file in Windows might look like this:

    Code:
    One\r\nTwo
    * Windows uses the two-character sequence CR LF (\r\n), but they can be treated as if it's a single newline character when read in "text" mode in C or C++.

    ** Consider an empty (zero-length) file. In Windows, you can't tell if it contains no lines or a single blank line. In Unix you know it contains zero lines.

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by HelpMeC
    BTW, if I want to consider lines starting with some white-space as invalid input. Can I change something elegantly at the final version of the parser, or should I have to add a condition in the beginning: if the first char of the line is a whitespace -> invalid input?
    Well, the solution would be really simple with regex...

    I'd just check the first char. It's simple enough once you know the string is not empty.

    Here's another thing though: do you want to allow whitespace before and/or after the comma? As john.c pointed out, that's currently allowed.
    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

  11. #41
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Thank you so much!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple parser - K&R ex6-2
    By lukasjob in forum C Programming
    Replies: 3
    Last Post: 11-16-2011, 08:46 AM
  2. Simple parser
    By lruc in forum C Programming
    Replies: 5
    Last Post: 11-19-2009, 12:19 AM
  3. Thread safety for tiny simple functions
    By CodeMonkey in forum C++ Programming
    Replies: 16
    Last Post: 12-31-2008, 12:20 AM
  4. not able to understand this tiny tiny method
    By noobcpp in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2008, 10:42 AM
  5. Open Source Tiny Simple TFTP Server
    By DeepBlackMagic in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 03:27 PM

Tags for this Thread