Page 4 of 10

Re: GCC errors with compiling C code.

Posted: Wed Nov 08, 2017 10:41 pm
by Redocbew
whm1974 wrote:
Well at least I'm learning to avoid writing bad code.


The phrase you might hear about that is "if the program works perfect with perfect input, then you're only half done".

Here's a real world example. At my last job I had a bug to deal with which seemed to be causing confusion between payment processors. At the time there was both paypal and a credit card available for payment methods, and it looked like a few people had managed to find a way to use both paypal and a credit card to place an order. It wasn't split payments where a portion was being placed on each. It was just a weird, bad order that the system choked on, and then caused the customer service folks headaches having to clean it up. It was doubly weird because the paypal entry form wasn't even part of the application its self. That was external, with service calls going back and forth between the two(hint: never really been a fan of paypal since then...). It turned out to be confined to cases where a credit card payment was declined for some reason and the user returned back to the payment page. The problem was that if they then tried to pay through paypal the CC fields were still populated with whatever garbage had been there previously. We cleared the other method any time they switched from one method to the other, and the bug was dead.

Re: GCC errors with compiling C code.

Posted: Wed Nov 08, 2017 10:48 pm
by just brew it!
whm1974 wrote:
just brew it! wrote:
In the real world, the process we just went through with that simple program would've been called a "code review". Code reviews are part of the process in well-run development groups. In a nutshell, you get one or more developers other than the original author of the code to analyze it, looking for potential bugs.

I can imagine that it is nearly impossible for one person to write code for highly complex and large program without being really buggy.

It is also more difficult than you might think to spot bugs when reading your own code, or even to test for them. If it did not occur to you to handle an unexpected situation or erroneous input (referred to as a "corner case") when you were writing the code, you're also less likely to think of it when testing the program. Ideally, the person who tests a piece of software should not be the person who wrote it.

But we're veering off into "software engineering best practices" as opposed to "learning how to program in language X"...

Re: GCC errors with compiling C code.

Posted: Wed Nov 08, 2017 10:52 pm
by whm1974
Redocbew wrote:
whm1974 wrote:
Well at least I'm learning to avoid writing bad code.


The phrase you might hear about that is "if the program works perfect with perfect input, then you're only half done".

Here's a real world example. At my last job I had a bug to deal with which seemed to be causing confusion between payment processors. At the time there was both paypal and a credit card available for payment methods, and it looked like a few people had managed to find a way to use both paypal and a credit card to place an order. It wasn't split payments where a portion was being placed on each. It was just a weird, bad order that the system choked on, and then caused the customer service folks headaches having to clean it up. It was doubly weird because the paypal entry form wasn't even part of the application its self. That was external, with service calls going back and forth between the two(hint: never really been a fan of paypal since then...). It turned out to be confined to cases where a credit card payment was declined for some reason and the user returned back to the payment page. The problem was that if they then tried to pay through paypal the CC fields were still populated with whatever garbage had been there previously. We cleared the other method any time they switched from one method to the other, and the bug was dead.

How long did that take to discover the solution? Did it turned out to be so simple that you guys were smacking your heads for taking so long to figure out? I have found that the simplest solutions tend to be the hardest to find.

Re: GCC errors with compiling C code.

Posted: Wed Nov 08, 2017 10:57 pm
by Redocbew
Yeah that's often true. It seemed to take a while. A few weeks, I think. Of course, discovering a bug like that doesn't suspend everything else that's going on at the time, and especially if you've got an app with bad behavior a bug like that which happens intermittently is likely to be prioritized below another which might be more visible. It might take some time to even be considered a bug, and not attributed to random bonehead users.

Anyway, back to C. :P

Re: GCC errors with compiling C code.

Posted: Wed Nov 08, 2017 11:14 pm
by just brew it!
whm1974 wrote:
How long did that take to discover the solution? Did it turned out to be so simple that you guys were smacking your heads for taking so long to figure out? I have found that the simplest solutions tend to be the hardest to find.

I would say that 95% of bugs are obvious in hindsight -- so-called "stupid bugs" that make you go "D'oh!" when you figure them out.

The other 5% can be real brainf*cks though. Subtle interactions between different modules, race conditions, random misbehavior/crashes that are really hard to reproduce, unexpected behavior from the OS due to the application doing something strange, that sort of thing. Chasing that 5% can end up eating the majority of your development time in a complex system.

