Page 178 of 251

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 10:27 am UTC
by phlip
... Huh. And here I thought M_PI was a standard part of <math.h>, but looking at the C99 spec, it's not in there. I guess it's just a common nonstandard addition.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 10:36 am UTC
by Jplus
Yes. Sometimes you may need to define _USE_MATH_DEFINES before including <cmath>.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 12:38 pm UTC
by PM 2Ring
EvanED wrote:I do pretty strongly think that *(x + y) notation instead of x[y] is rarely appropriate.

What he said.
Fancy wrote:
Yakk wrote:Write code better than your project deserves, so you become ready for a better project. :)

You saying this makes me feel bad, but if I change now all of code will have to be changed for consistency.

We're not trying to make you feel bad, we just want you to develop good coding habits now, while you're still fresh to the art of computer programming, both for your own sake, and for the sake of other people who read your code. Trying to break bad coding habits later down the track can be a lot harder.


Fancy wrote:A fleeting thought addendum
Trying to learn C is really frustrating me and making me feel like an absolute idiot, and I don't want to clog up these forums with my requests for help, but I have no where else to go...

I mean, I feel like an absolute imbecile for this. This is such basic stuff, and yet it doesn't work. The book teaches us with these nice, slow examples that make sense, and then BAM the end of chapter problems are virtually impossible.


Don't feel like an imbecile, what you're going through is pretty normal. Some of us here have been programming for decades, but we remember the confusion & frustration of our early days. And we get a "friendly" reminder of that whenever we embark on learning a new language. :) Don't be afraid to ask for help here, or of asking "dumb" questions: we're here to help.

Sure, part of the reason we hang around here is that we might pick up some neat programming trick, or read an amusing programming-related anecdote, but a big motivation is that we like to share what we know, and we enjoy giving others the benefit of our skills and experience. It's very rewarding to see new programmers gradually begin to understand what programming is all about and to see them break through the confusion & frustration and start turning out succinct, efficient code. But don't expect to become a top-flight programmer overnight. Like any worthwhile skill it takes time to develop.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 1:30 pm UTC
by Xenomortis
Diadem wrote:Some heroic previous developer apparently figured out that pi is equal to 4.0 * atan(1.0). This happy fact of course had to be shared with the world.

Yeah, that's annoying.
The corrrect way is obviously pi = atan2(-1, 0).

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 1:52 pm UTC
by Fancy
PM 2Ring wrote:
EvanED wrote:I do pretty strongly think that *(x + y) notation instead of x[y] is rarely appropriate.

What he said.
Fancy wrote:

We're not trying to make you feel bad, we just want you to develop good coding habits now, while you're still fresh to the art of computer programming, both for your own sake, and for the sake of other people who read your code. Trying to break bad coding habits later down the track can be a lot harder.


Fancy wrote:A fleeting thought addendum
Trying to learn C is really frustrating me and making me feel like an absolute idiot, and I don't want to clog up these forums with my requests for help, but I have no where else to go...


Sure, part of the reason we hang around here is that we might pick up some neat programming trick, or read an amusing programming-related anecdote, but a big motivation is that we like to share what we know, and we enjoy giving others the benefit of our skills and experience. It's very rewarding to see new programmers gradually begin to understand what programming is all about and to see them break through the confusion & frustration and start turning out succinct, efficient code. But don't expect to become a top-flight programmer overnight. Like any worthwhile skill it takes time to develop.
[/quote]
I'm demotivateed by others sometimes. You see these news stories about people who are age 12 and rocking it out with the programming.
Fun fact:Some of the Arch Linux devs were born in the early nineties. That points them in their early twenties, I'm 17. These guys write arch and I'm just sitting around confused over how some of the most basic things work.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 2:09 pm UTC
by Sizik
Xenomortis wrote:
Fancy wrote:"Write a function that finds the first word in an input line, ingoring all preceding whitespace, and have it store it in an array."
What the hell is this?

I can guarantee that if I tried to implement this in C, I'd fuck up in some way and get some mysterious error.
I can do it in Python trivially (although I had to look up how to read from stdin).

Edit:
Yeah, I corrupted something and my code printed some random ASCII character...


Took a stab at it.

