The programming thread.

pibbuR

Feeling ... lonely?
Joined
November 11, 2019
Messages
2,097
Location
beRgen@noRway
TL;DR; Starting with C++.

Don't think we have one (there's Ripper's Godot thread, but that's too narrow), but we should have one; To talk about our experiences, preferences (why Java sucks), asking for (and getting) answers and advice++; So here is my humble fist contribution;

As I've said elsewhere I'm now learning C++; I knew a bit about from years ago, and I have done some programming, but it's a long time a go; And Boy!, the language has certainly developed; I'm now using ver 20, and a lot has changed since back then;

Why C++? Well, it is quite often faster, see https://www.codeproject.com/Articles/5310338/So-Youve-Got-10M-Particles-to-Simulate for a comparison between C++ and c#, showing up to twice the peformance of C++ (but not for every project); Several reasons for that, I won't go into details, but as (most of you?) know, unlike C++, c# doesn't compile into machine code, but into intermediate code, which is then just in time compiled into machine language when needed; In most cases, I think the difference is negligible, unless performance requirements are very hign, as in games;

Other factors:
  • Support for 3D API's, which leaves something to be desired for c#;
  • The possibility (curse) of creating windows applications from the ground up;
  • Linux support;
  • Default;

Of course c# has it's benefits:
  • Developing Windows applications is very fast;
  • Huge libraries;
  • The languge is very elegant (Yes, I think part of C++ is a mess);
  • Lots of errors that might cause run time crashes (allowed in C++) are stopped by the compiler;
  • Default;
One example regarding the libraries. In one of my projects I needed to get a Guid. In c#, just call Guid.NewGuid(); In C++ I first had to consult the net for howto do it, and I ended up creating a 5-10 line routine for the job;

But I love C++, and I suspect that whenever possible I will use that language for my projects; Maybe in combination with c# for the windows;

I'm currently using Visual Studio 2019 and 2022 (preview) for C++, and I notice a few problems, compared to c# programming:

Intellisense is much slower, and often complain about errors in my code, when it compiles just fine (without any warnings); And compiling applications takes significantly more time; Some of it must be due to the linking part, but I doubt that all of it comes from that; I suspect that Visual Studio is first of all adapted to C# (and VB) programming, support for C++ isn't considered that important;

I am considering using other IDE's, maybe somthing that also works in Linux; Any advice?

For (re)learning the language I'm using "Beginning C++20" by Ivor Horton and Peter Van Weert, which I think is a very good book, hereby recommended; Unfortunately books about Windows UI programming using C++ are few; I have of course the Petzold book from '95. But that one is old, and is based on C; Fortunately a previous version of the Horton book ("Beginning Visual C++ 2013") do cover it to some degree;

Today I'm going to start Windows programming, two projects: One using the win32 API, and one using MFC; We'll see where I'm going from there; I will also try GTK and Qt at a later stage;

PibbuR pibbuR{191,94};

PS. As stated in another thread, I'm not a game programmer wannabe; DS.
 
Last edited:
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
C++ is a nice language if you need speed.

Just don't overdo classes and objects - use them only if you really need them.

C and C++ can be used in a classic functional way pretty well.
 
Joined
Oct 18, 2006
Messages
19,813
Location
Germany
I used to love C++ but I've moved on to C# and Kotlin which eliminate a lot of boilerplate code (Kotlin can also be used for Android development, and benefits from the Java library). But IMO C++ remains a very good choice for speed, and to learn what is really happening / to better control what is happening.

Doc:
I used to check this website for reference, it was excellent but I haven't used it lately: http://www.icce.rug.nl/documents/cplusplus/

IDEs:
Microsoft's Visual Studio and Jetbrains' CLion are the best IDEs, but only Visual Studio has a free Community Edition. CLion is running on Win10, Linux and macOS, and it's probably better than Visual Studio at this point. You could try it if you can't solve those performance problems and can afford it, there's a 30-day trial.

I see that Visual Studio is allegedly working in Linux, but I haven't tried (or I misunderstood).

Qt Creator was promising to be good when I last tried it, even if you don't use the Qt framework. It was slower in Windows, especially the debugger, because it was using gdb.

