Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 15 16 [17] 18 19 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95804 times)

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #240 on: July 10, 2010, 08:28:27 pm »

@ILikePie:
Blech, stop programming in C for the time being, that's my suggestion. I learned from some very experienced people recently that logging to stdout isn't always the most reliable method of error-checking. Did you know that if you try and call a non-virtual function from a faulty/broken/null reference, because of the nature of memory management in C++ no segfault will be called until any member variables are referenced? It's actually pretty cool at first, but then horrifying once you realize that you've been looking around in the wrong place for about an hour for a single infuriatingly simple bug (note: this actually happened to me yesterday).

Either that, or use a debugger. Use gdb or the built-in debugger with MSVC. It's not really a question of whether or not fopen is working. Canvas is probably failing silently. I'm not sure what you mean by width*height*3 and it's probably wrong. The proper usage is width*height*sizeof(RGB) if you want to get an array of RGB with dimensions width x height.

@winner:
Slow down there. Get familiar with inheritance. You're skipping steps along the way. And I must ask why you're not using std::list<>. This is why you shouldn't learn how to program by trying to program a game. They are incredibly complex systems that are very easy to botch completely. And their ability to actually educate is questionable at best.


EDIT:
Also, a question aimed specifically at the more experienced crowd, I'm kind of unsure of my knowledge with message-based design. It's really hard to find good (free online) reading material on this, because I assume it's something that is taught in some higher-level, but ubiquitous CS course (hence is trivial to those who understand it, but out of reach of hobbyists). From my guesswork, I presume it works something like this?

-MessageHandler class - accumulates events and passes them to listeners
 -All listeners are notified of all relevant messages?
 -Has method pushEvent() to insert messages into a queue?
 -Whenever some method pollEvents() is called, listeners are notified of queued messages?
-Listener interface - handles the received messages
 -Essentially, those large conditional branching things that handle different types of event?
 -Bad practice to only have one type of listener for each type of event? (i.e. should I have MovementListener and SpellHotkeyListener instead of just one event listener)?
-Main loop/thread
 -Calls MessageHandler.pollEvents()?
« Last Edit: July 10, 2010, 08:45:17 pm by Normandy »
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #241 on: July 10, 2010, 08:39:37 pm »

Quote
Can you elaborate, mainly about the lethal errors part, I've read a quite a few books and tutorials that use pointer arithmetic for array manipulations and some even say that a[1] can be written as *(a + 1). This also does fit its purpose here, or I at least I think it does, I intend to write this pointer into a file eventually.
If a C function is not declared prior to being used, it is assumed to take any number of int parameters and return an int value. This is one of C's many crapnesses (C++ does not contain this particular crapness, as it's really indefensibly awful, but the first versions of pre-ANSI C did not include function prototypes, so the behaviour was in a sense unavoidable).

malloc, however, does not return int. It returns void*.

C's typing system is extraordinarily weak. It allows integers to be explicitly converted to pointers, and it allows void* pointers to be silently converted to pointers of any other type. One thing it doesn't permit, however, is the silent conversion of integers to pointers: you must use a cast. C++, in contrast, does not allow void* pointers to be converted silently; it does, however, allow the value zero to be silently converted to a pointer type. This is probably marginally worse, but whatever. We're stuck with it.

If you do not include a definition for malloc, C assumes that it returns int. An attempt to assign an int to a pointer value results in a complaint from the compiler, as such a conversion cannot be made implicitly.

However, if you cast the return value of malloc to the destination type, you'll suppress that complaint. After all, the assumed int can be explicitly converted to a pointer type without any problem.

