Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

brx

Member Since 28 Jan 2011
Offline Last Active Yesterday, 07:47 AM
*****

#5027366 [C#/C++]Multithreading

Posted by brx on 30 January 2013 - 05:02 PM

I don't know what kind of multithreading you did in C#, but from my experience it's not easier than in C++. So if you say, that multithreading in C# for all your needs is easy than either a) you're a freaking genius or b) your problems were perfectly fitted for multithreading.

Our brain is just designed in a way that it thinks sequential. Once multithreading comes into play, it's hard to even imagine what might happen and to think of all possible scenarios. Andrei Alexandrescu from the C++ consorptium pretty much naiiled it: "Multithreading is just one damn thing after, before, or simultaneous with another". And that's all we know.

When the code based is not designed for parallelism from the start it is a great step to adapt.

The funny thing is, that most of those people complaining about only one core being used would shut up, if you'd just create <number of cores>-1 threads in your program doing nothing but an infinite loop so they see 100% of CPU usage.

What I am trying to say is, that saying "...there's no simple way to make something multithreaded..." is very accurate. Usually, the first 2 or 3 attempts to parallelize a previously sequential algorithm/architecure will lead in full CPU usage but in slower execution.


#5024684 How to read file line

Posted by brx on 23 January 2013 - 06:19 AM

Judging by the question, you do not only want to read a line from a file, but also write it, right?

Writing:
TextWriter writer = new StreamWriter("settings.dat");
writer.WriteLine(colorId);
writer.WriteLine(userName);
writer.Close();
Reading:
TextReader reader = new StreamReader("settings.dat");
String colorId = reader.ReadLine();
String userName = reader.ReadLine();
reader.Close();
Note that you also should do some error checking, right now bad things will happen if, for example the file cannot be opened or the file does not have two lines in it...


#5017399 Begining an editor

Posted by brx on 04 January 2013 - 02:52 AM

If you choose to compile Qt yourself there are two things that can greatly reduce the compile time:

1. Build Qt on all available cores: By default Qt will only be built on one core. If you have a multi-core CPU, that's annoying. In order to build it on all cores, open up the file <QTDIR>/mkspecs/win32-msvc2012/qmake.conf and find the line starting with QMAKE_CFLAGS. Add "/MP" (no quotes) to the end of the line and save the file (you should do this before running configure).

2. Don't build the examples and demos (you can always compile those that you need later): Open the file <QTDIR>/projects.pro and find the following text:

} else:isEqual(PROJECT, examples) {
     SUBDIRS += examples
} else:isEqual(PROJECT, demos) {
    SUBDIRS += demos
}

change them to

else:isEqual(PROJECT, examples) {
#       SUBDIRS += examples
    } else:isEqual(PROJECT, demos) {
#       SUBDIRS += demos
    }

With those two changes it takes only about 20 minutes to compile Qt on my computer (including webkit). I do have a powerful computer, though (i7-2700K 4 cores/8 threads 3.5 GHz and 32 GB RAM).




#5017034 Begining an editor

Posted by brx on 03 January 2013 - 04:35 AM

Building Qt (4.8.4, didn't try 5 yet) 64Bit with VS2012 works without problems (if you disable webkit - if you need/want webkit, there's some work necessary).  All you need to do is (inside a VS 64bit command prompt):

configure.exe -no-webkit <add more switches if you like, see configure.exe -? for all available options>
nmake

Works for me ™


#5011998 Open a file by tiping in its name in Visual studio 2010 c++

Posted by brx on 18 December 2012 - 05:44 AM

You can use the search box for this: just type in ">of myFile" (without quotes) and it opens the file.

As an alternative there are add-ons that do this. Visual Assist X, for example, has that feature.


#5009987 Can you create a Vector of structs in C++? I need help with a unknown error p...

Posted by brx on 12 December 2012 - 04:23 PM

There's absolutely no need to apologize.

Memory management in C++ is probably THE thing that a lot of people never get right and they just give up trying to learn C++ after a while. The basics are not THAT difficult, though:

The value of a variable has a "lifetime". This lifetime ends with the end of the current scope. I.e. it ends when the next "}" is encountered. So this:

if (true) {
  Rectanlge rect;
}
cout << rect.getX();

