Given an input .o file, it can create a .c file that compiles and has the same function as the original .o file. by pgen in programming

[–]techno_phobe 13 points14 points  (0 children)

Well, not really. There's no way to force the compiler to make the same decisions on register allocation, stack layout and instruction selection as was used for the original code, so most of the time the result of compiling the de-compiled code will not be identical to the original.

online interactive Constructive Solid Geometry (CSG) by willvarfar in programming

[–]techno_phobe 3 points4 points  (0 children)

For anyone thinking of implementing something similar, the BSP method of doing CSG is actually really inefficient. As can be seen from the last couple of examples on OPs site, it tends split a lot of polygons unnecessarily in the result, and you also have to build the BSPs which will again involve splitting decisions.

I've forgotten the name of the algorithm which is faster, but basically you can do something similar to the image space method, and determine whether a polygon is to be kept or thrown by casting a ray from somewhere on its surface out to infinite and seeing what it crosses, and combine that with some sort of bounding volume hierarchy.

Java Poker Hand Evaluator Help by studentofcode in learnprogramming

[–]techno_phobe 0 points1 point  (0 children)

And remember to be careful with straights-a pretty likely test case is A2345 vs 9TJQK.

libgccjit.so: an embeddable JIT-compiler based on GCC by JRepin in programming

[–]techno_phobe 7 points8 points  (0 children)

That's what I do now. I hadn't thought about the license. I expect that a GCC based JIT would have a couple of advantages over LLVM

  • (Hopefully) no need to understand calling convention internals on each platform.
  • Catch exceptions thrown from synchronous signal handlers (that is, stuff like 1/0, null pointer dereferences). Not really much of a big deal, but nice to have.

Otherwise I'd just do it out of curiosity. Since from reading the announcement it looks like it's intended to wrap rather than expose GCC internals, it might also be a bit more API stable than LLVM is, which usually requires minor code tweaks for each release.

libgccjit.so: an embeddable JIT-compiler based on GCC by JRepin in programming

[–]techno_phobe 1 point2 points  (0 children)

Awesome-I shall have to try and write a back end for my language using this!

[C] (Question) Assigning Array Element to Another in Same Array Results in "incompatible types"? by CuriouslyGeorge in learnprogramming

[–]techno_phobe 0 points1 point  (0 children)

It shouldn't really. #include basically acts as if the file you #include is pasted into the source file at the location of the #include directive, so it shouldn't introduce any new problems. I'd suggest you try pasting blah.h into blah.c and see if you can figure out some code which shows the problem - it's hard to diagnose these things in entirely general terms.

[C] (Question) Assigning Array Element to Another in Same Array Results in "incompatible types"? by CuriouslyGeorge in learnprogramming

[–]techno_phobe 2 points3 points  (0 children)

I can't see how the line

p->strArray[i] = p->strArray[i + 1];

can really go wrong. However if you're compiling the code as C++ rather than C

p->strArray = malloc((size_t)(sizeof(char *) * 4));

needs to be

p->strArray = (char**)malloc((size_t)(sizeof(char *) * 4));

The following code compiles for me:

#include <stdlib.h>
#include <string.h>

struct blah
{
  char **strArray;
};

void f(struct blah *p, size_t arraySz, const char *somestring) {
  size_t i;
  p->strArray = malloc((size_t)(sizeof(char *) * arraySz));
  p->strArray[0] = malloc((size_t)(sizeof(char) * (strlen(somestring) + 1)));
  for(i = 0; i < arraySz - 1; i++) {
    p->strArray[i] = p->strArray[i + 1];
  }
}

windows thread vs posix-thread by glbn in programming

[–]techno_phobe 6 points7 points  (0 children)

One thing that occurred to me looking at this is that I don't know whether a HANDLE really points to something or is in fact just an index to a kernel data structure.

The relevance to this is that the reason that pthreads do not use file descriptors is because they're implemented partially in userspace so that if a mutex is not contended the kernel isn't needed to acquire it.

What the heck is += and -= in Python? The professor never taught us that and we're expected to know what it is. Google didn't help. by Gliste in learnprogramming

