• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

poweld

Member
Recently bought this:

http://www.amazon.com/gp/product/098478280X/?tag=neogaf0e-20

And it's actually pretty good from what I can glean from first flip through.

Also came across this when I opened to a random page:

Find the bug in these lines of code

Code:
unsigned int i;
for i = 100; i >= 0; --i)
    printf("%d\n", i);

and I was quite surprised at myself for how fast I was able to spot the issue. Made me feel a little more confident in what I know as I start to go into interviews for jobs and such, even if it is a small bug.
Unless you consider looping forever a bug, it looks kosher to me :)

Is it the missing parentheses?
It's an unsigned int, and we enter the block while i is >= 0. This means that when i == 0, we decrement it, and since it is unsigned, we underflow the structure and wind up with the largest number an unsigned int represents (on 32 bit systems, 2^32 - 1). For this reason, the loop will continue indefinitely.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
Would it really loop infinitely? The variable is still being decremented right? Man I might be looking really dumb right now.
 

TheFatOne

Member
This isn't necessarily programming related, but I'm not sure if I should make a new thread. I'm finally transferring from a community college to a University this upcoming fall. The only problem is that I have no idea what I should specialize in. I could go for a general software engineering path or specialize in robotics,security, etc.. I'm thinking about doing there AI track since the college has a highly rated AI department, but I'm not sure if that is the best choice career wise. Anyone have any experience in something like this? Because I could use some advice.
 
Is it the missing parentheses?

No. I forgot to put it in.

Unless you consider looping forever a bug, it looks kosher to me :)

Well that unsigned i is lookin pretty shifty to me.

Would it really loop infinitely? The variable is still being decremented right? Man I might be looking really dumb right now.

It would. The loop evaluates at i = 0 and then decrements before exiting, so it'll be a negative number and blow up cause it's unsigned and never terminate cause it'll never reach the required conditions.
 

moka

Member
This isn't necessarily programming related, but I'm not sure if I should make a new thread. I'm finally transferring from a community college to a University this upcoming fall. The only problem is that I have no idea what I should specialize in. I could go for a general software engineering path or specialize in robotics,security, etc.. I'm thinking about doing there AI track since the college has a highly rated AI department, but I'm not sure if that is the best choice career wise. Anyone have any experience in something like this? Because I could use some advice.

I think my advice would be don't do something just because the University is 'highly rated' in that department. Do something that you can see yourself doing in 5 years.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
It's an unsigned int, and we enter the block while i is >= 0. This means that when i == 0, we decrement it, and since it is unsigned, we underflow the structure and wind up with the largest number an unsigned int represents (on 32 bit systems, 2^32 - 1). For this reason, the loop will continue indefinitely.

It would. The loop evaluates at i = 0 and then decrements before exiting, so it'll be a negative number and blow up cause it's unsigned and never terminate cause it'll never reach the required conditions.

Well shit. I have never actually had to declare whether a number was signed or unsigned in my (school) programs :S
 
Well shit. I have never actually had to declare whether a number was signed or unsigned in my (school) programs :S

I haven't had to much either. Your compiler would've caught it if you had tried to do it anyway. But only as a warning. [as least with gcc as I tried it]

SeL2xME.png
 

poweld

Member
Well shit. I have never actually had to declare whether a number was signed or unsigned in my (school) programs :S

It's interesting to note that because %d (which indicates a signed integer) was used in the print formatting we would see what we would "expect" on stdout, meaning we would count from 100 to 0, and then continue to -1 and so on. This is because even though the variable is designated as unsigned, the bits are still arranged in such a way that the two's complement will get us negative numbers even though in actuality we've underflowed an unsigned integer.

e.g.
Code:
int main( int argc, char **argv ) {
    unsigned int i = 10;
    for ( int j = 0; j < 20; j++ ) {
        i--;
        printf( "%d\n", i );
    }
    return 0;
}

Code:
$ g++ -o test test.cpp && ./test
9
8
7
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
 