Now, why is this a problem? It's a problem because ints and void*s are not necessarily convertible. A good example is 64-bit Windows. In 64-bit Windows, ints are 32-bit; void*s are 64-bit. If malloc is not declared, its return value is assumed to be 32-bit. If you then have an explicit cast, that 32-bit return value then gets converted somehow to a pointer value. This is bad, since it means ignoring the top 32 bits of the pointer. For small programs with small amounts of memory being allocated, that may be harmless, but for programs that use large amounts of memory, things will seem to work at first (as malloc will probably return memory below the 4 GiB barrier) but then will blow up once it returns memory above the 4 GiB limit.

On the other hand, if you do not include the cast, the compiler will tell you of the error and refuse to compile. This saves you--it will tell you "hey, this is an int, it can't be used as a pointer!" and you will fix it by letting it know that "Oh, by the way, malloc returns a void*, not an int".

So, in C, you should never cast the return value of malloc. In C++, you have no choice, but the same danger doesn't exist in C++ anyway (since C++ won't let you use functions that haven't been declared). Besides, C++ programs should in general use new.

Pointer arithmetic for array operations is safe and you are right that a[1] is exactly equivalent to *(a + 1) (and in fact, is exactly equivalent to 1[a]--addition is commutative, after all). It's just that it's much less clear to the reader. a[1] says explicitly "I have allocated an array of objects and I want to access the object at offset 1". *(a + 1) says, in contrast, "I have allocated some block of memory and want to do weird manipulations of it". It's a question of expectations.
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #242 on: July 10, 2010, 08:49:59 pm »

Back again, now there is something terribly wrong with my 'canvas' function, and I was hoping if someone could help me find out what is it.
The fundamental problem is that you are trying to program by iterative guesswork. Making changes based on whatever hunch you have without any kind of understanding of what you're changing, or why.

I think C is a lousy programming language. Probably the worst that's still in wide use (there are worse languages, but fortunately they have been consigned to the dustbin of history, or at least relegated to irrelevant niches). I don't believe any beginner programmer--and you are clearly that--should even think about C, much less learn it.

Quote
So, here's the deal. I modified canvas by adding a '* 3' in the for loop like so:
sizeof(RGB) may or may not be 3. The way to do this properly is to not use 3, but to use sizeof(RGB).

Quote
Code: [Select]
RGB* canvas(int width, int height, RGB color) {
RGB *canvas; canvas = (RGB *) malloc(width * height * 3);
int t = 0;
for (t = 0; t < width * height * 3; t++) {  //right here, in bold
printf("%d ", t);
*(canvas + t) = flip_color(color);
}
return canvas;
}
Pointer arithmetic does not work like that. Assuming sizeof(RGB) is 3, you only have room for "width * height" 3-byte objects in the memory you have allocated. But you are trying to access memory that goes far beyond the end of what you have allocated.

Quote
before I did this everything would run perfectly, but now when I run a simple fopen command, every statement, command, or whatever succeeding the command is ignored. The code looks like this:
fopen is probably bitterly upset that you have corrupted your program's memory so horrifically. You are lucky that it is merely terminating early and not deleting all your files.
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #243 on: July 10, 2010, 08:50:43 pm »

for a roguelike I made I had all the creatures in a linked list (in c++).  If I wanted all kinds of different creatures in the same linked list could I give each creature a pointer of their type that points at them, and a pointer that points at the pointer in the next creature? I am at the knowledge level of realizing that an iterator is a good idea that I should have thought of.
std::list<Creature*>

Done and done.
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #244 on: July 10, 2010, 09:07:49 pm »

Quote
EDIT:
Also, a question aimed specifically at the more experienced crowd, I'm kind of unsure of my knowledge with message-based design. It's really hard to find good (free online) reading material on this, because I assume it's something that is taught in some higher-level, but ubiquitous CS course (hence is trivial to those who understand it, but out of reach of hobbyists).
I don't believe so.

Quote
From my guesswork, I presume it works something like this?
-MessageHandler class - accumulates events and passes them to listeners
 -All listeners are notified of all relevant messages?
 -Has method pushEvent() to insert messages into a queue?
 -Whenever some method pollEvents() is called, listeners are notified of queued messages?