Spoiler:
Only thing that didn't work first try was assigning the output of realloc to a char array.

Code: Select all

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    // char buffer
    //char arr[8];
    char* arr = (char*) malloc(* sizeof(char));
    int len = 8;
    char c;
    
    
// skip whitespace
    for(= getchar(); isspace(c); c = getchar()){}
    
    
// first non-ws char.
    arr[0] = c;
    int i = 1;
    
    
// get chars until first ws char.
    for(= getchar(); !isspace(c); c = getchar(), i++){
        // resize array if needed
        if(>= len)
        {
            arr = (char*) realloc(arr, (len + 8) * sizeof(char));
            len += 8;            
        
}
        arr[i] = c;
    }
    
    
// terminate string
    arr[i] = '\0';
    printf("%s", arr);
    return 0;    
} 

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 2:25 pm UTC
by Xenomortis
Fancy wrote:I'm demotivateed by others sometimes. You see these news stories about people who are age 12 and rocking it out with the programming.
Fun fact:Some of the Arch Linux devs were born in the early nineties. That points them in their early twenties, I'm 17. These guys write arch and I'm just sitting around confused over how some of the most basic things work.

Those 12 year olds probably have some guidance and almost certainly aren't writing in C.

Look, programming is hard.
And C is a hard language; unless you're practiced or have incredible patience, even the most basic of tasks can be intensely frustrating - it does very little to help you, and without some know how, it can be infuriating to debug.

Those 20-odd year old Arch Linux devs?
These are people that have been programming for years; they love it, it's their life.
You're 17 and I'm guessing you've not been programming for long - you cannot compete with those guys yet.

But hey, you're 17 now - if you enjoy this and keep at it (and you should only continue if you do enjoy programming), by the time you're my age (23), you'd have been programming for 6 years!
For one thing; that's three times the amount of programming experience I have, and if it's in a language like C, you could find yourself working on something like Arch.

Above all, do not let others rob you of any feelings of success or joy when you solve something, even if it's trivial.
Everyone makes mistakes, or gets stuck on something that "feels obvious", and we all feel satisfaction when we finally solve it.
Just because someone spots the solution instantly when you recount the tale; do not feel bad for getting stuck.



Sizik wrote:Took a stab at it.

Spoiler:
Only thing that didn't work first try was assigning the output of realloc to a char array.

Code: Select all

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    // char buffer
    //char arr[8];
    char* arr = (char*) malloc(* sizeof(char));
    int len = 8;
    char c;
    
    
// skip whitespace
    for(= getchar(); isspace(c); c = getchar()){}
    
    
// first non-ws char.
    arr[0] = c;
    int i = 1;
    
    
// get chars until first ws char.
    for(= getchar(); !isspace(c); c = getchar(), i++){
        // resize array if needed
        if(>= len)
        {
            arr = (char*) realloc(arr, (len + 8) * sizeof(char));
            len += 8;            
        
}
        arr[i] = c;
    }
    
    
// terminate string
    arr[i] = '\0';
    printf("%s", arr);
    return 0;    
}  


Spoiler:

Code: Select all

#include <stdio.h>

void getFirstWord( int length, char* in, char* out )
{
   int i = 0;
   while ( i++ < length && *in && *in == ' ' ) in++;
   while ( (*in) != ' ' && *in ) {
      *out++ = *in++;
   }
   return;
}

int main()
{
   char in[128];
   char out[128];
   printf( "Enter input string: " );
   gets( in );
   getFirstWord( 128, in, out );
   printf( "%s\n", out );
   return 0;
}
 

The mistake I made earlier?
Used scanf( "%s", in ); instead of gets( in );
Seriously, fuck C.
Ok, I made a pointer error in getFirstWord, but I realised it shortly after compiling (incremented "in" inside the test, cutting off the first letter of the word to be retrieved).

Spoiler:
Yes, I use gets() to fill a stack allocated buffer, yes I'm an evil person.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 2:34 pm UTC
by Diadem
Xenomortis wrote:
Diadem wrote:Some heroic previous developer apparently figured out that pi is equal to 4.0 * atan(1.0). This happy fact of course had to be shared with the world.