It's interesting to note that because %d (which indicates a signed integer) was used in the print formatting we would see what we would "expect" on stdout, meaning we would count from 100 to 0, and then continue to -1 and so on. This is because even though the variable is designated as unsigned, the bits are still arranged in such a way that the two's complement will get us negative numbers even though in actuality we've underflowed an unsigned integer.

Yep. Been a while since I had to manually calculate two's complement, but it just flips the sign bit and continues decrementing the value, no?

ie

0000 0101 -> 5
0000 0100 -> 4
0000 0011 -> 3
0000 0010 -> 2
0000 0001 -> 1
0000 0000 -> 0
1111 1111 -> -1
1111 1110 -> -2
1111 1101 -> -3
1111 1100 -> -4
1111 1011 -> -5

At least that's how I remember it.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
I haven't had to much either. Your compiler would've caught it if you had tried to do it anyway. But only as a warning. [as least with gcc as I tried it]

SeL2xME.png

Visual Studio lets it go (no warnings either). I have Visual Studio configured for General Development instead of C++ though, not sure if that would make a difference or not.

Regardless, good to know for future reference, thanks everyone.
 
So after a long hiatus from native c/c++ development I've installed Visual Studio 2012 Express, but the damn thing won't even compile default project templates:

4EPjVOF.png


Any ideas?
 

squidyj

Member
So...I have my Java final project and it's been getting really busy for me here deployed...would anyone be interested in doing my final project? Of course I will pay ;]

Pms please!

haha, oh wow, without offering to do this I really want to know what this project is.



Also I have a question, that isn't strictly coding. Why am I like twice as dumb in interviews and during programming contests as I am normally? I mean what the fuck?
 

poweld

Member
Yep. Been a while since I had to manually calculate two's complement, but it just flips the sign bit and continues decrementing the value, no?

ie

0000 0101 -> 5
0000 0100 -> 4
0000 0011 -> 3
0000 0010 -> 2
0000 0001 -> 1
0000 0000 -> 0
1111 1111 -> -1
1111 1110 -> -2
1111 1101 -> -3
1111 1100 -> -4
1111 1011 -> -5

At least that's how I remember it.
Yup. The sign bit is the most significant bit, so once you decrement below 1000 0000, you will re-enter positive numbers.
 

The Technomancer

card-carrying scientician
Did you try Eclipse?

I don't have access to the computers yet, I'm just gathering recommendations at the moment. I might be teaching a workshop type thing for 12-14 year olds this summer on making simple Minecraft mods, and the computers at the place are old machines running XP. We're not sure if we can even run Minecraft on them yet :p
 
Hey guys, does anyone know of a good Java development environment that would run on old computers running Windows XP?

Eclipse and Netbeans are both resource hogs, I haven't used Netbeans as much, but Eclipse is pretty bad in that respect. I'd just fall back on some nice text editor (notepad++ for instance) and cmd.exe. It's not as convenient, but I assume they have no programming experience, so a full-blown IDE would probably confuse them more than anything, unless you abolutely need one for building the mod or something.

Yeah, Sublime Text 2 is fantastic and it's what I use currently as well (when I'm not doing anything in Visual Studio) but I was concerned about the licensing, so I didn't recommend it. (it costs ~50$ iirc)
 

Madtown_

Member
I'm not certain that it works, but perhaps Sublime Text 2? (http://www.sublimetext.com/2)

It's a fantastic editor, very light on resources.

Note: It's free to use, but popups to buy will occur on occasion. I'm not sure how much a license pertaining to you would cost.
 
Sublime Text is a great editor but when I was into Java development some 10 years ago I used Netbeans. It was way easier and friendlier than eclipse.
 
I don't have access to the computers yet, I'm just gathering recommendations at the moment. I might be teaching a workshop type thing for 12-14 year olds this summer on making simple Minecraft mods, and the computers at the place are old machines running XP. We're not sure if we can even run Minecraft on them yet :p

If you are going to focus on teaching the language, you should consider using a text editor until the kids get the basics down. There is quite a bit you can get through with just a text editor like Notepad++ or Textpad, both of which have syntax coloring and can get plugins to compile from within the editor.