To give an example that I'm involved with currently, there's one bug at work that has been a real bitch to chase down. It doesn't cause a crash or data loss, so it hasn't been top priority; but there have been a half dozen people (give or take) looking at this off and on, literally for months. Under certain usage scenarios, we were seeing a performance degradation we could not explain. We're finally close to an explanation and a potential fix; it appears to be a weird interaction between a particular pattern of system calls we're executing on one of our code paths, and a bug (or at best, a feature with a rather non-optimal implementation) in the Linux ext4 file system. This results in rapid and excessive fragmentation of the free space on a disk drive, which in turn results in a performance hit once none of the free space is contiguous any more.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 4:56 am
by Pancake
whm1974 wrote:
just brew it! wrote:
In the real world, the process we just went through with that simple program would've been called a "code review". Code reviews are part of the process in well-run development groups. In a nutshell, you get one or more developers other than the original author of the code to analyze it, looking for potential bugs.

I can imagine that it is nearly impossible for one person to write code for highly complex and large program without being really buggy.

It's the opposite, actually. With good software design and development practices it's entirely possible for one person to develop a highly complex and large program. One great programmer is often better than 10 average to good programmers. It's very difficult getting a team of people to communicate clearly especially if they're not all super-good highly disciplined devs with the confidence to communicate their what they are doing clearly instead of trying to make stuff just work and hide horrors from others who are also too beaten down from working with and managed by idiots.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 7:18 am
by just brew it!
Pancake wrote:
I can imagine that it is nearly impossible for one person to write code for highly complex and large program without being really buggy.

It's the opposite, actually. With good software design and development practices it's entirely possible for one person to develop a highly complex and large program. One great programmer is often better than 10 average to good programmers. It's very difficult getting a team of people to communicate clearly especially if they're not all super-good highly disciplined devs with the confidence to communicate their what they are doing clearly instead of trying to make stuff just work and hide horrors from others who are also too beaten down from working with and managed by idiots.

Yeah, quite true. I suppose whm's statement should be reworded "...it can be nearly impossible...", to reflect the fact that a well organized, highly competent developer can accomplish a lot. There will always be bugs, of course... but if things are properly planned and the architecture is well thought out, there should be no major design flaws.

Look at Linus Torvalds, for example. He singlehandedly created the Linux kernel; there are a lot more people working on it now, of course, but at the start it was a solo effort. He also wrote the core of the git version control system in less than a week, in a fit of pique over the fact that BitKeeper was no longer free to use and none of the other extant free version control systems met his needs.

I have worked on small teams which have accomplished amazing things, because everyone was very good at what they did and was willing to wear a lot of different hats. I've also worked on large teams that produced absolute garbage because they were poorly organized, with some people who didn't know WTF they were doing. If you don't have code reviews, one clueless developer can drag an entire project down -- every hour spent by the bad dev collectively results in more than one man-hour spent by the rest of the team dealing with the resulting bugs (i.e. negative productivity).

@whm1974 - Check out a book called The Mythical Man-Month sometime. All developers and (especially) managers of software projects should read it. The central tenet is "adding manpower to a late software project makes it later".

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 9:03 am
by whm1974
just brew it! wrote:
Pancake wrote:
I can imagine that it is nearly impossible for one person to write code for highly complex and large program without being really buggy.

It's the opposite, actually. With good software design and development practices it's entirely possible for one person to develop a highly complex and large program. One great programmer is often better than 10 average to good programmers. It's very difficult getting a team of people to communicate clearly especially if they're not all super-good highly disciplined devs with the confidence to communicate their what they are doing clearly instead of trying to make stuff just work and hide horrors from others who are also too beaten down from working with and managed by idiots.

Yeah, quite true. I suppose whm's statement should be reworded "...it can be nearly impossible...", to reflect the fact that a well organized, highly competent developer can accomplish a lot. There will always be bugs, of course... but if things are properly planned and the architecture is well thought out, there should be no major design flaws.

Look at Linus Torvalds, for example. He singlehandedly created the Linux kernel; there are a lot more people working on it now, of course, but at the start it was a solo effort. He also wrote the core of the git version control system in less than a week, in a fit of pique over the fact that BitKeeper was no longer free to use and none of the other extant free version control systems met his needs.

I have worked on small teams which have accomplished amazing things, because everyone was very good at what they did and was willing to wear a lot of different hats. I've also worked on large teams that produced absolute garbage because they were poorly organized, with some people who didn't know WTF they were doing. If you don't have code reviews, one clueless developer can drag an entire project down -- every hour spent by the bad dev collectively results in more than one man-hour spent by the rest of the team dealing with the resulting bugs (i.e. negative productivity).

@whm1974 - Check out a book called The Mythical Man-Month sometime. All developers and (especially) managers of software projects should read it. The central tenet is "adding manpower to a late software project makes it later".