-Listener interface - handles the received messages
 -Essentially, those large conditional branching things that handle different types of event?
 -Bad practice to only have one type of listener for each type of event? (i.e. should I have MovementListener and SpellHotkeyListener instead of just one event listener)?
-Main loop/thread
 -Calls MessageHandler.pollEvents()?
I would say it depends heavily on what exactly you would like to do with your event system (there are synchronicity and threading questions that need to be answered), and which language and platform you are using (many environments provide messaging features that you can leverage in your own systems).

Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #245 on: July 11, 2010, 04:20:48 am »

Haha, I see what I did. That is indeed fucked up.

a[1] says explicitly "I have allocated an array of objects and I want to access the object at offset 1". *(a + 1) says, in contrast, "I have allocated some block of memory and want to do weird manipulations of it". It's a question of expectations.
I use *(a + 1) because I find it cleaner and easier to read.


The fundamental problem is that you are trying to program by iterative guesswork. Making changes based on whatever hunch you have without any kind of understanding of what you're changing, or why.
Most of the time I spend programming I'm just googling random stuff and  looking for tutorials. The rest is a lot of guesswork, but that's mainly because I have no idea how to 'x' in C. I try to understand what exactly I am doing, but that's only after I get I working properly (So I can print values of stuff without getting insane numbers randomly).


I think C is a lousy programming language. Probably the worst that's still in wide use (there are worse languages, but fortunately they have been consigned to the dustbin of history, or at least relegated to irrelevant niches). I don't believe any beginner programmer--and you are clearly that--should even think about C, much less learn it.
I have used a few other languages in the past, mainly C#. Before reading what I've read about C, and trying to use it in code, I had no idea what was happening "behind the scenes". I didn't know what header files were, what object files were, what a stack is, and how it works, what the computer's memory looks, and many other little things that today I have a very basic understanding of. C may be lousy in many aspects, but I've learned a lot from using it.

Pointer arithmetic does not work like that. Assuming sizeof(RGB) is 3, you only have room for "width * height" 3-byte objects in the memory you have allocated. But you are trying to access memory that goes far beyond the end of what you have allocated.
I see, looking at the code again, I see whats wrong. The canvas function works again, but something has corrupted my colors, time to see what's wrong. I'll be sure to grab a debugger while I'm at it. Thanks for all the help.

« Last Edit: July 11, 2010, 04:36:40 am by ILikePie »
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #246 on: July 11, 2010, 07:55:22 am »

Quote
I use *(a + 1) because I find it cleaner and easier to read.
Given the extremely poor understanding of pointers demonstrated thus far, I find that completely absurd. Nobody with a background in C or C++ writes like that unless they absolutely have to. Nobody. Seriously, you're making basic errors with pointer usage and memory allocation so don't even pretend that this style somehow makes more sense.

It's no accident that languages like C++, Java, C#, and many, many others use C's array indexing syntax, rather than C's pointer arithmetic syntax, for array accesses. There are circumstances where pointer arithmetic is required, but you're not using any.

Quote
Most of the time I spend programming I'm just googling random stuff and  looking for tutorials. The rest is a lot of guesswork, but that's mainly because I have no idea how to 'x' in C. I try to understand what exactly I am doing, but that's only after I get I working properly (So I can print values of stuff without getting insane numbers randomly).
Then you are doing programming completely wrong, and will never produce anything that is worth a damn. Iterative guesswork is simply not a methodology that works.

Quote
I have used a few other languages in the past, mainly C#. Before reading what I've read about C, and trying to use it in code, I had no idea what was happening "behind the scenes". I didn't know what header files were, what object files were, what a stack is, and how it works, what the computer's memory looks, and many other little things that today I have a very basic understanding of. C may be lousy in many aspects, but I've learned a lot from using it.
Header files are not something going on "behind the scenes", so much as a totally lame implementation artefact brought about by C's anachronistic-forty-years-ago compilation model. Object files likewise. C will teach you nothing about "the stack" that C# will not.

