Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

  • 1
  • 6
  • 7
  • 8
  • 9
  • 10
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 12:29 pm

whm1974 wrote:
Anyway looking at the code it sure does look like a nightmare language to program in. :o

Some people say that about C/C++. :wink:

"What do you mean I need to do my own memory management? That's crazy talk!"
Nostalgia isn't what it used to be.
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 5:33 pm

just brew it! wrote:
whm1974 wrote:
Anyway looking at the code it sure does look like a nightmare language to program in. :o

Some people say that about C/C++. :wink:

"What do you mean I need to do my own memory management? That's crazy talk!"

C maybe... C programmers seem to be in love with cleverness and terseness.

There's really no excuse for it in C++. C++ code should be readable. The compiler optimizers are better than humans at optimizing code these days anyway, so getting clever to save a few cycles can backfire as frequently as it helps.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 5:40 pm

Buub wrote:
just brew it! wrote:
whm1974 wrote:
Anyway looking at the code it sure does look like a nightmare language to program in. :o

Some people say that about C/C++. :wink:

"What do you mean I need to do my own memory management? That's crazy talk!"

C maybe... C programmers seem to be in love with cleverness and terseness.

There's really no excuse for it in C++. C++ code should be readable. The compiler optimizers are better than humans at optimizing code these days anyway, so getting clever to save a few cycles can backfire as frequently as it helps.

Even so shouldn't programmers still need to know how to memory management?
 
Redocbew
Minister of Gerbil Affairs
Posts: 2495
Joined: Sat Mar 15, 2014 11:44 am

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 5:47 pm

It's best if you do just to help in avoiding doing something boneheaded, but I think Buub was referring more to the kind of "micro-optimizations" that may shave a some CPU time off the program, but do so at the expense of readability.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 5:55 pm

Redocbew wrote:
It's best if you do just to help in avoiding doing something boneheaded, but I think Buub was referring more to the kind of "micro-optimizations" that may shave a some CPU time off the program, but do so at the expense of readability.

I see, Yeah it would be pretty bad not to be able to read your own code later when you really need to badly do so. :o
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 5:55 pm

whm1974 wrote:
Even so shouldn't programmers still need to know how to memory management?

Part of the point of languages with background GC (Python and Java, to name two) is that no, the programmer does not need to worry about memory management. It is all supposed to work magically behind the scenes. And in general, it does. Until it doesn't. :wink:
Nostalgia isn't what it used to be.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Mon Nov 20, 2017 5:58 pm

just brew it! wrote:
whm1974 wrote:
Even so shouldn't programmers still need to know how to memory management?

Part of the point of languages with background GC (Python and Java, to name two) is that no, the programmer does not need to worry about memory management. It is all supposed to work magically behind the scenes. And in general, it does. Until it doesn't. :wink:

Maybe I'm just a control freak then that just wants to know what is going on behind the scenes.
 
DancinJack
Maximum Gerbil
Posts: 4494
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 12:19 am

whm1974 wrote:
Even so shouldn't programmers still need to know how to memory management?


If you get this far in C/C++, let me know if you feel the same way after you learn another language that does things for you.

POINTERS!!! POINTERS EVERYWHERE!!!!!!!!!!!!!!!!!
i7 6700K - Z170 - 16GiB DDR4 - GTX 1080 - 512GB SSD - 256GB SSD - 500GB SSD - 3TB HDD- 27" IPS G-sync - Win10 Pro x64 - Ubuntu/Mint x64 :: 2015 13" rMBP Sierra :: Canon EOS 80D/Sony RX100
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 12:46 am

DancinJack wrote:
whm1974 wrote:
Even so shouldn't programmers still need to know how to memory management?


If you get this far in C/C++, let me know if you feel the same way after you learn another language that does things for you.

POINTERS!!! POINTERS EVERYWHERE!!!!!!!!!!!!!!!!!

I didn't say that programmers should do memory management manually, but they should know how to do it.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 1:13 am