Sorry I have been more clear, as I meant that without code review it it nearly impossible to write large and complex programs with out bugs. One should have someone check the code every so often.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 9:45 am
by notfred
It's also really surprising that no matter how many hours you have spent staring at a bit of code trying to work out where the bug is, if you start explaining it to someone else the bug often becomes obvious.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 10:14 am
by whm1974
While it really sucks sometimes to be on disability, at least I have plenty of free time and don't have work issues to deal with as I did in the past. It is also looking like I'm more able to stay focused and keep going no matter what then when I was younger.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 11:51 am
by Buub
I haven't ready through the entire thread to see if this is recommended. Consider it a second opinion if it has.

First, subscribe to OReilly Safari online, where you can read any development book "free" after paying the monthly subscription: https://www.safaribooksonline.com/

Then go start digging into some good, classic C and/or C++ development books. Since I don't do C, and am purely a C++ programmer, I don't any pure C books to recommend.

For learning C++, you can't really get any better than these two:
http://www.stroustrup.com/programming.html
http://www.stroustrup.com/4th.html

Both are available with the Safari subscription, above.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 11:57 am
by whm1974
Buub wrote:
I haven't ready through the entire thread to see if this is recommended. Consider it a second opinion if it has.

First, subscribe to OReilly Safari online, where you can read any development book "free" after paying the monthly subscription: https://www.safaribooksonline.com/

Then go start digging into some good, classic C and/or C++ development books. Since I don't do C, and am purely a C++ programmer, I don't any pure C books to recommend.

For learning C++, you can't really get any better than these two:
http://www.stroustrup.com/programming.html
http://www.stroustrup.com/4th.html

Both are available with the Safari subscription, above.

Thanks for the advice, however money is kind of tight right now so I'm trying to limit my cash outflow at the moment.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 12:20 pm
by just brew it!
I think Stroustrup can be a little intimidating to beginners. At least the books of his that I've read seemed that way. Good references though.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 12:30 pm
by whm1974
just brew it! wrote:
I think Stroustrup can be a little intimidating to beginners. At least the books of his that I've read seemed that way. Good references though.

Yeah I need manual that starts with the basics assumes that the beginner knows nothing about programming or how to compile source code.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:26 pm
by whm1974
OK I decided to write my own Hello, World code to see if I'm actually understanding the material and not simply just typing in example code. So here it is:
#include <stdio.h>

Main()
{
        printf("Hello, World!\n");
}
 

Note, I type this in to avoid copy and paste issues.
When compiled I get these errors but it still compiles and runs.
gcc myhello.c
myhello.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^~~~

Still works but I'm wondering what the errors mean.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:29 pm
by Redocbew
That's the compiler complaining about the function just being declared as "main" instead of "int main". It'll also complain about the lack of a return statement at the end of the function. Since it's defined as an integer function, the compiler is expecting a return value of an integer. Convention is that in the absence of any errors, main will return 0.

Edit: strike that, reverse it.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:31 pm
by DancinJack
You haven't applied a data type or return value for your main function. You need "int" in front of Main, and a return statement before the closing bracket, such as "return 0;"

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:35 pm
by whm1974
Redocbew wrote:
That's the compiler complaining about the lack of a return statement at the end of the function. Since it's defined as an integer function, the compiler is expecting a return value of an integer. Convention is that in the absence of any errors, main will return 0. I'm surprised it's also not complaining about the function just being declared as "main" instead of "int main", but I guess in the absence of any declared type the compiler must be assuming that int is the intended type.

Yeah I was trying to write the simplest code that will compile and execute. Like I said I wanted to make sure I'm understanding what I'm doing. I have the tendency to read fast anyway, and sometimes I have to read over a bit.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:37 pm
by whm1974
DancinJack wrote:
You haven't applied a data type or return value for your main function. You need "int" in front of Main, and a return statement before the closing bracket, such as "return 0;"

But it still compiles and runs.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:39 pm
by Redocbew
Yeah, because that message is a warning, not an error. The difference between the two is murky in some languages, but generally speaking warnings are the compilers way of saying "hey, are you sure you meant to do this?" while errors are "something's definitely broken".

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 1:52 pm
by whm1974
Redocbew wrote:
Yeah, because that message is a warning, not an error. The difference between the two is murky in some languages, but generally speaking warnings are the compilers way of saying "hey, are you sure you meant to do this?" while errors are "something's definitely broken".

Ahh I should read more carefully then.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 2:07 pm
by just brew it!
whm1974 wrote:
DancinJack wrote:
You haven't applied a data type or return value for your main function. You need "int" in front of Main, and a return statement before the closing bracket, such as "return 0;"

But it still compiles and runs.

The compiler is telling you that what you did is bad form and/or may result in unexpected behavior depending on context. That's what compiler warnings are for. "I think I understand what you're trying to do, and I'm going to compile this anyway, but you're doing it wrong", pretty much.