It's possible you've "learned a lot" (though I am extremely dubious). But it's clear that you haven't learned, well, anything about C, nor is it clear why you would even want to.

Quote
I see, looking at the code again, I see whats wrong. The canvas function works again, but something has corrupted my colors, time to see what's wrong. I'll be sure to grab a debugger while I'm at it. Thanks for all the help.
Debuggers are useful, for sure. But they are no substitute for actually understanding the code.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #247 on: July 11, 2010, 08:23:30 am »

It's no accident that languages like C++, Java, C#, and many, many others use C's array indexing syntax, rather than C's pointer arithmetic syntax, for array accesses. There are circumstances where pointer arithmetic is required, but you're not using any.
If that's the case, I'll use a[1]'s instead then.

Header files are not something going on "behind the scenes", so much as a totally lame implementation artefact brought about by C's anachronistic-forty-years-ago compilation model. Object files likewise. C will teach you nothing about "the stack" that C# will not.
True, header/object files are not something running "behind the scenes", but they are on that list of things I didn't know. C won't teach you about stack-based memory, nor does it it explain how you computer stores its memory, but the fact that it is exposed and can be manipulated makes you think, "What's all this memory stuff? What's an address? Lets google it and find out", at least that's how it works for me.

Well I guess if I've learned anything from this conversation its that I should read that "The C Programming Language" book I have lying around much more thoroughly.
« Last Edit: July 11, 2010, 08:25:02 am by ILikePie »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #248 on: July 11, 2010, 12:49:59 pm »

Then you are doing programming completely wrong, and will never produce anything that is worth a damn. Iterative guesswork is simply not a methodology that works.
Of course it works! At least, if you're interested in doing anything even remotely original in your life, not if you're interested in becoming a cubicle drone. Or if you're working with anything Microsoft has produced, in which case iterative guesswork is the only method of progress.

Yes, you'll make absolute garbage code, but in the next project you'll have learned and do better. Rinse and repeat.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #249 on: July 11, 2010, 01:07:41 pm »

Then you are doing programming completely wrong, and will never produce anything that is worth a damn. Iterative guesswork is simply not a methodology that works.
Of course it works! At least, if you're interested in doing anything even remotely original in your life, not if you're interested in becoming a cubicle drone. Or if you're working with anything Microsoft has produced, in which case iterative guesswork is the only method of progress.

Yes, you'll make absolute garbage code, but in the next project you'll have learned and do better. Rinse and repeat.
Yep, in my experience it is quite useful for learning as long as you understand that all code you make is utter garbage. I'm on iteration 5 of programs using DirectX in an attempt to make something vaguely resembling a game; and its just now beginning to look like something which could possibly be interpretted as a game. My code is probably as effecient as a Redundancy Department of Redundancy and a Bureau For Oversite of Oversite Bureaus combined, but it works and I am learning quite a bit from it. Mostly what not to do. Although at this point I haven't gone off to college to get any real training in programming; at which point this method likely won't be as useful. But for those who don't plan on buying half a dozen books or going to real classes and are just hobbiests, iterative guesswork isn't too bad. Although even if you are using said method, I suggest at least stealing most of your code from tutorials and going from there.
« Last Edit: July 11, 2010, 01:09:51 pm by alway »
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #250 on: July 11, 2010, 01:18:44 pm »

Then you are doing programming completely wrong, and will never produce anything that is worth a damn. Iterative guesswork is simply not a methodology that works.
Of course it works! At least, if you're interested in doing anything even remotely original in your life, not if you're interested in becoming a cubicle drone.
Complete crap. No good programs were ever written by guesswork. They were written by people who understood what they were doing and how to do it.

Quote
Or if you're working with anything Microsoft has produced, in which case iterative guesswork is the only method of progress.
HURR DURR.