will lead to a compiler error since rect is not valid anymore at the point where rect.getX() is called. In the moment the closing "}" is encountered the Rectangle object called "rect" is destroyed. (Keep reading, I know, it's not a great example, since this is a compiler error, and the problem at hand is a runtime problem, just trying to get a feeling for scoping here...)

So for your case this means:
for(int j=0;j<8;j++){
   int ya=(j*50)-1;
   Rectanglee rect(Point(i,j),50,50);
   rects.push_back(&rect);
  }

every time the closing brace of the for-loop is hit, the object formerly know as "rect" is destroyed.

Now, your code saves the address of that object inside the vector. Too bad... the object is dead when the for-loop is left... So that address is nothing but garbage.So in your vector there's a whole bunch (well 8...) addresses that used to be Rectangle objects but are not anymore...

So now, let's look at the "new" keyword. The rectangless we've created so far were on the stack. Objects on the stack behave like I just said. They are deleted once the variable goes out of scope. The second kind of memory is the "Heap". When you use "new" this will create a new object on the heap. That object is valid and available until
a) the program is terminated
or
b) you use "delete" to ... well... delete it.
So this:
for(int j=0;j<8;j++){
   int ya=(j*50)-1;
   Rectanglee* rect = new Rectangle(Point(i,j),50,50);
   rects.push_back(rect);
  }
(note: used a temporary variable to make it clearer) will create 8 new Rectangle object on the HEAP that will be valid even after exiting the for-loop. Now... This may be a problem. Who will ever invalidate (i.e. delete those Rectangles)? They will be on the heap occupying memory until the program is exited (thanks to modern operating systems) or until you explicitly remove them using "delete".

So, if you want to get rid of those rectangles (all of them) this would work:
for(int j=0;j<8;j++){
   delete rects[i];
  }
rects.clear();
This first deletes all the Rectangle objects that you've created on the heap and then clear the vector (because since we've just deleted all the rectangles, it contains nothing but a lot of addresses that formerly pointed to an object of type "Rectangle").

And this is exactly where smart pointers come in play... smart pointers help you to get rid of the burden to manually delete those objects that you've created on the heap, as they will automagically delete the object when there is no smart pointer left pointing at that object (it does not work all of the time [circular dependencies are a problem], but for a start it is pretty good)...


Hope this somewhat helps to get rid of the headache....


#5008070 Problem returning pointer from method

Posted by brx on 07 December 2012 - 03:40 AM

To be honest, I didn't dive too deep into the code, but the problem is that the connectionDestination of the Connection is never initialized (except for the NULL initialization in the Connection constructor). So at the end of the traverse method it will always return a NULL pointer, so currentRoom is NULL after calling traverse.

[EDIT] Damn... forgot to refresh. I opened the topic about two hours ago but then was away from the computer for a while...


#4991393 A "What's Next" Networking Question

Posted by brx on 18 October 2012 - 04:42 AM

There are services that provide dynamic dns lookup. This means that you install a little program that informs a server of your current ip address. It then routes the registered "domain" to that address. So basically, even though your actual IP changes there is a static hostname that will be updated whenever your IP changes.

There are several free providers for this kind of thing. One of the first ones (I think) was dyndns which I used for quite a while. There you registered a name (e.g. DontReferenceMyPointer.dyndns.com) and that one always pointed to your current IP address. However, looking at their website they don't offer a free service anymore. But I found a list of free provider: http://dnslookup.me/dynamic-dns/


#4985681 Visual Studio Express 2012 Yay

Posted by brx on 01 October 2012 - 02:32 AM

I really like VS2012 (using the professional edition) and I really like that the express edition can finally compile 64Bit applications out of the box (although it wasn't hard to set up in VS2010 either: install SDK -> chose it in the project's properties as the platform tools -> done).

What is really a problem is that (until now) applications compiled with VS2012 do not run on WinXP! Yes... WinXP is old and should not be supported anymore. I agree with that. BUT! As a developer of "professional" software (CAD/CFD software in my case) with many customers in Asia (China, Korea) I just have to live with the fact that a very big percentage of my customers use WinXP. So I have to wait for the promised upgrade: http://blogs.msdn.com/b/vcblog/archive/2012/06/15/10320645.aspx. I just hope it comes before our next release...


#4980455 License server and open ports

Posted by brx on 15 September 2012 - 12:58 PM

That would be an option. But, applications that go around scanning ports might trigger security mechanisms.

About the hassle that companys go through to use your software: It all depends on how unique (and good) your software is. If there is no alternative companys will do quite a lot of stuff to use it. But it would be very inconvenient if the customer has to manually type in the hardware information. Plus, instead of you obtaining the actual hardware information, I think it would need to be some kind of hashed value.
What I would do is to have the server generate a license request including the hashed hardware information at install time. Then send that to you. The license file is then generated at your site and sent back. Of course, this would be a lot nicer if you would actually have a server to contact instead of having to use emails for that. Depending on the expected number of sales processing those emails may get quite annoying on your side as well.
I would also not only use the MAC address(es) of the server as it is actually possible to change them.

