Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 304 305 [306] 307 308 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 902604 times)

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4575 on: June 29, 2013, 03:58:34 pm »

The char* isn't an exception in the least. The reason why it's treated like a null-terminated sequence of characters is because the contract of the c string functions expect them to be like that--that's it. The difference/similarities between a pointer and an array are irrelevant here other than the fact that you can pass in a char array and the function will still work (cstring functions always call for char*, by the way).

Speaking of breaking function contracts, SethCreiyd, if you pass in a string literal where the function expects a char, you're going to get weird behavior. Why? Don't ask why!* It's bad programming. If you want a function that puts the first char in a string to the screen, write a second function that takes a string instead.

*You can still ask why--it's fun to know compiler/language nuances, but 99% of the time, you shouldn't rely on them.
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4576 on: July 01, 2013, 07:38:41 am »

Thanks very much for the help, guys.  putChar() is designed to draw pixels mapped to a single char value onto a software surface, so there's no real reason to use strings with it.  I did actually know that bit about arrays pointing to the first block of memory, that's why the first function was taking const char*.  I wasn't trying to dereference the string literal or anything, it would get passed like so:

Code: [Select]
    setForegroundColor();  // white
    putChar(20, 20, '@');
    setForegroundColor(MAGENTA);
    putChar(22, 15, 165);
    putChar(22, 13, 'Ñ');
    putChar(22, 17, "Ñii!");
    flush();

With both functions unchanged, this results in the following:

Spoiler (click to show/hide)

This appears to be working as designed; the characters are mapped like code page 437, 'Ñ' == 209 and characters[209] is ╤.  So far, so good. 

Though I still find it strange that the last line worked.  I changed both functions to report when they're called - it turns out the first function isn't called even once!  I think I'm hiding it instead of overloading it - I guess const char* and unsigned char are not different enough?  That the compiler is automatically passing the proper data (the first char of the string literal) to the second function?

Could the latter be an optimization thing?  I'm not sure how to determine what's going on here, and it does seem like some of that aforementioned weird behavior.  But I'm really liking C++, even if it occasionally boggles the mind.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4577 on: July 01, 2013, 07:46:09 am »

I have no idea why the "Ñii!" thing would even work. Good thing you don't need it when you can use single quotes too.
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4578 on: July 01, 2013, 09:11:04 am »

I read this when it was posted a few days ago.

On the one hand, C++ is fast. On the other hand, basic operations that work in just about every other language ever can lead to undefined behavior. That's a good trade-off, right?
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4579 on: July 01, 2013, 09:36:01 am »

We should all take up lisp.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4580 on: July 01, 2013, 09:43:20 am »

I read this when it was posted a few days ago.

On the one hand, C++ is fast. On the other hand, basic operations that work in just about every other language ever can lead to undefined behavior. That's a good trade-off, right?
I guess it's just performance vs. beginner friendliness.

We should all take up lisp.
And die a horrible death every time we miss a close-paren somewhere.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4581 on: July 01, 2013, 10:07:46 am »

I read this when it was posted a few days ago.

On the one hand, C++ is fast. On the other hand, basic operations that work in just about every other language ever can lead to undefined behavior. That's a good trade-off, right?

As far as I see it, if you dont need the speed, use a higher-level language, they are generally considered more productive.

If you need the speed, write the speed-critical parts in C/C++ (while trying to avoid commiting suicide :P), then use a higher-level language. You can get a good average of the best of both worlds.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4582 on: July 01, 2013, 10:14:20 am »

I tried to do that with my roguelike, but libtcod for python doesn't let you access the SDL stuff directly. Specifically, I wanted SDL's event handling because libtcod's one sucks. Thus I gave up and went back to nice ol' C++. :3

Also am I the only one who
> gets more motivated to code when something important like finals is on the horizon
> gets this really cool idea (like a simple thermodynamics simulation with OpenCL!) while in the midst of another really cool idea (Merchant AIs to make a game...).
> finds a version-control program like git+github or mercurial+github motivating

?
« Last Edit: July 01, 2013, 10:17:15 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4583 on: July 01, 2013, 10:19:18 am »

Also am I the only one who
> gets more motivated to code when something important like finals is on the horizon
> gets this really cool idea (like a simple thermodynamics simulation with OpenCL!) while in the midst of another really cool idea (Merchant AIs to make a game...).
> finds a version-control program like git+github or mercurial+github motivating

?
Absolutely not.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4584 on: July 01, 2013, 10:49:57 am »

Also am I the only one who
> gets more motivated to code when something important like finals is on the horizon
> gets this really cool idea (like a simple thermodynamics simulation with OpenCL!) while in the midst of another really cool idea (Merchant AIs to make a game...).
> finds a version-control program like git+github or mercurial+github motivating

?

I also end up writing more code on my own projects when anything is due than any other time. Its not a good habit.
The second point has been the death of many programming experements of mine. So many awesome things that could be coded.