DancinJack wrote:
whm1974 wrote:
Even so shouldn't programmers still need to know how to memory management?

If you get this far in C/C++, let me know if you feel the same way after you learn another language that does things for you.

POINTERS!!! POINTERS EVERYWHERE!!!!!!!!!!!!!!!!!

I do C/C++ and Python in roughly equal measure these days. Overall I wouldn't say I prefer one over the other. They each have their niche, and I'm fine with doing explicit memory/pointer management when it's appropriate (generally for performance reasons).
Nostalgia isn't what it used to be.
 
Pancake
Gerbil First Class
Posts: 161
Joined: Mon Sep 19, 2011 2:04 am

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 1:31 am

whm1974 wrote:
I didn't say that programmers should do memory management manually, but they should know how to do it.

You've got some strange ideas of what programmers should or shouldn't be doing - for a complete noob! My advice - don't take on any prejudices or preconceptions. Have an open mind about everything when you're learning otherwise you'll create all sorts of artificial barriers for yourself.

In actuality, having a heap you can rely on to allocate/deallocate chunks of memory is a cop-out. A crutch. If you're really down to the bare metal you will deal with one large chunk of RAM and have at it - as we would have to in the old days. A heap is - of course - far better. But it is also an abstraction. The CPU doesn't know there's such a thing.

In actuality, even in a high level language like Java with garbage collection you STILL have to be quite careful how you manage your memory and other resources. But it's nicer not having to think about the minutiae and focus on the design/implementation. Garbage collector > C-style heap management. Garbage collection can even be a performance win as with the JVM which can do it in separate thread(s) helping take advantage of multi-core processors. But whatever heap model you're using a GOOD programmer will be mindful they're not thrashing it by unnecessary use.

In actuality, 99% of software developers have no need to know about free()/malloc(). We've been freed from the tyranny of explicit memory management for the vast majority of our work. And that's a good thing.
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 11:31 am

whm1974 wrote:
Buub wrote:
just brew it! wrote:
Some people say that about C/C++. :wink:

"What do you mean I need to do my own memory management? That's crazy talk!"

C maybe... C programmers seem to be in love with cleverness and terseness.

There's really no excuse for it in C++. C++ code should be readable. The compiler optimizers are better than humans at optimizing code these days anyway, so getting clever to save a few cycles can backfire as frequently as it helps.

Even so shouldn't programmers still need to know how to memory management?

You definitely want to know how it works. There's value in knowing how data structures are built, for example. But I would smack any of our C++ developers who was writing their own linked-lists or map containers, because the standard ones supplied with the compiler are almost always as good as, if not better than, something you could write yourself.

C++ is relatively unique among programming languages in its RAII model of resource management. It's basically scope-based destruction, if used properly, which means that it's almost always correct, and it's much more aggressive about freeing resources than a garbage-collected language. More modern versions of C++ (C++14 and C++17) are also moving strongly into value semantics, in which you don't have to manage memory at all.

Between the use of value semantics, unique_ptr, smart_ptr, and appropriate use of RAII, there is almost no explicit management of "memory" in a normal application.
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 11:36 am

Redocbew wrote:
It's best if you do just to help in avoiding doing something boneheaded, but I think Buub was referring more to the kind of "micro-optimizations" that may shave a some CPU time off the program, but do so at the expense of readability.

Yes, this too. Make the code correct, first, then optimize later. Making the code well architected and readable is frequently more valuable than clever optimizations for two reasons: 1) You may be optimizing something that isn't even a bottleneck, and making the code harder to maintain for no real benefit, and 2) the compiler might already be doing as good a job as you could.

Of course, this is not to say that you shouldn't be performance aware. You absolutely should. Don't make a bunch of n-squared cost loops. Don't do stupid things. Make "free" optimization habits (pre-increment/pre-decrement rather than post-increment/post-decrement all your variables, except where the latter is explicitly needed, for example).