There are others like Code::Blocks that some seem to appreciate, but honestly I don't know them. When I tried, it looked awkward to use but it was long ago.

Frameworks:
It all depends what you need, of course, but here are some tidbits, others probably have more experience than me with that.

MFC has some components running in Linux, through Mono. Not WPF unfortunately, which is one of the best UI framework around. It also has a good framework for databases (Entity Framework).

Qt was fun, and they have adopted the stylesheet paradigm for UI, like WPF, Android and others, which usually makes it much easier to create interfaces. I don't know how they fare, after all the changes in ownership.
 
Joined
Aug 29, 2020
Messages
10,157
Location
Good old Europe
And if you're meddling with C#:

- C# in a Nutshell by the Albahari brothers is a must-have reference
- don't miss their tool either, LINQPad, it's a tremendous help. Even more if you are using C# to process data.

But as much as I love the language, it's too tied to Windows. Sure, Mono exists, but it's always hard to tell what is available and stable.

For me, Kotlin is one of the most promising languages now, although the performances are not 100% there yet, nor does it have any mature UI framework for workstations.

Python is my friend for scripting and small project, or easy web design with Django.

For both, Jetbrains tools are just a developer's dream become true.
 
Joined
Aug 29, 2020
Messages
10,157
Location
Good old Europe
Isn't there a single call to create a GUID?

Looked it up and yes, it is CoCreateGuid(). It is also the function C# calls to get a GUID.
Do you need something more than just the GUID that you end up with 5-10 lines of code?
 
Joined
Aug 30, 2006
Messages
11,223
Ages ago, when I was stuill learning programming, I really really FUCKED UP my Win98 installation by testing a program from that training at home and not setting a pointer right … I had to reinstall my whole system.
In Windows 2000, which we had for training at that "schol", that wouldn't have happend, because of the far better memory system.
But, in the end, that gave me the willies and since then I stay away from C++ as far away as possible. Not 10 horses can bring me back to that.

I must admit, though, that it is indeed very fast - and that garbage collectors can be garbage, because one never knows when they collect (early Java) …

I have never been actively progrtamming in ... 15 years or more, but that way of thinking helps a LOT in daily work. A few days ago I just had the idea of doing an IF-THE-ELSE cjeck in a calculation program to check whether one list has had any differences than another list ... saved us a LOT of time and eye work !
 
Joined
Nov 5, 2006
Messages
21,908
Location
Old Europe
I'm not a programer by any means, but I do like to tinker with with some open source programs for Linux if I think I can change something I need. I use Visual Studio Code for that. It's a text editor, not an IDE but it can be customized with extensions which are plenty. I know there are some debugging extensions for C# and Unity but there should also be something similar for C++. I don't know if it is any good or how it compares with Visual Studio, but it was enough for my (again, pretty small) needs. It runs natively on Linux and is free and open source so you can try that very easily.
 
Joined
Jun 24, 2014
Messages
899
I'm actually shocked that C#/VB is so close to C++ speeds now. It doesn't matter to me - I'm working with databases most of the time that take 30+ seconds to get data while my own code only takes a split second to get it organized and displayed, but it's still impressive that a kinda-interpreted language could even get close to a compiled program.
 
Joined
Aug 3, 2008
Messages
8,238
Location
Kansas City
I'm actually shocked that C#/VB is so close to C++ speeds now. It doesn't matter to me - I'm working with databases most of the time that take 30+ seconds to get data while my own code only takes a split second to get it organized and displayed, but it's still impressive that a kinda-interpreted language could even get close to a compiled program.
Yes, it's surprising at first :D It really depends what you do, and how you do it. C# automatically handles a part of what is done, whereas it's more obvious in C++ since you have to do it yourself.

There are hidden costs too. When a chunk of C# bytecode needs to be executed for the first time after the program is loaded, it's compiled to native code (I think it's rare the bytecode is actually interpreted). It's not instantaneous, and if you're executing a time-sensitive program, or if you're trying to measure its performance, it's noticeable. But once it's done, it can run almost as fast as native code compiled from source like C++. Maybe the optimization won't be as good, but the impact is usually small.