I started learning Java with notepad. Once I became dangerous with that and made the jump to Eclipse, my mind was blown by all the cool stuff Eclipse could do. At my Alma Mater, the basic programming class for non-engineers used an IDE called BlueJ. I tutored a few people who were in that class and they seemed to like BlueJ.
 

The Technomancer

card-carrying scientician
Eclipse and Netbeans are both resource hogs, I haven't used Netbeans as much, but Eclipse is pretty bad in that respect. I'd just fall back on some nice text editor (notepad++ for instance) and cmd.exe. It's not as convenient, but I assume they have no programming experience, so a full-blown IDE would probably confuse them more than anything, unless you abolutely need one for building the mod or something.

Yeah, Sublime Text 2 is fantastic and it's what I use currently as well (when I'm not doing anything in Visual Studio) but I was concerned about the licensing, so I didn't recommend it. (it costs ~50$ iirc)

I guess this is what I don't know about. I'm going to spend the next couple months learning Minecraft modding, we'll see how complex the compiling is.
 

0xCA2

Member
Speaking of java, I know I will eventually be required to learn it for college so I've been doing some stuff in it to prepare myself. I can pretty much only do basic stuff at this point, as I've only been programming for a year, sporadically, but I like it so far. Why do many people seem to hate it?

note that this isn't my first language, I did python before this
 
Speaking of java...Why do many people seem to hate it?

A big complaint with Java is its verbosity.

For example, with Python hello world is

Code:
print "Hello World"

In Java, you need at the very least need a class which runs a main method which prints out 'Hello World'. If you were doing it 'properly', you should have the main class which runs a main method which creates some other class and calls its method to print out the text.
 
Speaking of java, I know I will eventually be required to learn it for college so I've been doing some stuff in it to prepare myself. I can pretty much only do basic stuff at this point, as I've only been programming for a year, sporadically, but I like it so far. Why do many people seem to hate it?

note that this isn't my first language, I did python before this

In my opinion? originally the hate was because it was the new kid on the block and all the C/C++ people obviously didn't want to switch or learn something new so it copped a lot of flack. This is more or less natural with anything and to be expected.

Related to this was primarily the argument of speed and the advantages of native code for applications like games.

As time moved on of course, it was clear that the java/c# world was the way to go for commercial applications as the interweb took off. The criticism about verbosity actually helps you in a lot of cases or is inconsequential in the context of a project. Hence those platforms became extremely popular and the de-facto choice for professionals.

Hence that fight lost, a lot of people jumped onto other languages that more or less do the same thing, either because they don't want to be on the popular bandwagon or still hold a grudge about not being able to do web projects in C.

Bottom line? There is no real reason to hate any language and they all have their strengths and weaknesses and proper use.
 
Speaking of java, I know I will eventually be required to learn it for college so I've been doing some stuff in it to prepare myself. I can pretty much only do basic stuff at this point, as I've only been programming for a year, sporadically, but I like it so far. Why do many people seem to hate it?

note that this isn't my first language, I did python before this


imo top of the list is:

1. Oracle are a terrible company.

2. the press confuses java web apps with regular applications, and then everybody runs around screaming about hijacking.

3. oracle patches exploits, people assume because java wants to update a lot "it must be a virus".

... and the legitimate issues a lot further down ...

98. speed, especially of graphics stuff.
99. It's sometimes overly verbose
100. Doesn't support tail-call optimization.
 
imo top of the list is:

1. the press confuses java web apps with regular applications, and then everybody runs around screaming about hijacking.

2. oracle patches exploits, people assume because java wants to update a lot "it must be a virus".

... and the legitimate issues a lot further down ...

97. speed, especially of graphics stuff.
98. It's sometimes overly verbose
99. Doesn't support tail-call optimization.

1. Oracle is a terrible company. Shift everything down one.
 
imo top of the list is:

1. the press confuses java web apps with regular applications, and then everybody runs around screaming about hijacking.

2. oracle patches exploits, people assume because java wants to update a lot "it must be a virus".

... and the legitimate issues a lot further down ...

