Live data from Hacker News

Deep C and C++ (2011)

slideshare.net

211–220 of 243 posts

Re: Deep C and C++ (2011)

#211
post #197

Earlier quoted context omitted.

turbo pascal is as unsafe as C is, just with more verbose syntax. it has pointers, pointer arithmetic, manual memory allocation, function pointers, casts, unions, you name it. I bet you could write could write 30 odd rules for cpp and then compile your "pascal" with cc, e.g.: #define begin { #define end } #define := = #define ^ * #define @ & and so on

No it is not, because: - It has real strings - Arrays don't decay to pointers - It has reference arguments, which avoid pointer usage for records and out arguments - Requires explicit casts for type conversions, instead of implicit type casts - Provides region allocators, which allow to release a full block of memory in one go - Has real enumerations that don't decay into ints - Allows writing generic code over array…

so C doesn't have proper type safe enums (though you could hack those up with typedef + cpp), but does require casts for most type conversions (except those for void*).

pascal style strings can be done with the preprocessor, as can generic iterators, allocation for idiots, and "out" function arguments.

memory pools are a library feature.

and the arrays are the same too, a pointer in pascal can be treated as an infinitely sized array, but the reverse isn't true, just like in C.

and, believe it or not, there are other languages in the C family, that provide safety in the same way there are for pascal.

tl;dr: pascal and C are more alike than different.

Re: Deep C and C++ (2011)

#212

Earlier quoted context omitted.

My guess you apply your experience with some bizarre language (PHP?) to C, which you never learned. C and C++ are internally consistent in the same way a math theory is. Many programming languages are not, indeed. > You could make many different tiny changes to the rules of C and still end up with a workable programming language. (Many people have done that, there are tons of languages derived from C.) Coincidentally…

Saying that C++ is internally consistent at all is a huge exaggeration. Actually I'd be very careful with stating it is more consistent than PHP. Both have many sharp-edges and in both there are plenty of features that feel "tacked on" and are non-orthogonal to each other. E.g. why I can't make a generic (template) function virtual or why I cannot overload some overloadable operators to have exactly same semantics as…

You can't make a member function template virtual because it's just a template, the function doesn't exist until it's statically instantiated. Instantiation can happen across units of compilation, making it impossible for the compiler to generate a vtable with all the instantiations within it. It's not a "tacked on" feature/limitation at all, and I don't see how any statically typed language that allowed function overloading could really work around that issue.

I'm not sure what you mean about the operator overloading thing.

Re: Deep C and C++ (2011)

#213
post #81

I haven't seen these tricks come into play when solving real problems at ALL. Not saying you should not know the language, but better to have problem-solving skills than rot the standard. The bit about leaving a new line after main tells me she really has memorized the standard.

It's not about tricks, it's about precisely understanding the mechanics of the language. This can be exploited to craft tricks, which is occasionally important. It can also be exploited to avoid gotchas, which is usually important. It can also be exploited to better and more quickly understand what some behavior means when things aren't working as expected, which is always important.

I guess it depends on what you consider "real problems". One can always write good and efficient code by following what he/she knows is the defined behavior. Just found weird that some things related to standards were given more importance whereas problem solving is the key while writing any program.

Re: Deep C and C++ (2011)

#214
post #213

Earlier quoted context omitted.

It's not about tricks, it's about precisely understanding the mechanics of the language. This can be exploited to craft tricks, which is occasionally important. It can also be exploited to avoid gotchas, which is usually important. It can also be exploited to better and more quickly understand what some behavior means when things aren't working as expected, which is always important.

I guess it depends on what you consider "real problems". One can always write good and efficient code by following what he/she knows is the defined behavior. Just found weird that some things related to standards were given more importance whereas problem solving is the key while writing any program.

Well, I'm currently being paid to write C day to day, so it's solving someone's real-enough problem that they're willing to part with money. I described roughly how I see my comprehension of the language reflected in my work.

Re: Deep C and C++ (2011)

#215
post #213

Earlier quoted context omitted.

I guess it depends on what you consider "real problems". One can always write good and efficient code by following what he/she knows is the defined behavior. Just found weird that some things related to standards were given more importance whereas problem solving is the key while writing any program.

Well, I'm currently being paid to write C day to day, so it's solving someone's real-enough problem that they're willing to part with money. I described roughly how I see my comprehension of the language reflected in my work.

So am I and I've mostly used a subset of C features to solve many problems. I've also seen some C 'experts' fail to come to a proper solution even if they know all the features.

Re: Deep C and C++ (2011)

#216

Earlier quoted context omitted.

My guess you apply your experience with some bizarre language (PHP?) to C, which you never learned. C and C++ are internally consistent in the same way a math theory is. Many programming languages are not, indeed. > You could make many different tiny changes to the rules of C and still end up with a workable programming language. (Many people have done that, there are tons of languages derived from C.) Coincidentally…

Saying that C++ is internally consistent at all is a huge exaggeration. Actually I'd be very careful with stating it is more consistent than PHP. Both have many sharp-edges and in both there are plenty of features that feel "tacked on" and are non-orthogonal to each other. E.g. why I can't make a generic (template) function virtual or why I cannot overload some overloadable operators to have exactly same semantics as…

As nly pointed out - the virtual template function just does not make sense if you understand what it is. As for the operators I'd guess you mean && and ||? You cannot make them shortcut because the user defined operators are just functions and functions in C++ do always evaluate their arguments.

Re: Deep C and C++ (2011)

#217
post #194

Most of the C stuff here is pretty good, but some of the C++ information is a little questionable. Even the "good programmer" exhibits a few common misconceptions about inheritance and virtual destructors. Here's the text from slide 348: "What is the point of having a virtual destructor on a class like this? There are no virtual functions so it does not make sense to inherit from it. I know that there are programmers…