Yeah, that's annoying.
The corrrect way is obviously pi = atan2(-1, 0).

Assuming you mean 4*atan2(-1,0), then don't worry, that one occurs as well. And we shouldn't forget 4*atan2(1,1).

And did I mention the endless variants of pi? There is two_pi and twopi and Twopi and TwoPi and Two_pi. I'm not kidding, all of those happen, some several times. Then there are there really weird ones where pi2 is defined as pi*pi in one file and 2*pi in another. Of course for pi*pi there are 3 other constants as well (pi_pi, pi_sqr and pi_2).

There are literally (and I mean literally literally, not figuratively) hundreds of lines of codes dedicated to defining pi or variants of pi. It's impressive.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 2:41 pm UTC
by Xenomortis
Actually I meant atan2(0, -1).
It's "preferable" because you don't need to multiply by 4.

atan2(-1, 0) = - pi / 2
Hey, that could be a useful #define for your team!

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 2:45 pm UTC
by Thesh

Code: Select all

#ifndef _ASTROPHYSICS_H
#define _ASTROPHYSICS_H

//snip

#define PI 1

//snip

#endif

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 4:35 pm UTC
by Fancy
Xenomortis wrote:
Fancy wrote:I'm demotivateed by others sometimes.

Those 12 year olds probably have some guidance and almost certainly aren't writing in C.

Look, programming is hard.
And C is a hard language; unless you're practiced or have incredible patience, even the most basic of tasks can be intensely frustrating - it does very little to help you, and without some know how, it can be infuriating to debug.

Those 20-odd year old Arch Linux devs?
These are people that have been programming for years; they love it, it's their life.
You're 17 and I'm guessing you've not been programming for long - you cannot compete with those guys yet.

But hey, you're 17 now - if you enjoy this and keep at it (and you should only continue if you do enjoy programming), by the time you're my age (23), you'd have been programming for 6 years!
For one thing; that's three times the amount of programming experience I have, and if it's in a language like C, you could find yourself working on something like Arch.

Above all, do not let others rob you of any feelings of success or joy when you solve something, even if it's trivial.
Everyone makes mistakes, or gets stuck on something that "feels obvious", and we all feel satisfaction when we finally solve it.
Just because someone spots the solution instantly when you recount the tale; do not feel bad for getting stuck.


Another huge problem I find is I don't know where to start. I can't think of any cool porjects to do, and every time I start a book, I hop to another book halfway through. I've tried K&R, Learn C The Hard Way, C: How to Progrem, C Programming: A Modern Approach...the list is infinite. I've seen some bad reviews for many of these, too. "K&R is outdate" "LCTHW isn't teaching right" along with the accompanying "K&R is god" "LCTHW is excellent"

Right now I'm starting Ivor Horton's "Beginning C" because it looks good.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 4:40 pm UTC
by ahammel
Is there a reason you want to learn C in particular?

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 4:54 pm UTC
by Fancy
ahammel wrote:Is there a reason you want to learn C in particular?

I want to know the lower-level things, but they are more challenging than say just learning Python. I did start a little Python a while ago and it wasn't bad, along with taking a couple of silly classes last year that covered the most basic of the basics.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 4:58 pm UTC
by Thesh
Is there any open source software you use? Preferably not something too complex. Figure out a feature you want to add to it, and try to do it. Go to the bug reports and see if you can figure out how to fix bugs. Reading is all well and good, but at some point you just need to dive into it. Just don't get frustrated because you can't figure it out, just move on to something a bit easier and come back to it.

Personally, I wouldn't be too concerned about low level until you are comfortable with the high level.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:00 pm UTC
by Fancy
Thesh wrote:Is there any open source software you use? Preferably not something too complex. Figure out a feature you want to add to it, and try to do it. Go to the bug reports and see if you can figure out how to fix bugs. Reading is all well and good, but at some point you just need to dive into it. Just don't get frustrated because you can't figure it out, just move on to something a bit easier and come back to it.

Personallyz I wouldn't be too concerned about low level until you are comfortable with the high level.

I'm a Linux user, but don't really know of any open source things I could help with.