97. speed, especially of graphics stuff.
98. It's sometimes overly verbose
99. Doesn't support tail-call optimization.

lwjgl and jogl solve most of the speed issues with graphics. As well, Java2D is Graphics Accelerated on Windows and certain versions of linux with certain graphics cards.

I being owned by Oracle is the only real complaint I have.
 
1. Oracle is a terrible company. Shift everything down one.

This is true enough, but people have hated Java for a long time before Oracle got their hooks in. I think the original hate just breeds more hate.

One thing I will say though is the bloody java updater that never goes away on windows systems still gets my goat.
 
Speaking of java, I know I will eventually be required to learn it for college so I've been doing some stuff in it to prepare myself. I can pretty much only do basic stuff at this point, as I've only been programming for a year, sporadically, but I like it so far. Why do many people seem to hate it?

note that this isn't my first language, I did python before this

I understand a lot of the complaints with Java, but I do hold a special place in my heart for the language. It was the first language where I did non-trivial programming, and it was the language of choice at my university. If it was up to me, Java would have made more progress than C#. Oracle sucks at taking care of Java. They should just release it to the community.
 
The JVM is a wonderful thing. Java on itself is just a bad language and most of it's environments are overbloated, overcomplicated and designed to sell you certifications and and consultant time.
 
Reasons why Java sucks without even getting into whether its brand of OO is a good thing or not or attacking design decisions of the language:

1. Java is overly verbose in a ridiculous way. The only thing that even comes close to being as character heavy is STL or templating stuff in C++.
2. The language is full of dumb legacy shit that exists entirely for backwards compatibility reasons. The Point class has a public x and y field as well as getX() and getY() methods. WTF.
3. Unneccesary restrictions on features that the language itself doesn't abide by but the programmer has to. For example: Java does not allow operator overloading yet there is built in operator overloading for Strings with +.
4. Generics are implemented via type erasure, this means that there is no way to build a method to behave differently based on type of argument if using generics, removing a large part of the reason to have them in the first place.
5. Speaking of type erasure, since generics must be objects and not primitives, auto-boxing/unboxing will fuck you hard on performance. Example:

Code:
List<Integer> list = new LinkedList<Integer>();
// fill list with tons of numbers
for (Integer i : list) {
    // do maths 
    // UH OH you got fucked by unboxing
}

6. No tail recursion elimination, even though the JVM is perfectly capable of doing it if implemented correctly.
7. No functions as parameters, which means no list comprehensions, no closures, no nothing. There aren't even function pointers so you literally can't pass a function anywhere. The closest thing you can do is define an interface which implements a foo() method and pass an empty object around to call that function. It's needlessly restrictive and stupid.
8. Despite a massive reliance on getters/setters as a core language design philosophy, there is no easy way to define an accessor/mutator with shorthand or autogenerated methods.
9. Interfaces are not a full featured replacement for multiple inheritance. Plenty of languages avoid the diamond problem with some logical inheritance priority rules, there's no reason to not have it.
10. Static methods aren't even methods, they're global functions, you can't override them in a subclass. Good OO there java.

There are lots more but those are the main complaints I could come up with on the spot. You'll notice the common theme with all of these issues is that they all make Java easier to understand for bad programmers. This is why people hate Java, it's designed to be an easy language for bad programmers to make shitty software with en masse.
 
There are lots more but those are the main complaints I could come up with on the spot. You'll notice the common theme with all of these issues is that they all make Java easier to understand for bad programmers. This is why people hate Java, it's designed to be an easy language for bad programmers to make shitty software with en masse.

I agree with most of what you say, but this summation is just a bad one. The reason Java is the way it is is because it is focused around enterprise development which is slow moving, often consists of massive teams and massive projects, and relies heavily on backwards compatibility and legacy support.

That also happens to be the exact reason why it's pretty much the best language for that sort of setting. The portability also helps. There's a reason why it has the most tools out there for that sort of development.
 

usea