What would a resource management class look like?

It has a constructor (to initialize the resource), a destructor (to release the resource), copy/move constructors, and lets you get access to the underlying resource. See http://en.cppreference.com/w/cpp/memory/shared_ptr for an example.

Re: Deep C and C++ (2011)

#218
post #197

Earlier quoted context omitted.

No it is not, because: - It has real strings - Arrays don't decay to pointers - It has reference arguments, which avoid pointer usage for records and out arguments - Requires explicit casts for type conversions, instead of implicit type casts - Provides region allocators, which allow to release a full block of memory in one go - Has real enumerations that don't decay into ints - Allows writing generic code over array…

so C doesn't have proper type safe enums (though you could hack those up with typedef + cpp), but does require casts for most type conversions (except those for void*). pascal style strings can be done with the preprocessor, as can generic iterators, allocation for idiots, and "out" function arguments. memory pools are a library feature. and the arrays are the same too, a pointer in pascal can be treated as an infini…

> so C doesn't have proper type safe enums (though you could hack those up with typedef + cpp), but does require casts for most type conversions (except those for void*).

That still does not make them safe, implicit conversion to int will still happen.

> pascal style strings can be done with the preprocessor, as can generic iterators, allocation for idiots, and "out" function arguments.

No type safety.

> memory pools are a library feature.

That aren't part of any C compiler. So you can't count them being available.

> and the arrays are the same too, a pointer in pascal can be treated as an infinitely sized array, but the reverse isn't true, just like in C.

You lost me there.

> and, believe it or not, there are other languages in the C family, that provide safety in the same way there are for pascal.

Yes, that is why I use Modern C++, C#, Java, D and will only use C at gunpoint.

> tl;dr: pascal and C are more alike than different.

True, except in Pascal when bad things happen is because the developer explicitly wanted them to happen.

In C everything goes and to have Pascal's safety back, all modern C compilers provide static analyzers. Thus proving a point about the languages design's.

Re: Deep C and C++ (2011)

#219
I think this presentation is interesting from a programming language design point of view; if you were designing a new language, this presentation highlights various 'gotchas' in C and C++ that might be the sort of thing you would try to avoid creating in your new language.

Many of these fall under the heading of opportunities to apply the Pythonic design criterion "refuse the temptation to guess":

1) in C, according to one of the comments, it was claimed (i didn't check) that if you declare your own printf with the wrong signature, it will still be linked to the printf in the std library, but will crash at runtime, e.g. "void printf( int x, int y); main() {int a=42, b=99; printf( a, b);}" will apparently crash.

-- A new programming language might want to throw a compile-time error in such a case (as C++ apparently does, according to the slides).

2) In C, depending on compiler options, you can read from an uninitialized variable without a warning

-- A new programming language might want to not auto-initialize any variables, and to throw a compile-time error if they are used before initialization.

3) In C, code like "int a = 41; a = a++" apparently compiles but leaves 'a' in an undefined state because "you can only update a variable once between sequence points" or it becomes undefined, but on many compilers works anyway. A sequence point is "a point in the program's execution sequence where all previous side effects SHALL have taken place and all subsequent side-effects SHALL NOT have taken place".

-- A new programming language might want to throw a compile-time error in such a case

4) In C, the evaluation order of expressions is unspecified. so code like "a = b() + c()" can call b() and c() in any order. If they have side effects then this might matter, yet no compiler error is given. However, the evaluation order of a() && b() IS specified.

-- A new programming language might want to throw a compile-time error when side-effectful code is called in context in which the order of evaluation is unspecified.

Other miscellaneous gotchas:

5) In C, static vars (but not other vars) are initialized to 0 by default.

-- A new programming language might want to either auto-initialize all variables, or to not auto-initialize any variables,

6) The presentation says "C has very few sequence points. This helps to maximize optimization opportunities for the compiler.". This is a tradeoff between optimization vs. principal of least surprise.

-- A new programming language which wanted to make things as simple as possible would maximize 'sequence points', putting them in between practically every computation step. But some new programming languages would choose to minimize sequence points in order to allow the compiler to optimize as much as possible.

7) The presentation says that the standard says that source code must end with a newline.

-- Imo that's a bit pedantic and the ideal programming language would not care if code ended in a newline.

8) In one context (inside a function), the 'static' keyword is used to make a variable persist across calls to that function. But in another context (outside of any function), the same 'static' keyword is used as an access modifier to define visibility to other compilation units!

-- Using the same keyword for two different (albeit related) purposes is confusing. A new programming language might either drop one of those features entirely, or have a distinct keyword for it.

Re: Deep C and C++ (2011)

#220
post #212

Earlier quoted context omitted.

Saying that C++ is internally consistent at all is a huge exaggeration. Actually I'd be very careful with stating it is more consistent than PHP. Both have many sharp-edges and in both there are plenty of features that feel "tacked on" and are non-orthogonal to each other. E.g. why I can't make a generic (template) function virtual or why I cannot overload some overloadable operators to have exactly same semantics as…

You can't make a member function template virtual because it's just a template, the function doesn't exist until it's statically instantiated. Instantiation can happen across units of compilation, making it impossible for the compiler to generate a vtable with all the instantiations within it. It's not a "tacked on" feature/limitation at all, and I don't see how any statically typed language that allowed function ove…

This was a rhetoric question. I perfectly know why I can't do that in C++. C++ does not allow this because of particular generics implementation (done by static compile-time macro expansion), not because those two features are inherently contradictory. In fact, they are completely orthogonal and Java/Scala/C# have no problems with that, despite being statically typed.
Post reply on HN