I feel that I am comfortable, to an extent, with the high level, but it bores me. I think the class I took in high school, a very basic primer using Visual Basic, may have turned me away from some of the higher level things. I absolutely hated VB.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:02 pm UTC
by Thesh
You don't even have to cobtribute; just pick something and see if you can figure it out.

Also, I suppose I should mention https://projecteuler.net/

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:08 pm UTC
by Fancy
Thesh wrote:You don't even have to cobtribute; just pick something and see if you can figure it out.

Also, I suppose I should mention https://projecteuler.net/

I've considered PE, but am terrible at math and 1) it seems to be very mathy 2) it seems to improve math skills more than it does programming.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:11 pm UTC
by ahammel
Implement your favourite data structures?

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:19 pm UTC
by Xenomortis
Fancy wrote:every time I start a book, I hop to another book halfway through. I've tried K&R, Learn C The Hard Way, C: How to Progrem, C Programming: A Modern Approach...the list is infinite. I've seen some bad reviews for many of these, too. "K&R is outdate" "LCTHW isn't teaching right" along with the accompanying "K&R is god" "LCTHW is excellent"

Right now I'm starting Ivor Horton's "Beginning C" because it looks good.

Books don't seem to be helping you.
I have "read"* precisely two "learning to program" books.

(Spoiler relates to my personal experience - not exactly relevant)
Spoiler:
The first was a Pascal book (can't remember the name) we followed in my AS Computing class**. It wasn't used for long; it basically covered the very basics (primitive types, arrays, loop constructs, etc). That class was basically "here's how to do very basic programming, here's how to make a GUI using Delphi (and Borland's IDE, whatever it was called), now write a program to do something" - the coding part of it lasted about 4 months and I didn't continue into the second year.

The second was a VB.NET book I briefly followed when I started my first job (21, straight out of University, with no programming experience since that Computing class at 16/17).
This was a bigger book, but I didn't follow it closely (I remembered the basics). This did cover the basics of Object Oriented Programming, but I didn't read that bit much, because I didn't get the point of OOP (book examples are always so contrived that OO struck me as overly complicated). Of course my first project at that job was a mess - still didn't get the point of OOP and so arrays of things flew everywhere. It was ok; nobody seemed to care what I did for the first 6 months there.
That VB.NET book was still used for a bit though, as reference, just not for progression.
**(AS Level is the first year of A Levels; an academic qualification taken at 16-18)

*Maybe I got half way through one of them.


FancyHat wrote:Another huge problem I find is I don't know where to start. I can't think of any cool porjects to do

This is a problem I share.
There have been a few things that have helped me, Project Euler being a big one (shame the functionality of that's been reduced for the moment...), there's the Red Spider Project on these forums, which are basically a collection of cute programs / scripts, that are bundled together - my first real C++ effort went towards that (and I swear I'll finish it someday!).
There's also Programming Wars on these forums (viewtopic.php?f=14&t=103385 and viewtopic.php?f=14&t=107922) - kinda on a break because I'm supposed to be hosting the next round and motivation and me go together like vampires and werewolves; there's no known reason why they can't get along, it just never seems to work out.

I will be honest though; the only reason I program as a hobby was because the initial barriers were removed by getting a job as a developer.
The fact you seem to be actively seeking this out is promising.

Fancy wrote:I absolutely hated VB.

I think everybody hates VB.

ahammel wrote:Implement your favourite data structures?

How dull.
Program a simple game - it's what the cool kids do!
(And I do mean, really simple; terminal stuff - and let it build up from there).

Fancy wrote:I've considered PE, but am terrible at math and 1) it seems to be very mathy 2) it seems to improve math skills more than it does programming.

They are, but they're great for getting familiar with the basics of a language.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:22 pm UTC
by ahammel
Xenomortis wrote:
ahammel wrote:Implement your favourite data structures?

How dull.

Suit yourself. I like a good binomial heap :P

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:26 pm UTC
by Fancy
Xenomortis wrote:...

I don't even know what a data structure is, but I'll look into PE. Finsihed problem 1, problem 2 seems reasonably easy too.
I don't know where I would even start with a game. I don't have that sort of creativity, I'm more of an analytical problem-solver.

On seeking it out
I do it for three reasons. The first is that I enjoy it, the second is that is seems like an excellent career oppurtunity, and the third is that it produces something useful. Sometimes. Other times it just adds up all the multiples of 3 and 5 less than 1000.

FT: Is it considered bad on these forums to do what I do with omitting a large amount of quoted text? It seems like a huge waste of screen space.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:31 pm UTC
by ahammel
Fancy wrote:I don't even know what a data structure is
It's a thing you put data in. Arrays and structs are the ones C gives you for free. If you want something fancy like a hash table, you have to make it yourself (or use a library, but where's the fun in that?)

FT: Is it considered bad on these forums to do what I do with omitting a lrage amount of quotes? It seems like a huge waste of screen space.

It's fine.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:38 pm UTC
by Fancy
See, I'm horrible at this programming thing. I can't even do Project Euler problem 2. I'm trying to debug it with gdb, and that isn't going so well.

Spoiler:

Code: Select all

#include <stdio.h>
int main(void)
{
   int num = 2, prev_num = 1, sum = 0;
   while (num < 4000000) {
      if (num % 2 == 0)
         sum += num;
      prev_num = num;
      num += prev_num;
   }
   printf("%d", sum);

   return 0;
}

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:45 pm UTC
by Thesh
Everyone was horrible when they started. Figuring out where you went wrong is how you get better.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:48 pm UTC
by Fancy
Thesh wrote:Everyone was horrible when they started. Figuring out where you went wrong is how you get better.

I posted the code in my spoiler. It seems perfect, but I'm just retarded.
And then I got it immediately after my post. Very embarassing.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:55 pm UTC
by speising
Fancy wrote:See, I'm horrible at this programming thing. I can't even do Project Euler problem 2. I'm trying to debug it with gdb, and that isn't going so well.

Spoiler:

Code: Select all

#include <stdio.h>
int main(void)
{
   int num = 2, prev_num = 1, sum = 0;
   while (num < 4000000) {
      if (num % 2 == 0)
         sum += num;
      prev_num = num;
      num += prev_num;
   }
   printf("%d", sum);

   return 0;
}

after the first iteration, prev_num will be 2 and num will be ... 4.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:57 pm UTC
by EvanED
Two thoughts:

  • There's a religious war thread on this, but I'm pretty down on C for a first language for most people. "If you really want to learn C" is pretty much the exception to that, so you may be someone for whom C is right. But my position is that there are multiple aspects that are hard about programming, the hardest of which is learning how to "think computationally" -- break down a problem into intermediate goals that you can implement, think through edge cases and details, etc. And C throws too many difficulties that are, IMO, completely unrelated to that problem at you at the same time you're trying to learn the hardest thing about programming -- you have to deal with buffer overruns and stray pointers, you have to deal with manual memory management, you have to deal with an incredibly anemic standard library, and you have to deal with a language that prevents any reasonable implementation of generic containers. I suspect much better is to start with something where the language isn't set against your progress, work in that for a while, and then learn C after you have some reasonable experience programming. It should soften the learning curve. Python is the in-vogue language for this, and I don't think I have suggestions that are clearly better, so that's what I'd recommend even though I like types.
  • You might look for open class materials for classes based around the language you're interested in. The benefit of that over most books would be that you may be able to get assignments and projects and such that are designed for the right level. (This is another place where Python would help -- you'd have resources like MIT OCW at your disposal. Another weird option would be to find a class, like Berkeley's, that is in Scheme using "SICP". It should be easy to find resources for Python and for Java; it'll be harder to find a class in C that assumes no prior programming experience.)

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 5:59 pm UTC
by Fancy
EvanED wrote:Two thoughts:

  • There's a religious war thread on this, but I'm pretty down on C for a first language for most people. "If you really want to learn C" is pretty much the exception to that, so you may be someone for whom C is right. But my position is that there are multiple aspects that are hard about programming, the hardest of which is learning how to "think computationally" -- break down a problem into intermediate goals that you can implement, think through edge cases and details, etc. And C throws too many difficulties that are, IMO, completely unrelated to that problem at you at the same time you're trying to learn the hardest thing about programming -- you have to deal with buffer overruns and stray pointers, you have to deal with manual memory management, you have to deal with an incredibly anemic standard library, and you have to deal with a language that prevents any reasonable implementation of generic containers. I suspect much better is to start with something where the language isn't set against your progress, work in that for a while, and then learn C after you have some reasonable experience programming. It should soften the learning curve. Python is the in-vogue language for this, and I don't think I have suggestions that are clearly better, so that's what I'd recommend even though I like types.
  • You might look for open class materials for classes based around the language you're interested in. The benefit of that over most books would be that you may be able to get assignments and projects and such that are designed for the right level. (This is another place where Python would help -- you've got resources like MIT OCW at your disposal. Another weird option would be to find a class, like Berkeley's, that is in Scheme using "SICP".)

I don't like Python because "ha indents actually matter" and this makes it a pain to edit. I don't know of a scheme interpreter (compiler?) for Linux. I really want to learn C, because C is what is used for the things that interest me, but it's so unbelievably hard.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:01 pm UTC
by korona
Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:02 pm UTC
by ahammel
Fancy wrote: I don't know of a scheme interpreter (compiler?) for Linux

Racket, Chicken, and MIT Scheme. That's off the top of my head, I'm sure there are half a dozen others. I recommend Racket (decent module system).

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:03 pm UTC
by Fancy
korona wrote:Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

That's a whole lot of extra coding to be done though.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:07 pm UTC
by ahammel
Fancy wrote:
korona wrote:Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

That's a whole lot of extra coding to be done though.

No? Just printf some relevant variables in functions where you think there are bugs.

That's a common debugging strategy in pretty much every language.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:11 pm UTC
by Fancy
ahammel wrote:
Fancy wrote:
korona wrote:Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

That's a whole lot of extra coding to be done though.

No? Just printf some relevant variables in functions where you think there are bugs.

That's a common debugging strategy in pretty much every language.

I know that it's a relevant strategy, but then my screen won't just be filled with my failing code, now it'll be "AT LINE X VARIABLE Y HAS VALUE Z"

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:13 pm UTC
by Thesh
I'm not a fan of printf in general for debugging, unless it's a small program - it probably suits your needs here. However, you may consider using an IDE like KDevelop which doesn't have nearly as steep a learning curve as GDB.

Also, there are plenty of languages out there. Some easier to learn languages:
Java
C# if you don't mind windows (despite being from Microsoft, it's a great language, and Visual Studio is one a great toolset)
Ruby

There are also some newer langusges like Go and Rust if you don't mind being out on the fringe.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:19 pm UTC
by Dason
Fancy wrote:I don't like Python because "ha indents actually matter" and this makes it a pain to edit.


Well you should be indenting properly anyways. I understand some people are against this aspect of python but honestly if you're just starting off it might not be bad to learn some decent indentation conventions.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 6:25 pm UTC
by korona
Thesh wrote:I'm not a fan of printf in general for debugging, unless it's a small program - it probably suits your needs here. However, you may consider using an IDE like KDevelop which doesn't have nearly as steep a learning curve as GDB.

I use mostly printf debugging, even for large projects. In my experience (which is mostly low level stuff, I'm almost never involved with end-user applications) the bugs that are hard to find all happen in corner cases with heavy branching. Fixing bugs in linear code is easy. Adding a temporary "if(in corner case) printf(relevent vars)" to the code is easy - setting conditional breakpoints and complex watch expressions in debuggers is annoying and time consuming.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 7:33 pm UTC
by Ubik
Not related to the current discussion, just a fleeting thought: If V8 supported asm.js, you could run C code pretty much natively on node.js.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 8:51 pm UTC
by Fancy
Dason wrote:
Fancy wrote:I don't like Python because "ha indents actually matter" and this makes it a pain to edit.


Well you should be indenting properly anyways. I understand some people are against this aspect of python but honestly if you're just starting off it might not be bad to learn some decent indentation conventions.

I do follow a strinct indent and formatting rule, it's just not "python compliant" For my C, which is my main focus, it's linux kernel style.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 9:05 pm UTC
by ahammel
In what way is kernel style not Python compliant?

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 9:38 pm UTC
by Fancy
ahammel wrote:In what way is kernel style not Python compliant?

Tabs are 8 wide. I tried it, and it didn't work. I'll go find some example code and try again.