![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
I don't consider "using namespace std;" as laziness or as defeating the purpose of namespaces. I tend to avoid it because it can introduce ambiguity into code. Ambiguity in code means a lot of problems, including failure to compile (you confuse the compiler).
The reason I don't tend to use "using namespace std;" when posting examples in forums like this one is because some people simply cut and paste code samples written with a "using namespace std;" into a bigger code base. That can trigger compiler errors depending on what other namespaces there are using, what "using" directives are active and what other classes/functions/etc they have defined. And then the person doing that comes back complaining about being misinformed when, in reality, it is because they have used the example in an invalid manner. By avoiding using namespace std; I allow people to cut and paste (should they really want to) and don't have to worry about having to fix up a later mess because they cut and pasted the example in a way that broke their code. And, yes, I do consider that the many text books that encourage beginners to employ "using namespace std;" are doing a disservice. "using namespace std;" is a directive that works correctly in simple cases used in text books, but tends to trip people up later when they work on larger programming problems with multiple namespaces. The reason is that "using namespace std;" works VERY DIFFERENTLY from the way those textbooks encourage a beginner to think about it. One technique of effective teaching is to teach part of the truth, and fill in details later. Teaching blatant misinformation that has to be unlearnt later is a much less effective teaching technique, in the long run. |
|
|
|
|
|
#12 | |||
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
Quote:
A pointer solution is as easy as passing the pointer back to the caller; calling convention will dictate how this is done, either via EAX register or by stack, either way there are FAR less opcodes executed and if you're doing this hundreds of times per second you want this approach. Quote:
Quote:
I don't know what you keep calling pointers "raw pointers". It seems that you want to spark an argument (trolling?) just because you think an efficient pointer approach (which is completely provable via assembly output) is too "messy". If you code it carefully, it won't be. If you write an application that tosses pointers around like they are nothing, and don't take the time to track what points to what and what has been allocated and what needs to be freed, obviously you are going to have a huge mess. All I'm saying is the pointer approach is the best if you want efficienty, and I honestly don't know how you can call yourself an expert if you didn't know this stuff anyhow. This is C++ 101 information. |
|||
|
|
|
|
|
#13 |
|
Programmer
|
@grumpy
kk, thanks for the info! Always good to learn something new
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former. |
|
|
|
|
|
#14 | ||||||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
The C++ standard also explicitly allows compilers to avoid creating temporaries if the only means of detecting those temporaries is by tracking constructor and destructor calls. One of the common optimisations that this allows C++ compilers to implement is Return Value Optimisation (RVO), which essentially means they do not create additional temporaries when returning an object from a function. RVO is a VERY basic optimisation for a compiler writer: compiler technologies for several programming languages predating C++ have implement RVO for at least 20 years. Quote:
RAII are simple techniques that, among other things, PREVENT memory leaks. I might be slow, but I fail to see how using a tool to find leaks and eliminate them is more effective use of programmer time than using a relatively simple and consistent technique to prevent them in the first place. And valgrind is also Linux specific. It may sound strange, but some of us actually do development on machines that don't run linux. Not all of those machines have free memory analysis tools available. And, even if we assume everyone runs Linux, there is still an effort for learning the tool and using it. Quote:
Quote:
Quote:
Working with pointers is relatively easy if the code that releases dynamically allocated data is written by the programmer who allocated it, and is also relatively easy for data with a short duration. When you start doing non-trivial things (eg data with a long lifetime, complex conditions for allocating and releasing it), the work is more difficult. It gets even more difficult when different programmers write the different parts of the code (eg one allocates the data and does something with it, another programmer does things with it, and yet another eventually releases it). Quote:
Second, you may view this as "C++ 101 information", but it is not. Working with pointers is one of the most common things that C/C++ newbies get in trouble with. And part of the problem is that people like you say you won't get in trouble if you "code carefully", but you have only written programs of (say) a few hundred or [less likely] a few thousand lines of code. A characteristic of such tiny or small programs is that it is possible for a single person to wrap their mind around what the code is doing. And people still get in trouble with pointers on those programs. When the program gets larger, more complex, or there are serious implications when it fails, your platitudes of "code carefully" completely fail to cut the mustard. If the scope of the program is large enough that no one person can wrap their mind around what it does, it is necessary to use techniques that work on such larger systems, are easy to understand (that doesn't necessarily imply easiest to understand), and serve to eliminate errors rather than relying on "careful coding". Fortunately, the techniques that work on large scale systems also work pretty well on small scale systems. Unfortunately, techniques that work on small scale systems often DO NOT scale up to work on larger systems and techniques that are problematical on small systems (eg using raw pointers) scale up into massive problems on larger systems. Third, there are a lot of case studies where code which uses raw pointers have been compared with functionally equivalent code using the C++ standard library (eg using vectors rather than a new'd array). Yes, there are trade-offs. Efficiency (by which you actually mean run time speed), in practice, is rarely one of them. Most operations with (say) std::vector<int> actually have comparable efficiency to those involving working with a pointer that address a new'd array. Some non-trivial operations (eg sorting, reversing, etc) have actually been proven to do better, because the code written by a professional library implementer is more efficient than the code which a novice would write to achieve the same thing. If you follow the links I gave in an earlier post (and links from those links), you will find some of those case studies. |
||||||
|
|
|
|
|
#15 |
|
Expert Programmer
|
Thanks for the explaination, now i need to work on changing my habit :p
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#16 |
|
Newbie
|
Well, it's also not very wise to have using-directives in header files, sinc those will be included in other files when somebody includes that headerfile. It can save you from a lot of trouble!
__________________
«Don't touch what you can't grab!» |
|
|
|
|
|
#17 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#18 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
Given a choice, I'd prefer to have a program that can be built correctly without excessive effort, even if I lose some runtime performance. The idea of a program that generates the wrong results very efficiently, or takes a lot of effort to actually get right, is not particularly appealing. |
|
|
|
|
|
|
#19 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Very interested responses to the debate I seem to have indirectly started. I've learned a lot.
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#20 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
"Computers are getting faster - people aren't"
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|