To my big disappointment, last time I checked Java was faster in general, especially when processing I/Os and big collections of data. Maybe C# caught up since then.
 
Joined
Aug 29, 2020
Messages
10,157
Location
Good old Europe
@pibbuR; You may want to check https://www.hackerrank.com/, it provides many exercises for several languages, including C++. I saw that when I was looking for a little refresher in Python.

There are surely other similar sites. This one is easy, you can directly type and run your code via the browser. It tests the solution with a (hidden) set of data to validate the answer, it's quite fun to practice. What I also like there is that the exercises cover the language and library features, they're not just random little problems.

Nothing like a bit of practice beside the theory :)
 
Joined
Aug 29, 2020
Messages
10,157
Location
Good old Europe
TL;DR: the beauty of the WIn32 API.

So I've started windows programming. Below is the mandatory "Hello World" using the Win32 API. Admittedly there are ways to simplifying the code, for instance by using dialogs, but I consider that cheating. Admittedly (again) there's not much C++ here, aside form the nullptr.

Now, why do I post this? Because I think it's beautiful. I am at the moment exploring MFC, and I've downloaded the Qt libnraries and tools. But at the moment I consider staying with Win API. It is of course not an efficient way of developing windows applications these days, but who cares? As a retiree I've got all the time in the world, and I'm doing this for fun. However, in the end I may go for Qt, as I can use that one also under Linux. Or I may stay with c# for the UI, and C++ for the things where performance is essential.

Here we go (MFC and Qt versions upcoming).:

#include <Windows.h>
#include <tchar.h>

LRESULT CALLBACK WinProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE InstanceHandle, HINSTANCE PrevInstanceHandle, LPSTR CmdLineLp, int CmdShow)
{
WNDCLASSEX WinClass;

static LPCTSTR ClassName{ _T("Pibbur's class") };
static LPCTSTR WindowName{ _T("Pibbur's window") };

WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.style = CS_HREDRAW | CS_VREDRAW;
WinClass.lpfnWndProc = WinProc;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hInstance = InstanceHandle;
WinClass.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
WinClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
WinClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
WinClass.lpszMenuName = nullptr;
WinClass.lpszClassName = ClassName;
WinClass.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);

if (!RegisterClassEx(&WinClass))
{
MessageBox(nullptr, _T("RegisterClassEx failed"), _T("Error"), MB_OK);
return -1;
}

HWND WinHandle = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
ClassName,
WindowName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
nullptr,
nullptr,
InstanceHandle,
nullptr
);

ShowWindow(WinHandle, CmdShow);
UpdateWindow(WinHandle);

MSG WinMsg;
while (GetMessage(&WinMsg,nullptr,0,0)==TRUE)
{
TranslateMessage(&WinMsg);
DispatchMessage(&WinMsg);
}
return WinMsg.wParam;
}

LRESULT CALLBACK WinProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC DisplayContext;
PAINTSTRUCT PaintStruct;
RECT Rect;

switch (message)
{
case WM_PAINT:

DisplayContext = BeginPaint(winHandle, &PaintStruct);
GetClientRect(winHandle, &Rect);
SetBkMode(DisplayContext,TRANSPARENT);

DrawText(DisplayContext, _T("Hello, pibbuR"), -1, &Rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(winHandle, &PaintStruct);
return 0;

case WM_DESTROY:

PostQuitMessage(0);
return 0;
}

return DefWindowProc(winHandle, message, wParam, lParam);
}


pibbuR::postQuitMessage(42);

PS. How can I avoid "::p" :) : P -remove whitespace) being interpreted as an emoji? DS.

PPS: The code has indentations, but rpgwatch won't let me us'em. DS
 
Last edited:
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
@pibbuR; You may want to check https://www.hackerrank.com/, it provides many exercises for several languages, including C++. I saw that when I was looking for a little refresher in Python.

There are surely other similar sites. This one is easy, you can directly type and run your code via the browser. It tests the solution with a (hidden) set of data to validate the answer, it's quite fun to practice. What I also like there is that the exercises cover the language and library features, they're not just random little problems.

