Thread: Java can do it, why not a IDE compilier give a warning,

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    19

    Java can do it, why not a IDE compilier give a warning,

    Here is something strange, As programmers allocate memory space to their program, "they" including me, forget to release the memory, JAVA has no trouble releasing used memory that is no longer needed in the program,
    So the big million dollar question is, Why not have the IDE compiler, give a warning if it can not find a free() or delete[] for every memory block of memory put aside in the program.




    While I was getting back into writing and reading to text files, I would have to open a file, but after everything work OK, I commented out the "close file" line,, I never ran the program, but it did compile and link alright.
    But maybe if I pay good money on a compiler, maybe it will do all the things I have highlighted above.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    C++ can do everything Java can do, if you learn to use it properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    19
    Quote Originally Posted by Salem View Post
    C++ can do everything Java can do, if you learn to use it properly.
    I do not doubt that Salem

    I have used a program called "HTML tidy" to alert me if my website pages written with a simple text editor had mistakes in it, one page I did was ok with Internet explorer, but was a complete mess with Opera, that was because Internet Explorer was bloated with code to fix up poorly written HTML, like it might start with a table row <tr> but no end of table row </tr> that was why it was a mess with opera back in the late 1990's. The HTML tidy would pick up if there was no closing tags, along with lots of other errors.

    So I thought why not get a C, C++ IDE compiler not to "tidy up" any C / C++ code, but warn a allocated block of memory was used in the program but it was not release, just like JAVA does automatically, along with making sure a opened file was closed, but you would have to be a student learner "lost in space" to miss out the "close file". But not to release the memory is more of a "contagious error" a lot of programmers make.

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    59
    C++ compilers may raise a warning if you use `(m/c/re)alloc` or `new([])` in your code, because this indicates that you are about to violate RAII.

    In other words, C++ is object-oriented where the constructors (and possibly other functions) of a class acquire resources, and destructors are designed to automatically release them once the class object goes out of scope. So, whenever you allocate ressources yourself, rather consider this a design flaw in the first place and carefully check what library class could be used instead. At least use a smart pointer class.
    Last edited by aGerman; 2 Weeks Ago at 08:33 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    The IDE can't possibly check everything at compile time.

    If you don't want memory leaks in your C++ program, then never call new (at least not directly).
    If you must, then at least use the smart pointers available.
    Dynamic memory management - cppreference.com

    Or stick to containers like std::vector and std::list which manage resources behind the scenes for you.

    Java is different:
    You never allocate explicitly, you want an object instance, it goes away and makes one for you
    You never free explicitly, you just lose the last reference to it, and the garbage collector comes along sometime later and cleans up the mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shouldn't this give a warning?
    By homer_3 in forum C++ Programming
    Replies: 7
    Last Post: 03-25-2015, 08:04 PM
  2. Why it give warning message, how will it remove
    By hitlar in forum C Programming
    Replies: 2
    Last Post: 01-31-2014, 02:50 PM
  3. Free Windows C Compile that give good warning?
    By stahta01 in forum C Programming
    Replies: 2
    Last Post: 11-24-2010, 09:20 PM
  4. Having trouble with my compilier (the very first step)
    By BrandNew in forum C Programming
    Replies: 6
    Last Post: 05-09-2009, 04:45 PM
  5. What Compilier?
    By KaibaFan321 in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2004, 08:51 AM

Tags for this Thread