Yes, creating your own license system is quite a hassle. So, again, looking into exiting ones might be worth it.

Overall, I agree with GWDev. Site licenses (for example key based, similar to windows licenses) are probably the easiest way to go.


#4980225 License server and open ports

Posted by brx on 14 September 2012 - 05:46 PM

I don't understand your use case, why does the client have access to the license server? Doesn't that defeat the point?

Actually, not at all. As already mentioned, especially in the CAD/CAE/CFD field many license models work like that (e.g. StarCCM+, Flowtech Shipflow, NUMECA tools). You get a license issued to a server and several clients can connect to it. FlexLM is a great (and most commonly) used program to achieve that.

Back to the OP...
I assume FlexLM is no option for you. If we are talking about a software where each license is worth a couple of thousands dollars (or euros or the equivalent value in any other currency), then I'd say looking into professional solutions for this problem is definitely worth it. Again (as I am working in the CAD/CFD field and have good experience with it) FlexLM would be a very good option in that case.

Now, if you really want/need to roll your own:
Having a server application with non-configurable ports is a no-go imho.

First of all, (of course) the floating license needs to be node locked to the server it runs on. I assume that's a given, but I'd still like to stress it. HDD-serials and MAC-addresses are good candidates for that. They are easy to get from the OS and rarely change. Limiting the license to the OS is also a good way to go, to avoid "cracking" your license using parallels, for example.

To circumvent the problem of having multiple servers on the same machine I would take a shared memory approach. I don't know what language your server is in, but for C++ the boost interprocess library offers a ncie API.
I'd generate a signature based on the hardware infos stored in the license (which are neccessary and would prevent the license from working on that particular machine if they were changed) and put that into the shared memory. Then have every instance of the server access that shared memory and look for that signature. If it's there, the license is in use and cannot be used again. If it isn't, store the signature in the shared memory which locks it for other instances of the server.

About the VM thing... Sure if the original license was issued for a VM, it is possible to clone it which would cause the same hardware IDs to be generated and then multiple VMs could run on the same license.
However, for most VMs (VMWare, Virtual Box, for example) there are pretty good methods to detect them. It usually involves using some inline assembler, but google it, and then it's only copy paste ;)
So I would actually put a check in the server (which we do for our software, btw.) to prevent it from running on a VM. You might even put a switch in your license to allow customers that can explain why they want to run the server in a VM to do so (which we do).


#4974469 Compiling a Desktop Application?

Posted by brx on 29 August 2012 - 10:19 AM

If you can't find the Qt DLLs following the excellent description by Servant of the Lord, another tip from me:
- Open up your project in QtCreator
- go to "Projects" (toolbutton in the left pane)
- select "Build Settings" (should be selected by default).
- Now in the "General" category the first entry is "Qt version".  Click on "manage" right next to it.
- It should show you the path to the Qt version used by the QtCreator. The "qmake" Location shows you the path of the qmake.exe file which is located in the same folder as the Qt DLLs. Now use the windows explorer to navigate to it and copy the DLLs following SoL's description.


#4970148 Can't figure out why same results appear

Posted by brx on 16 August 2012 - 07:03 AM

The problem is that the if statements are all executed in exactly the order you stated them. So when, for example, you enter 40, the condition if (age >= 17) will be executed and return true. Therefore all the other if statements are actually dead code (i.e. unreachable code). What you need to do is put both conditions age >= 17 and age < 26 into one statement using the '&&;' operator:
else if (age >= 17 && age <26) {
MessageBox.Show("Fame beckons!");
}
This way the "Fame beckons!" message box will only appear when the age is between 17 and 26. Same applies for the "There's still time!" case.


EDIT: Damn, editing messes up the '&' showing up correctly.


#4956434 Pointer to class type?

Posted by brx on 06 July 2012 - 01:30 PM

Yes it's possible, yes they need to inherit the same base class.

Sorry, but while everything (except, when looking at the original question, the quoted part) you wrote is correct, the original question was

I'm looking for a feature where you can make a variable contain a class type.

and that is simply not possible in C++.


#4956253 Pointer to class type?

Posted by brx on 06 July 2012 - 02:15 AM

This is not possible in C++. Classes in C++ are no objects and therefore it is not possible to store them in a variable. It is possible in different languages (e.g. Java, Smalltalk...).

What problem are you trying to solve with this? Looks to me like you want to implement some kind of factory pattern with this?!




PARTNERS