Nothing like a bit of practice beside the theory :)

Thanks. But the book is very good and I 'm using Visual Studio for programming the examples. (I'm very familiar with OOP after using c# for 15 years, and I have used C++ a looong time ago. Just needed to freshen up a bit, and of course learn the new things in C+ +13/17/20.

pibbuR++;
 
Last edited:
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
Yes, it's surprising at first :D It really depends what you do, and how you do it. C# automatically handles a part of what is done, whereas it's more obvious in C++ since you have to do it yourself.

There are hidden costs too. When a chunk of C# bytecode needs to be executed for the first time after the program is loaded, it's compiled to native code (I think it's rare the bytecode is actually interpreted). It's not instantaneous, and if you're executing a time-sensitive program, or if you're trying to measure its performance, it's noticeable. But once it's done, it can run almost as fast as native code compiled from source like C++. Maybe the optimization won't be as good, but the impact is usually small.

To my big disappointment, last time I checked Java was faster in general, especially when processing I/Os and big collections of data. Maybe C# caught up since then.

The article regarding performance is updated. Now, they've including running the parallell array version in usnafe mode for c#, and in this case the difference between C++ and C# is quite smilar. Which shows (I think) that part of the performance hit of c# is due to safety mechanisms such as automatic garbage collection and other things.

pibbuR at somewhat slower than the speed of 299792458 m/s.
 
Last edited:
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
Code:
Have you tried the " [ CODE ] " tag ?

No.

Yes. But it doesn't work as I hoped.

pibbuR
Code:
::P
ostQuitMessage(42);

DS. Better perhaps (Still not as I hoped for, but I guess it will do:
Code:
pibbuR::PostQuitMessage(42);
DS.
 
Last edited:
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
Isn't there a single call to create a GUID?

Looked it up and yes, it is CoCreateGuid(). It is also the function C# calls to get a GUID.
Do you need something more than just the GUID that you end up with 5-10 lines of code?

Yes. But CoCreateGuid returns a struct which has to be formatted before returning a string. Unlike the C# call.

pibbuR who 1) may be exaggerating and 2) has learned to spell "exaggerating" and 3) desperately tries to find the function he wrote and 4) who sadly is a bit unstructered.
 
Last edited:
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
As I've said, the book I'm reading is very good. Here are a couple of things I've learned:

  1. The terms "little endian" and "big endian" derives from Gulliver's Travels by Jopnathan Swift and how eggs were supposed to be cracked in the state of Lilliput.
  2. “Rodolfo Alfonso Raffaello Pierre Filibert Guglielmi di Valentina d’Antonguolla” is the the real name of an iconic star of the silent movie era [aka "Rudolph Valentino"].

pibbuR who aknowledges the benefits of std::string;
 
Joined
Nov 11, 2019
Messages
2,097
Location
beRgen@noRway
Wow a programming thread, we need more threads like this.

C# is not only quite fast now, if you want to do new things you should use .NET core, which means everything will run on linux and/or windows.

But for certain tasks C++ remains much faster and better this page is very interesting for speed comparasions, speed is of course very interesting....

https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/csharpcore-gpp.html

People always talk about speeds, but let us talk about memory usage, RAM manufacturers love java and C#...... take a look at this example. for certain memory heavy applications java and C# is just not in the game at all:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/binarytrees.html
 
Joined
Oct 25, 2006
Messages
6,292
Pthtthth, memory is cheap. ;)

Though the 32-bit barrier has been bothering me of late. One of my big programs has to be 32-bit because it needs to use (I kid you not) JET drivers to read old Access .mdb files. However, it also has to deal with large pictures. I'm hoping we can get a more recent Access engine distributed to everyone but, right now, JET is the only thing that I know is on everyone's PC. People working with pictures that are too large get memory errors all the time.

Even with JET replaced and switching to 64-bit, .Net Framework still has lots of memory limits. I'll find out those details if I ever get to switch.

P.S. If you "go Advanced" on your post, there's a checkbox to disable emojis.
 
Joined
Aug 3, 2008
Messages
8,238
Location
Kansas City
Back
Top Bottom