In a lot of cases where warnings are issued (like this one), it would've been considered perfectly normal back in the original (pre-ANSI) C days, and likely would not have resulted in a warning from compilers of the day.

Many commercial development organizations insist that all code compile warning-free in their coding standards. Some also mandate that the -Wall compiler switch be used; this causes the compiler to be extremely pedantic about warnings. I happen to agree with both of these practices. With few (if any) exceptions, if you can't code it in a way that doesn't result in warnings, "you're doing it wrong".

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 2:11 pm
by whm1974
just brew it! wrote:
whm1974 wrote:
DancinJack wrote:
You haven't applied a data type or return value for your main function. You need "int" in front of Main, and a return statement before the closing bracket, such as "return 0;"

But it still compiles and runs.

The compiler is telling you that what you did is bad form and/or may result in unexpected behavior depending on context. That's what compiler warnings are for. "I think I understand what you're trying to do, and I'm going to compile this anyway, but you're doing it wrong", pretty much.

In a lot of cases where warnings are issued (like this one), it would've been considered perfectly normal back in the original (pre-ANSI) C days, and likely would not have resulted in a warning from compilers of the day.

I wasn't sure if I was understanding the material correctly and not just typing in example code.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 2:19 pm
by whm1974
just brew it! wrote:
Many commercial development organizations insist that all code compile warning-free in their coding standards. Some also mandate that the -Wall compiler switch be used; this causes the compiler to be extremely pedantic about warnings. I happen to agree with both of these practices. With few (if any) exceptions, if you can't code it in a way that doesn't result in warnings, "you're doing it wrong".

Ah that is more clear now. So maybe I should learn what is the "standard practices" are of commercial development? While chances are that I will just be writing programs for my amusement and needs, if other people find them useful as well that would be great.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 2:21 pm
by Redocbew
just brew it! wrote:
Many commercial development organizations insist that all code compile warning-free in their coding standards. Some also mandate that the -Wall compiler switch be used; this causes the compiler to be extremely pedantic about warnings. I happen to agree with both of these practices. With few (if any) exceptions, if you can't code it in a way that doesn't result in warnings, "you're doing it wrong".


That was always the way I treated C, even though I've never had a job writing C code. It pays to do the same with PHP and other interpreted languages as well. Otherwise, it just leads to a ton of useless junk in your application logs which you have to sift through in order to find the stuff that's actually important.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 3:13 pm
by just brew it!
Redocbew wrote:
just brew it! wrote:
Many commercial development organizations insist that all code compile warning-free in their coding standards. Some also mandate that the -Wall compiler switch be used; this causes the compiler to be extremely pedantic about warnings. I happen to agree with both of these practices. With few (if any) exceptions, if you can't code it in a way that doesn't result in warnings, "you're doing it wrong".

That was always the way I treated C, even though I've never had a job writing C code. It pays to do the same with PHP and other interpreted languages as well. Otherwise, it just leads to a ton of useless junk in your application logs which you have to sift through in order to find the stuff that's actually important.

No kidding. I am dealing with a project like that right now. The log is a disorganized jumble of spewage, including full stack traces from random Python background threads which die by throwing uncaught exceptions in some deeply nested library. And this is considered "normal".

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 3:27 pm
by Buub
whm1974 wrote:
Thanks for the advice, however money is kind of tight right now so I'm trying to limit my cash outflow at the moment.

I understand, and urge you to spend as little as necessary. But that's one of the reasons I recommended this option -- it's cheaper than going out and buying books, and they have a relatively inexpensive plan that still allows you to have like ten books on your bookshelf at a time.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 3:28 pm
by Buub
whm1974 wrote:
just brew it! wrote:
I think Stroustrup can be a little intimidating to beginners. At least the books of his that I've read seemed that way. Good references though.

Yeah I need manual that starts with the basics assumes that the beginner knows nothing about programming or how to compile source code.

That pretty accurately describes his "Programming -- Principals and Practice" book.

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 3:34 pm
by whm1974
Buub wrote:
whm1974 wrote:
Thanks for the advice, however money is kind of tight right now so I'm trying to limit my cash outflow at the moment.

I understand, and urge you to spend as little as necessary. But that's one of the reasons I recommended this option -- it's cheaper than going out and buying books, and they have a relatively inexpensive plan that still allows you to have like ten books on your bookshelf at a time.

OK I see then. Thanks I'll look into it. I've seen the prices of college level C programming textbooks before, and they are sure not cheap. :o

Re: GCC errors with compiling C code.

Posted: Thu Nov 09, 2017 4:31 pm
by DancinJack
Yeah, I can't find an online reference right now, but my first semester as a freshman in college required a C++ object orientated programming book that was probably ~1K pages and it was over 100 bucks.

College!