But make the code correct, without doing dumb things. Worry about the clever optimizations later.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 11:47 am

Pancake wrote:
whm1974 wrote:
I didn't say that programmers should do memory management manually, but they should know how to do it.

You've got some strange ideas of what programmers should or shouldn't be doing - for a complete noob! My advice - don't take on any prejudices or preconceptions. Have an open mind about everything when you're learning otherwise you'll create all sorts of artificial barriers for yourself.

Choosing the right tool for the job can make a huge difference in implementation difficulty, and whether or not you meet your performance and resource constraints. Writing an OS kernel in Python would be stupid; OTOH, using C as glue logic to tie a bunch of existing CLI tools together and coordinate them is just creating a lot of extra work for yourself.

@whm, at this point you're probably going to fall into the "when all you have is a hammer, everything looks like a nail" trap; that's to be expected, since you're learning your first programming language. Don't worry about it too much right now, just be aware that C isn't the best tool for everything.

Pancake wrote:
In actuality, having a heap you can rely on to allocate/deallocate chunks of memory is a cop-out. A crutch. If you're really down to the bare metal you will deal with one large chunk of RAM and have at it - as we would have to in the old days. A heap is - of course - far better. But it is also an abstraction. The CPU doesn't know there's such a thing.

Not sure I'd call it a cop-out. The way you'd likely deal with "one large chunk of RAM" would be to write a module with some utility functions to help you manage it. That module might even end up looking a lot like malloc() and free(). :wink:

Pancake wrote:
In actuality, even in a high level language like Java with garbage collection you STILL have to be quite careful how you manage your memory and other resources. But it's nicer not having to think about the minutiae and focus on the design/implementation. Garbage collector > C-style heap management. Garbage collection can even be a performance win as with the JVM which can do it in separate thread(s) helping take advantage of multi-core processors. But whatever heap model you're using a GOOD programmer will be mindful they're not thrashing it by unnecessary use.

I've also seen GC cause big problems. If you've got an application with a lot of in-RAM data where some of that data is seldom accessed (and consequently gets paged out to the swap file), a global GC can be deadly. The GC causes a crapload of page faults because it is sweeping through the entire heap, and the application goes unresponsive for a significant period of time. Very hard to deal with that sort of thing effectively, unless you're willing to turn swap off (which comes with its own set of issues).

Pancake wrote:
In actuality, 99% of software developers have no need to know about free()/malloc(). We've been freed from the tyranny of explicit memory management for the vast majority of our work. And that's a good thing.

99% is probably a little high, given the continuing popularity and ubiquity of C. But yeah, in a lot of cases use of a language with a GC heap makes more sense.
Nostalgia isn't what it used to be.
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 1:22 pm

Buub wrote:
Redocbew wrote:
It's best if you do just to help in avoiding doing something boneheaded, but I think Buub was referring more to the kind of "micro-optimizations" that may shave a some CPU time off the program, but do so at the expense of readability.

Yes, this too. Make the code correct, first, then optimize later. Making the code well architected and readable is frequently more valuable than clever optimizations for two reasons: 1) You may be optimizing something that isn't even a bottleneck, and making the code harder to maintain for no real benefit, and 2) the compiler might already be doing as good a job as you could.

Every time I've seen these little tricks and tips, they've turned out to be wrong in the end because compilers and processors have advanced since the trick was invented. Take a look at Duff's device and how that turned out in the X server code.

Buub wrote:
Of course, this is not to say that you shouldn't be performance aware. You absolutely should. Don't make a bunch of n-squared cost loops. Don't do stupid things. Make "free" optimization habits (pre-increment/pre-decrement rather than post-increment/post-decrement all your variables, except where the latter is explicitly needed, for example).

But make the code correct, without doing dumb things. Worry about the clever optimizations later.
Totally agree with the n-squared cost loops UNLESS you can guarantee that this is only ever used for small values of n - and by guarantee I mean a fundamental limit of what you are dealing with, not just what the dataset is today because sometime in the future someone will increase it!