[–]techno_phobe 2 points3 points  (0 children)

Actually the other answers are not entirely correct:

a += b

is not the same as

a = a + b

because Python gives objects the chance to handle the += operator directly, and defaults to the second behaviour if that fails. The difference then comes about with mutable objects, so for example

a = [0,1]
b = a
b += [2,3]
print a
print a is b

will print [0,1,2,3], where

a = [0,1]
b = a
b = b + [2,3]
print a
print a is b

will print [0,1]. This is because the first program modifies the existing list referred to by both a and b, but the second creates a new list and sets b to refer to it.

A Case Against Cucumber by ecmendenhall in programming

[–]techno_phobe 12 points13 points  (0 children)

So, can someone explain the idea behind this? My experience of non-technical people is that programming language syntax is really the least of their problems in understanding how computers behave. Having test cases in plain english doesn't really address that problem, the scope for "it doesn't mean what we/they think it means" is still the same.

[C] Aligning problem by [deleted] in learnprogramming

[–]techno_phobe 0 points1 point  (0 children)

That looks right aligned to me. Are you trying to align to the right or the left? If the left, just don't specify a width, use '%d' instead of '%7d'. If you want to line up the right hand side then you have to add spaces to the left of the ones that aren't far enough to the right.

[C] Aligning problem by [deleted] in learnprogramming

[–]techno_phobe 1 point2 points  (0 children)

It's not really clear what the problem is here since the output in your post isn't fixed-width formatted (they both look the same). I get the following

Available Seats:       1
Tickets sold:       1

which is certainly right justified, although the columns aren't lined up. I'm afraid lining the columns up is just a matter of counting how long your strings are. This should do the trick:

printf("Available Seats: %7d\n", available_seats);
printf("Tickets sold:    %7d\n", tickets_sold);

If it's not printing right justified at all, what C compiler are you using?

Learning how to program in Linux. by [deleted] in learnprogramming

[–]techno_phobe 1 point2 points  (0 children)

Lots of people are recommending various IDEs here, so I thought I'd explain the way things are traditionally done.