Quote
Yes, you'll make absolute garbage code, but in the next project you'll have learned and do better. Rinse and repeat.
But you won't learn and you won't do any better, because you gain no insight or understanding.

You have to stop guessing. Don't just look at the code and bleat "why isn't this working, I'll try changing stuff until it stops breaking". That's not making you a better programmer. It's not going to save you from making the same mistakes over and over.

To become better, more competent, more able to express ideas in code, you need to be able to analyze and understand what you've done. You need to demonstrate some introspection.

You need to stop guessing, and until you do, your programs will be buggy, unmaintainable, and fundamentally uninteresting.
Logged

DrPizza

  • Bay Watcher
    • View Profile
    • Ars Technica
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #251 on: July 11, 2010, 01:31:17 pm »

Quote
Yep, in my experience it is quite useful for learning as long as you understand that all code you make is utter garbage.
Iteration is often useful. Guesswork is not.

Quote
I'm on iteration 5 of programs using DirectX in an attempt to make something vaguely resembling a game; and its just now beginning to look like something which could possibly be interpretted as a game. My code is probably as effecient as a Redundancy Department of Redundancy and a Bureau For Oversite of Oversite Bureaus combined, but it works and I am learning quite a bit from it. Mostly what not to do. Although at this point I haven't gone off to college to get any real training in programming; at which point this method likely won't be as useful. But for those who don't plan on buying half a dozen books or going to real classes and are just hobbiests, iterative guesswork isn't too bad.
If you never want to progress beyond writing shit code, perhaps. You don't need to buy lots of books or go to university to learn how to read and understand source code.

Quote
Although even if you are using said method, I suggest at least stealing most of your code from tutorials and going from there.
Brilliant.

Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #252 on: July 11, 2010, 02:41:20 pm »

Steps to success:
  • Write Really Shitty Code half based on stuff blindly copied from tutorials and examples.
  • Drop project.
  • Copy Really Shitty Code for new project.
  • Research some of the parts you don't understand of your old code.
  • Fix Really Shitty Code into Mostly Shitty Code.
  • Drop project.
  • Copy Mostly Shitty Code for new project.
  • Research some of the parts you don't understand of your old code.
  • Fix Mostly Shitty Code into Shitty Code.
  • Drop project.
  • Copy Shitty Code for new project.
  • Research some of the parts you don't understand of your old code.
  • Fix Shitty Code into Poor Code.
  • Drop project.
  • Copy Poor Code for new project.
  • Research some of the parts you don't understand of your old code.
  • Fix Poor Code into Adequate Code.
  • Drop project.
  • Copy Adequate Code for new project.
  • Copy Adequate Code for new project.
  • Copy Adequate Code for new project.
  • Improve Adequate Code to Decent Code.
  • Copy Decent Code for new project.
  • Copy Decent Code for new project.
  • Copy Decent Code for new project.
  • Copy Decent Code for new project.
  • Improve Decent Code to Good Code.
  • Copy Good Code for new project.
  • Copy Good Code for new personal library.
  • Use library in new project.
  • Use library in new project.
  • Use library in new project.
  • Use library in new project.
  • Use library in new project.
  • Use library in new project.



This list is not a perfectly accurate depiction. It does not detail unrelated projects. Experienced users may skip a few iterations. Some coders may skip all but the "Research..." stages.
Logged
Eh?
Eh!

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #253 on: July 11, 2010, 05:10:36 pm »

Learning coding in a University? I tried, but it has nothing to do with learning. Really, coding is for hobbyists or losers who didn't learn a trade (that's me). Why anyone in the world would ever WANT a job in IT, even bad enough to get a degree in it, is still beyond me.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #254 on: July 11, 2010, 09:22:05 pm »

I used to know a guy, crazier than me.

I never asked.
Logged
Pages: 1 ... 15 16 [17] 18 19 ... 78