However I would say the pre vs post increment falls in to one of those tips and tricks, in C with a modern gcc they evaluate to the exact same thing so write the one that is clearer in the code intent. The answer may be different for different object types in C++ and in other languages, but don't put it everywhere and make readability suffer.
 
liquidsquid
Minister of Gerbil Affairs
Posts: 2661
Joined: Wed May 29, 2002 10:49 am
Location: New York
Contact:

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 1:48 pm

Totally jumping into this late in the game, but honestly I believe a batter way to learn C is to learn in on a small micro-controller development kit with limited resources. If you don't really feel like learning the nuts and bolts of writing "to the iron" drivers, go to Freescale (NXP/Qualcomm), get a cheap Kinetis processor development kit, and use Processor Expert to create the drivers you wish to play with through a GUI. Then you only need to worry about your application, and you can learn a lot by examining the code Processor Expert creates for the drivers. Most dev environments are Eclipse-based now, and have everything you need all set to go upon install.

Personally I try to avoid malloc() and its ilk. For one, usually RAM is limited on small MCUs. For another, if it isn't VERY carefully managed, your heap can get fragmented on a smaller processor over time to the point you can no longer get a new chunk of continuous memory even if there is plenty available. It is a long-term operation issue on embedded devices, especially ones that can (hopefully) run for years at a time.

When I absolutely need dynamic allocation, I first think "can I pull this off by using static/const stuff?" If not, I write wrappers to help assure that things are created and destroyed in mirror-order. That usually means a layer on top of malloc() that will defer de-allocations until order can be maintained. It gets ugly on small systems that need to run "forever" so yes, malloc() without a behind the scenes defragmenter stinks.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 1:59 pm

liquidsquid wrote:
Personally I try to avoid malloc() and its ilk. For one, usually RAM is limited on small MCUs. For another, if it isn't VERY carefully managed, your heap can get fragmented on a smaller processor over time to the point you can no longer get a new chunk of continuous memory even if there is plenty available. It is a long-term operation issue on embedded devices, especially ones that can (hopefully) run for years at a time.

If you have a finite list of structure types that need to be dynamically allocated, and you can place a hard upper limit on the number of each kind you will ever need, you can statically allocate (or malloc() just once on application start-up) an array for each structure type to serve as a pool, then manage those pools explicitly. I've used this approach to good effect implementing display lists and associated structures for graphics applications. At the start of each frame render, just reset all of the pools to empty, and don't use malloc() anywhere inside the main rendering loop. Makes memory leaks and heap fragmentation logically impossible.
Nostalgia isn't what it used to be.
 
Pancake
Gerbil First Class
Posts: 161
Joined: Mon Sep 19, 2011 2:04 am

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 2:22 pm

just brew it! wrote:
Not sure I'd call it a cop-out. The way you'd likely deal with "one large chunk of RAM" would be to write a module with some utility functions to help you manage it. That module might even end up looking a lot like malloc() and free(). :wink:


Oh, totally!

Here's Pancake's List of what Real Men (tm) do ;-)

Real Men design and build their own computers from scratch (from ICs, wire wrap and circuit boards) *tick* *tick* *tick*

Real Men write their own pre-emptive multi-tasking hard realtime operating systems *tick* *tick*

Real Men write their own (in my case C-like) language, compilers and virtual machines *tick*

Real Men brew their own alcohol beer/wine/spirits *tick*

...

To be honest, I dropped out of the IT industry many years ago. I do my own thing. I plant lots of native plants as part of environmental rehabilitation and build beautiful gardens using native plant species.

But there's a joy in computation. In exploring algorithms and the unknown. Mathematics is the frontier of the mind. This is my country: http://fluffysoft.com.au/war/Com_fluffysoft_web.html
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 2:29 pm