Member
Reasons why Java sucks without even getting into whether its brand of OO is a good thing or not or attacking design decisions of the language:
9. Interfaces are not a full featured replacement for multiple inheritance.
10. Static methods aren't even methods, they're global functions, you can't override them in a subclass. Good OO there java.
:p

I used Java in college for a lot of stuff, including in several programming competitions. I thought I was really effective. All the non-trivial stuff I had done was either in Java or C++. Then I graduated and started using other languages. I'll never go back to Java, unless it's for a massive pay-raise. Getting anything done takes ages, although I guess that it's good for some domains.

Also, I don't have any problem with the style of interfaces in Java, though I think Go handles them much better. Screw multiple-inheritance. I almost never use inheritance of any kind anyway. Imo you can use composition more effectively most of the time to solve the same problems.
 
Multiple Inheritance is a terrible thing. It's incredibly unnecessary to. If you need multiple inheritance, I would question your design because you're probably violating the single responsibility principle.
 

CrankyJay

Banned
Multiple Inheritance is a terrible thing. It's incredibly unnecessary to. If you need multiple inheritance, I would question your design because you're probably violating the single responsibility principle.

That really depends on what you're using it for.

In C# you can inherit multiple interfaces...you might have specialized dialogs that only inherit a subset of everything that's available.
 
I agree with most of what you say, but this summation is just a bad one. The reason Java is the way it is is because it is focused around enterprise development which is slow moving, often consists of massive teams and massive projects, and relies heavily on backwards compatibility and legacy support.

That also happens to be the exact reason why it's pretty much the best language for that sort of setting. The portability also helps. There's a reason why it has the most tools out there for that sort of development.

Yeah I get that there are benefits there for larger teams etc. I'm just a bitter, twisted elitist and hate everything.

Screw multiple-inheritance. I almost never use inheritance of any kind anyway. Imo you can use composition more effectively most of the time to solve the same problems.

Multiple Inheritance is a terrible thing. It's incredibly unnecessary to. If you need multiple inheritance, I would question your design because you're probably violating the single responsibility principle.

You are right that yes composition is the preferred approach most of the time but for some problems multiple inheritance is the correct way to model something, (A is a B and it is also a C) and right then is when the lack of it just sucks so so much. It all comes back down to the main objection I have with the language which is that it should let me decide how to do it and don't hold my hand so much that one of them effectively gets tied behind my back.

Also, interfaces are effectively doing the same thing as multiple inheritance, except in a more limiting way. It's a half assed way to solve the traditional problems with multiple inheritance without actually providing a fully fleshed out alternative.
 
Total newbie here. I'm doing excel VBA stuff at the moment.

I've got this form-like thing on sheet 1. After the user has written all kinds of stuff in it, I want the "Done!" button to make a new sheet with that same form in it, but empty.

I've got a done button and it's making a new sheet, but I'm scratching my head over the get-the-form-from-the-last-page-also bit.
 

Kimaka

Member
GAF, I need your help. I'm trying to implement a template class in Java and I get this error 'The operator < is undefined for the argument type(s) T, T' in Eclipse. I'm fairly new to Java and come from a C++ background so I have no idea what that means. I tried looking it up and thought using Comparable would fix it but that seems to be something used to compare Generics instead of unknown types within an array. Bolded is the error.

Code:
public class PriorityQ<T>
{
	PriorityQ(){}
	
	void Sort()
	{
		int varPos = m_queue.size() - 1;
		int swap = 1;
		
		while(swap == 1 && varPos > 0)
		{
			if([b]m_queue.get(varPos) < m_queue.get(varPos - 1)[/b])
			{	
			}
			else
				swap = 0;
		}
	}
	
	private ArrayList<T> m_queue;
}
 
I agree with most of what you say, but this summation is just a bad one. The reason Java is the way it is is because it is focused around enterprise development which is slow moving, often consists of massive teams and massive projects, and relies heavily on backwards compatibility and legacy support.

That also happens to be the exact reason why it's pretty much the best language for that sort of setting. The portability also helps. There's a reason why it has the most tools out there for that sort of development.

In a way, I feel you are validating his point.
 
Top Bottom