The third... version control is boring :P
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4585 on: July 01, 2013, 10:53:21 am »

Also am I the only one who
> gets more motivated to code when something important like finals is on the horizon
> gets this really cool idea (like a simple thermodynamics simulation with OpenCL!) while in the midst of another really cool idea (Merchant AIs to make a game...).
> finds a version-control program like git+github or mercurial+github motivating

?
Absolutely not.
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))

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4586 on: July 01, 2013, 09:27:26 pm »

I read this when it was posted a few days ago.

On the one hand, C++ is fast. On the other hand, basic operations that work in just about every other language ever can lead to undefined behavior. That's a good trade-off, right?
Oh God, that link makes me want to headdesk so much. This is what happens when you introduce programming to someone via a very high level language and very little theory. Especially the person who said C was designed by people who shun correctness for speed. That's just a stupid attitude--it's very much similar to the person who asked Babbage if putting the wrong inputs into a the Difference Engine would yield the correct outputs. C/C++ wasn't designed by people who prefer speed over correctness, they were designed by people who expect others to create correct code. If you type the wrong code in assembly, you don't complain when the assembler doesn't correct it for you. Jeez

One thing to take away from this: learn the not-so-esoteric nuances of the languages you work with. Learn some theory about the organization of programming languages, and don't expect the compiler/language to do things for you--know what the compiler/language is going to do.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4587 on: July 02, 2013, 12:17:46 am »

Interestingly, that behavior seems consistent across compilers?

I used the code

Code: (C++) [Select]
#include <iostream>
#include <string>

using namespace std;

string string_to_int(int number)
{
    switch(number){   
    case 0: return string("0");
    case 1: return string("1");
    case 2: return string("2");
    case 3: return string("3");
    case 4: return string("4");
    case 5: return string("5");
    default: return string("-1");
    }
}

int main()
  {
  int counter = 0;
  string output(string(string_to_int(counter++)) + string(string_to_int(counter++)) + string(string_to_int(counter++)) + string(string_to_int(counter++)));
  cout << output;
  cin.ignore();
  }

and got the exact same output "3210" on gcc and msvc.
Fakeedit: I realized that string(string_to_int()) is totally redundant. Doesn't matter, though. xD

Meanwhile, the code

Code: (Java) [Select]
import java.lang.*;

public class TestIncrement
{
public static void main(String[] args)
{
int counter = 1;
System.out.println(""+counter++ + counter++ + counter++ + counter++ + counter++);

}
}
results in an output of 12345, and the very similar code

Code: (C#) [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
    {
    static void Main(string[] args)
        {
        int counter = 1;
        Console.WriteLine("" + counter++ + counter++ + counter++ + counter++ + counter++);
        Console.ReadLine();
        }
    }
has the same output.

Same with
Code: (Perl) [Select]
$counter = 1;
print "" . $counter++ . $counter++ . $counter++ . $counter++ . $counter++;

Sadly, that's the extent of the languages I know that have ++ :(
It's interesting that both gcc and msvc have the same output for the undefined behavior. Someone's guess that it had to do with the stack and writing simpler machine code is probably right...

Also, if you try to copy the C++ code into codepad, you'll find that the compiler generates an 'undefined behavior' warning. The only probable reason you'd see it if the option to treat warnings like errors is on, like codepad does o_O

Anyhow, that's the end of my rather simple testing. It was on a whim.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4588 on: July 02, 2013, 07:41:58 am »

I read this when it was posted a few days ago.

On the one hand, C++ is fast. On the other hand, basic operations that work in just about every other language ever can lead to undefined behavior. That's a good trade-off, right?
C/C++ wasn't designed by people who prefer speed over correctness, they were designed by people who expect others to create correct code. If you type the wrong code in assembly, you don't complain when the assembler doesn't correct it for you. Jeez
I fail to see how the differential engine anecdote is relevant (Incidentally, someone over there used the same example). There is a difference between putting in obviously bad data and data that appears correct to someone who doesn't spend all day reading about sequence points on Wikipedia.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4589 on: July 03, 2013, 03:44:01 pm »

Actually Sky, that's not due to differences in ++. You can replace counter++ with Foo() where Foo() is defined as
Code: [Select]
string Foo()
{
    static int counter = 0;
    counter = counter + 1;
    switch(counter)
   {
      case 1: return "one ";
      case 2: return "two ";
      case 3: return "three ";
   }
}
And the result will still be a countdown. Which means it has to do with order of operations for string concatenation, rather than the ++ itself. If you do the concatenation using actual integers with the following code, you get "123"
Code: [Select]
int count = 0;

int Foo(int mult)
{
count = count + 1;
return mult * count;
}

int main()
{
int s = Foo(100) + Foo(10) + Foo(1);
cout<<s;
return 0;
}
Logged
Pages: 1 ... 304 305 [306] 307 308 ... 796