I built the keyboard controller of my first PC (and wrote the device drivers for the keyboard, disk, and display) from scratch, wrote a "soft" real-time OS, and brew beer and hard cider. I guess I'm about half of a Real Man in your book? :lol:
Nostalgia isn't what it used to be.
 
liquidsquid
Minister of Gerbil Affairs
Posts: 2661
Joined: Wed May 29, 2002 10:49 am
Location: New York
Contact:

Re: GCC errors with compiling C code.

Tue Nov 21, 2017 4:06 pm

Pancake wrote:
I plant lots of native plants as part of environmental rehabilitation and build beautiful gardens using native plant species.
But there's a joy in computation. In exploring algorithms and the unknown. Mathematics is the frontier of the mind. This is my country: http://fluffysoft.com.au/war/Com_fluffysoft_web.html


*Really* real men do the above. And plant trees.
Terrible picture, but our natives and my front yard:
https://photos.app.goo.gl/kHIl91g2i2XBrIPe2

More of the "front yard": https://photos.app.goo.gl/24WT8qlvTnLjGCc73

I have been known to go out with many large trash bags and pull out invasive wild turnips from my field. Fully protected from sap, and sweating my ass end off. 6 acres of weeding takes a while, but there is no better effective way of controlling it without killing other plants.
 
derFunkenstein
Gerbil God
Posts: 25427
Joined: Fri Feb 21, 2003 9:13 pm
Location: Comin' to you directly from the Mothership

Re: GCC errors with compiling C code.

Wed Nov 22, 2017 10:11 am

Guess I have to give up my Real Man card. :lol:
I do not understand what I do. For what I want to do I do not do, but what I hate I do.
Twittering away the day at @TVsBen
 
DancinJack
Maximum Gerbil
Posts: 4494
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

Re: GCC errors with compiling C code.

Wed Nov 22, 2017 10:22 am

Can we please stop it with the real man ****? It's dumb and just, no.
i7 6700K - Z170 - 16GiB DDR4 - GTX 1080 - 512GB SSD - 256GB SSD - 500GB SSD - 3TB HDD- 27" IPS G-sync - Win10 Pro x64 - Ubuntu/Mint x64 :: 2015 13" rMBP Sierra :: Canon EOS 80D/Sony RX100
 
liquidsquid
Minister of Gerbil Affairs
Posts: 2661
Joined: Wed May 29, 2002 10:49 am
Location: New York
Contact:

Re: GCC errors with compiling C code.

Wed Nov 22, 2017 1:40 pm

DancinJack wrote:
Can we please stop it with the real man ****? It's dumb and just, no.

What, no real-man comebacks? j/k.

Oddly enough, I have hit most of the points he mentions as well. Never ever doing my own RTOS ever again. Took a year off my life. I will use FreeRTOS from here on out if I need one. Of course back when I made my own, FreeRTOS was nothing but a twinkle in someone's eye, and my primary role is a hardware engineer. The darned product it was used in is still alive and on the market today, and runs fine AFAIK. Most of the 2000+ piece electronics in the guts of the thing could be replaced by a 4-IC chipset between Xilinx and ADI. (AD9371) It would be 100,000x more flexible and powerful.
COM3010

Back to the point of the post, learning to develop code when there are too many development environment variables and project dependencies to work through can be a painful experience. It is best to start in a well-defined environment and grow from there with your experience level. The last thing you want to do is try to decode someone else's "clever" which winds up being a bunch of macros that require a certain version of a distribution and a few libraries from all over creation and have specific versions you cannot find just to compile a "Hello World".

I was only peripherally involved in code development on a Beaglebone Black - based project, and the distribution had a major overhaul in the middle of the project. There was plenty of infighting on the risk of switching to the new distribution due to the pain that was involved just getting the first one's development environment operational. It turned out the new distribution was MUCH easier.

Good luck with your learning!
  • 1
  • 6
  • 7
  • 8
  • 9
  • 10

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On