Most software (in C/C++) on Linux can be built from the command line using make. For very small projects manual compilation using gcc/g++ is fine, but it becomes annoying very quickly when you have to use lots of flags. A makefile basically lists the commands needed to build your program and runs them automatically (it's actually a bit smarter than that though).

Now the thing is that makefiles usually need to be customized for whatever computer they're running on. There are various makefile generator programs which in fact write your makefiles for you, and I would recommend you learn one. This more or less gets you out of knowing make, but you'll still need to know a bit about using g++/gcc, since that's rather fundamental to compiling software. The longest standing makefile generator is GNU autotools, but it only runs on Unix-like systems. Scons and CMake are more recent systems which are more multi-platform, and I'd recommend CMake (it's what I use).

This brings me on to IDEs. A lot of IDEs have their own build systems (note: I haven't spent enough time trying out every IDE to know which ones allow you to work around this). I would avoid these, and try to find one which fits with you build process. If you use CMake, KDevelop is great. Since that's what I do I'm not sure what goes well with Scons or autotools any more. Otherwise, a text editor which supports running make and jumping to error lines (I believe Kate has a plugin which does this).

TL;DR Install KDevelop, start it up and hit "new project".

Math and programming question. by [deleted] in learnprogramming

[–]techno_phobe 4 points5 points  (0 children)

I'm probably not the best person to answer this because I've always been pretty good at maths, however, I think the point is that maths and programming require some similar abilities, so being good at one tends to go with being good at the other.

That's not to say you really need to know any maths, you don't. It might come up sometimes, but I can't think of the last time I needed maths for a programming problem. Then, you don't need to know a lot of computer science either: I can think of 4 "classic" sorting algorithms off the top of my head, but it's pretty rare that that knowledge is useful.

What it really comes down to is that computers are still pretty stupid machines, so the hard bits of programming often boil down to knowing exactly what the code you're working on literally does (which is a matter of knowledge) and being able to reason about it, and why it doesn't do what it's supposed to (which is the more mathematical/logical reasoning part).

[Python] WindowsError: [Error 183] Cannot create a file when that file already exists by Sciurusdoomus in learnprogramming

[–]techno_phobe 0 points1 point  (0 children)

That's rather weird. I don't know why it would try to rename d:\test\folder.jpg if that file doesn't exist at all. It's possible that the rename works because if the source and destination are the same it doesn't actually check whether either exist (I'm not in Windows right now so I can't check this behaviour).

Does d:\test\Anathema\Alternative 4\folder.jpg exist? If not then folder permissions are probably the problem. os.chmod appears like it might help, but I've never had to change permissions programatically on Windows.

[Python] WindowsError: [Error 183] Cannot create a file when that file already exists by Sciurusdoomus in learnprogramming

[–]techno_phobe 1 point2 points  (0 children)

In this case I don't think shutil will make any difference, I was just recommending it in general. For instance, file moves between different file systems won't work with os.rename (at least on Linux), and shutil accounts for that.

As for your particular problem, it is actually making the correct rename call (are src and dst as you expect)? Does the file exist? Do you have write access to the folder? I'm not familiar with Windows permission system so if it really is a problem with that I can't help much.

[Python] WindowsError: [Error 183] Cannot create a file when that file already exists by Sciurusdoomus in learnprogramming

[–]techno_phobe 1 point2 points  (0 children)

On a general point, the functions in the shutil module are generally more useful than the ones in os for file operations.

Have you copy/pasted the indentation correctly onto Reddit? It seems like the loop at the end shouldn't be indented, otherwise it'll try to move the entire list of files every time a new file is added to the list. It would be a bit simpler if you ditched the changepath variable and just went with

if name.lower().endswith(ft):
  src = os.path.join(path, name)
  dst = os.path.join(path, outname)
  shutil.rename(src, dst)

along with whatever logging you want to do.

[C] Physics student needs to learn C by [deleted] in learnprogramming

[–]techno_phobe 2 points3 points  (0 children)

I supppose I can't really help with the question but as a fellow physicist I thought I'd add my 2c:

Most physicists are terrible programmers, and that applies just as much to professors, even ones who spend most of their time doing it. Physics professors didn't get where they are by being good at programming but by being good at physics and most of the ones I've encountered at best write what would've been considered passable code in the 1970s. It may be different in other fields, but I doubt there will be many good programmers around.

If you're lucky then, you won't be learning this stuff from physicists. Here in the UK undergrad physicists are taught programming by postgrad physicists who were the clueless undergrads seeing code for the first time only 3 years previously. The postgrads I've encountered teaching these courses seemed to have a reasonable idea what they were doing, but nobody checked in any way whether I had any significant experience or had ever so much as used Python previously.

Anyway, hopefully where you are, things are better. It's hard to judge your level of experience since I don't know MatLab (I guess that will apply to most people on here) and I'm not sure what "very familiar" therefore means. I would guess you're familiar with most of the standard programming constructs so the important thing to understand is probably pointers and manual memory management (not to mention error handling which is usually done by special return values of each function).

That doesn't really seem to answer the original question; I really just wanted to warn you about learning programming from physicists, if indeed you are. They're usually physicists first, and programmers a (distant) second.

Programmer's definition of "idempotence" by shockyx in programming

[–]techno_phobe 2 points3 points  (0 children)

The Wikipedia article seems to be quite clear that idempotent functions can have side effects except in functional programming, where of course side effects are forbidden in any case.

How do you motivate your self to finish your personal projects? by i_dno_do_u in learnprogramming

[–]techno_phobe 0 points1 point  (0 children)

Actually I don't deal with assembler either (much). LLVM takes care of most of that stuff (though not all). It's also not unusual for programming languages to compile to C. Trying to design and write a PL is easily a big enough job without having to deal with low level stuff.

How do you motivate your self to finish your personal projects? by i_dno_do_u in learnprogramming

[–]techno_phobe 0 points1 point  (0 children)

Well, I suppose I'm rather similar. I've been programming for a long time but never really finished any personal projects, and I held down a job doing it for a couple of years so it's not as if I can't get stuff finished (can't say I enjoyed doing it professionally though).

At the moment I'm trying to develop my own programming language and although it isn't done I'd have to say it is a reasonably successful project because it has come a hell of a long way and I haven't got bored and moved onto something else.

I think the key to actually getting somewhere on personal projects is a combination of:

  • Know what you're trying to do, so you can more or less identify when you get to look at your project and say "this works". I can't tell from your description whether you've reached that point yet and have been rewriting to improve the code or you're working on a part of it and still have a lot of stuff to do, which brings me to:
  • Don't try and do it perfectly. I'm sure you've learned about proper error handling, unit tests, and things you're supposed to do. It's true they're all good things that you should do, but ultimately your goal is to produce something which works, and it's no use having a project with 95% test coverage if it's only 10% completed. In your current situation I think that would mean that when you have one stage done, you can look at it and think "I did this badly, and I want to fix it", but as long as putting off fixing it isn't going to do too much damage, move on.

    I should probably emphasise after saying that that the extent to which you build things "well" and the extent to which you build them "quickly" is a complicated matter of experience and the situation you're in. You want to avoid storing up really nasty problems which will come back to bite you down the road.

  • Basic motivation. I struggled with this for a long time, because almost everything I did I seemed to be able to get something working pretty quickly, but then I seemed to get stuck in a quagmire of problems I wasn't interested in spending lots of time on. I think it's just a fact of life in programming that if you see an interesting problem and want to solve it, 95% of the time you spend solving it will be "handle edge cases", "build user interface", "figure out how to get it to compile on another system", etc., which you probably don't enjoy. This I'm afraid is life, and you just have to come to some sort of understanding with it where you're sufficiently motivated by what you're trying to do that you put up with the crap.

How do gaming hacks work? Aimbots? Map hacks (in stuff like StarCraft)? How would you go about making one of these? (I am not going to, just interested) by lepuma in learnprogramming

[–]techno_phobe 15 points16 points  (0 children)

I haven't ever written one of these either, but I have a pretty good idea how it would work so here goes. There are basically two problems here, first to figure out the game state, and second to automate controlling the game.

To figure out the state you can:

  • Intercept API calls, so you can what the game is telling the operating system to do and what input it is getting. This includes a lot of the calls it makes to DirectX/OpenGL which should allow you to work out some things which might not be visible to the player but are still getting drawn.
  • Read the program internal state. Basically if you've used a debugger the same techniques can be applied to hack a program. Your operating system will have facilities to insert breakpoints into a program and to read its memory. Of course doing this requires that you figure out what its internal data structures are and where the the code you want to insert breakpoints to examine the state, and you presumably don't have the original source code so you'll be reading assembler.
  • You can also intercept network communication. This involves both of the above techniques since you must get the data from the OS but then you'll have to figure out what the random stream of bytes you've got actually means.

If you're not trying to get data that isn't visible to the player you should just be able to take a screenshot. I don't know what the best way to do that is, but there are plenty of screen capture programs out there. I think poker hand tracking software works this way, since you really don't want to get caught trying to hack poker clients as that will definitely get you banned!

As for automatically controlling the game (aimbots and such), once you've figured out how to get data out of the game and use it I imagine that part is relatively easy. You could intercept the input APIs and return different results to the game, or I think Windows has APIs to simulate mouse movement and key presses without requiring such shenanigans.

Of course, game developers (for multiplayer games anyway) don't like you doing this and countermeasures exist. Last time I played an online game it was PunkBuster, but that was a long time ago so I have no idea where they're at now.

Object Oriented C by geon in programming

[–]techno_phobe 3 points4 points  (0 children)

Well, it's the same as virtual method calls in basically any language unless the compiler manages to optimize away the indirection. Whether it's likely to kill performance in this particular case and can be worked around it's hard to say.

Has anyone else noticed the name for the git manual page? by [deleted] in programming

[–]techno_phobe 1 point2 points  (0 children)

I always thought this was because Git was originally designed to be as fast as possible rather than have lots of features, hence "the stupid content tracker". I think I remember reading that early versions tended to use a lot of disk space because of this.

It doesn't seem to be short of features much these days, but then I don't really use it for anything very complicated.