r/Cplusplus • • 1h ago

Question Problem in running C++ codes

0 Upvotes

So i recently purchased a new laptop and when i try running the C++ code sometimes even if my code is correct the vscode doesnt execute the code.

Then I sent the screenshot to chatgpt and it said my code is correct some feature called

WINDOWS SMART APP CONTROL might be preventing it from executing the code .

But once I turn it off i can't turn it on again so what should i do??


r/Cplusplus • • 7h ago

Question Signed left shift causes overflow, and therefore undefined behavior

5 Upvotes

I was solving a question on Leetcode: Reversing Bits

The goal is simple, that we have been given an positive even number and we are supposed to reverse it's binary representation.

If we notice, it says "even" number - that implies the LSB is set to 0 and when we reverse the binary representation the 0 would move to MSB

I thought, it would be great to reverse it with just "bit manipulation" which is as folllows:

```cpp

n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
n = ((n >> 8) & 0x00FF00FF) | ((n & 0x00FF00FF) << 8);
n = (n >> 16)               | (n << 16);

```

Instead of reversing the number, we swap 2 bits, then 4 bits, then 8 bits and then 16 bits. The resultant sequence is exactly the reverse of the original binary representation.

Now, the incoming number is a signed int meaning it has range from:

```cpp

-2,147,483,648 to 2,147,483,647

-2^(31) to 2^(31) - 1

10000000 00000000 00000000 00000000 to 01111111 11111111 11111111 11111111

```

My perspective is that: Just shifting things around isn't really going to make it overflow, since it's a signed int - all that's gonna happen is the result will be interpreted in a wrong manner.

Example: Take the INT_MIN and << 1, what happens? Mathematically it should produce a smaller number that falls outside of the range of INT_MIN, but in computers the 1 is just discarded and we are left with a value of 0

Example: Take the INT_MAX and << 1, what happens? It turns into a negative number, because we are using signed int, does it over flow? not really.

I had initially used int for this and it produced wrong values because the reversed final value would be read as int, so i started casting int to uint and worked with it. Which internally should only work with positive numbers and correct binary representation and it worked.

Where it got weird was - I asked chatGPT to see if my logic is sound and it said that without the cast the number would just overflow in a signed int. From what I understand that's not really possible, because we are just shifting things around and even if we set the MSB to 1 ( for largest possible positive number ) it's just gonna get interpreted as a negative number.

Verdict: It won't overflow

But then it says that the representation is not an issue, it's c++ standard and due to that it may produce an undefined value - basically, in our first example it won't allow wrapping because mathematically it doesn't make sense. But it works!

At this point, i am 100% sure that it's wrong, it's just not possible to overflow the signed int through this algorithm, it makes sense to use uint because it would maintain proper representation internally and then finally when we cast it back to int - we would get the correct representation in signed format. It's only possible to overflow if we were working with decimal values.

I would say that even if the input was an odd number, still there wouldn't be any overflow just wrong representation!

TLDR: Don't sideline me for using AI, i just used it to summarize and verify my understanding. I think that it's wrong according to the current standards and undefined behavior or overflowing isn't really any issue. Just that the results will be wrong because it would be interpreted wrong.

The same would apply to even if the incoming number was negative or odd or completely positive (uint)

Thoughts?! What am I missing like would it really overflow?


r/Cplusplus • • 17h ago

Discussion Why C++?

Thumbnail reddit.com
0 Upvotes

r/Cplusplus • • 19h ago

Discussion Exp. C++ Dev - Getting started with multithreading ?

Thumbnail reddit.com
1 Upvotes

r/Cplusplus • • 1d ago

Feedback 4k balls, single threaded

Thumbnail
youtu.be
6 Upvotes

r/Cplusplus • • 1d ago

Question IDE suggestions

14 Upvotes

I have just started learning C++ from a background in Python, but I don't know many C++ IDEs. What is a good C++ IDE (ideally online but that isn't a requirement) with a built in compiler. If it doesn't have a compiler, what is a good compiler to use with it.


r/Cplusplus • • 2d ago

Tutorial Physics Programming Part 5: Spring Physics

Thumbnail
youtu.be
5 Upvotes

I explain the mathematics underpinning spring mechanics then implement a solution in C++ using a net as an example. Implementation begins at 10:05. Feedback is welcome.


r/Cplusplus • • 2d ago

Question Raylib help

0 Upvotes

I can’t add images in raylib even tho I’ve tried everything I’m a beginner and my teacher told us to make a game and I want to add music and some pictures. Please help


r/Cplusplus • • 2d ago

Discussion A Cargo-like custom build system wrapping CMake for linux system programmers

Post image
1 Upvotes

I am working on a custom build system named Redline which is like Cargo of Rust.

This is actually a wrapper and the real build system is still CMake so that the cpp programmers who have been using and learning CMake can work with it easily.

Features:

  • redline rev: scaffolds a project (src/main.cpp, redline.toml, CMakeLists.txt, git init)
  • redline run [--release]: builds with CMake + Ninja and runs, with separate dev/release profiles (dev has ASan/UBSan on by default)
  • redline run --gdb | --valgrind | --perf --flame: debugging and profiling without hand-assembling the flags
  • redline doctor: checks for cmake, ninja, gdb, valgrind, perf and a flamegraph renderer, and offers to install what’s missing
  • redline add / redline test: FetchContent dependencies and Catch2 tests
  • redline todo: lists TODO/FIXME comments across your code
  • redline man : a local man-page-style C++ feature reference (C++11 to 23) in the browser (my personal favourite)

The generated CMakeLists.txt stays fully hand-editable. Redline only touches the managed block between its markers, and everything outside it is yours.

It’s written in Go. I might do a C++ rewrite in the future, but it’s not planned for now.

I used AI assistance while building this.

A short demo video is attached (doctor, rev, run --release, man).

I’d really appreciate reviews on the design, the UX, and anything that feels off or missing. Feedback and issues are welcome.

GitHub: repo


r/Cplusplus • • 2d ago

Feedback I migrated my C++26 networking library to C++17 to make it easier to integrate into existing projects

18 Upvotes

I've been developing LightnerX, an open-source C++ networking library for multiplayer games.

I originally wrote it using C++26 modules. I liked the idea of building a new project with modern C++ features, and since I controlled the development environment, compatibility wasn't initially a major concern.

But eventually I realized that requiring C++26 for a networking library was probably limiting its usefulness.

So I decided to migrate the entire library to C++17.

This involved more than changing the language standard in CMake:

Replacing C++ modules with conventional .hpp and .cpp files.

Removing dependencies on newer language and standard library features.

Implementing C++17-compatible alternatives to std::span and std::expected.

Updating the build configuration.

Testing the implementation against the original behavior to detect regressions.

One of my biggest concerns was preserving the existing API and functionality during the migration.

LightnerX uses Valve's GameNetworkingSockets as its transport layer and provides:

Client/server networking.

Connection management.

Message batching.

Single, group, and broadcast delivery.

Message interpretation and callbacks.

INER, my own communication protocol for authoritative client-server interactions.

During testing, I also used differential test fixtures to compare the migrated implementation against recorded behavior from the original version.

One fixture included 13,275 parsing invocations and 1,897 delivered messages.

The project is now available as a C++17 library, and I'm continuing to improve its tests, documentation, and examples.

GitHub: https://github.com/pietroth/LightnerX

I'd appreciate feedback from developers who have experience maintaining reusable C++ libraries.

Would you have chosen C++17 as the minimum standard for a project like this, or would C++20 be a better compromise between compatibility and modern language features?


r/Cplusplus • • 2d ago

Homework Give me toughes task do in c++

17 Upvotes

I got addicted to porn so I want to move myself from that

So give me the toughest programming or building things

It makes me busy


r/Cplusplus • • 2d ago

Question Is there any other compiler backend other than LLVM, MLIR?

Thumbnail reddit.com
1 Upvotes

r/Cplusplus • • 2d ago

Question What game should I make in C++?

0 Upvotes

My post with everyone's stories got a bit of traction—not huge, but a few people shared their experiences, and now I want to ask you: what game should I make?

It won't be a 3D game, more of a text/command game where you type something in and things happen. When it's ready, I'll put it on itch.io so you can test it out and drop a link here.

Feel free to suggest ideas and game names! You can even build off each other—for example, one comment can suggest a name, and another comment can suggest how the gameplay works. I'll check out every suggestion.

Instead of clicking buttons or UI, I'd rather have the player type a word or a couple of words—like typing dig (or a command like that) and having something happen in the game.

I'll look through all the ideas, and whichever suggestion gets the most upvotes will be the one I go with. I'll start building it tomorrow! r/cpp r/gamedev r/textgames r/roguelikedev r/learnprogramming


r/Cplusplus • • 2d ago

Answered At what age did you start programming, and in which language?

7 Upvotes

I have a pretty classic question: at what age did you start programming, and with which language? Also, what do you think is the best language for beginners?

For me, C++ wasn't my first language, but it's the one where I really started to understand how programming actually works. I've written some games in it—some didn't turn out quite right, though they were still decent—and I think it actually works well even for beginners.

My very first language was Python, but it felt a bit weird to me since I didn't really know how to use it yet, so I moved on to HTML and CSS. Making websites wasn't boring, though some parts were hard and I didn't understand everything. After that, I tried C#, which felt a bit intimidating and harder at the time. Then I got a C++ book for almost 200 PLN, and while I'm reading it, I understand some things and still don't understand others—which is why I'm curious about your experience! r/cpp r/learnprogramming r/programmingbuddies


r/Cplusplus • • 4d ago

Discussion Updating 21 year old C++ software

45 Upvotes

This is just a dump of what I have been doing lately post. Feel free to discuss.

So I recently undertook the task of updating a piece of software that I designed and developed in 2005. It was first written in C++03 using QT 3.3.6 for the GUI. This ran on CentOS 4.3. Over the years as hardware and OS versions were refreshed it was updated to QT 5.9 but still the same logic written in C++03. Now we are on a system that supports C++23 and QT 5.15.

The software runs on all the nodes of our mid scale distributed systems. Acts as a control interface, status display and master logging software and site UPS interface so the system goes down properly when signaled. That is just a top level list of what is crammed into this this one piece of software. Oddly enough it was meant to just display the status of the system in a simple red or green set of indicators. Yeah, scope creep got the better of this one.

Myself and others have hacked in new functionality, updated other functionality and removed old stuff over these 21 years. However, we have kept to the logic patterns that were laid down in C++03. Which is to say this was more C with classes than pure C++. But a lot of C++ in 2005 was still C with classes. It was just the times. Interesting side note: We practice a form of obsoleting code that does not delete old code. It gets commented out and the new code is under it and comments explain why the change was made. So there is 21 years of this stuff commented out. Yeah finally deleting that stuff as this is going to be new major version.

So I have been rebuilding the program as a modern C++23 app using QT 5.15 as the GUI. Moved from a bunch of global variables to a shared data object. Making the app a data driven system that can run headless now if needed. Configs have moved from custom text file format to JSON. A lot of custom stuff executed by script files called by systems calls has been moved from hard coded to part of the JSON data.

So far I have pruned over 4000 lines of code, added about 2000 lines, rewritten around 5000 lines, removed entire classes and redesigned how the app works underneath without changing the GUI appearance which the customers are very use too. Networking has moved from broadcast UDP ( i don't remember why it had to be that way 21 years ago but there was a tech reason) to Multicast. It has been a wonderful journey so far. The challenge is when we drop it in it has to do everything it did before and be a seamless update. So offline testing is going to be extensive.

Things I knew already and this effort re-enforces. 1. Careful with what you write it will come back to haunt you. 2. There is never enough comments. 21 year is a long time and I just don't remember some of the original reasons for things to work certain ways. 3. Software Life Cycle Support can be rewarding and a curse at the same time.

After 35 years of software engineering and systems engineering specializing in mid to large scale distributed systems I can say it never gets boring. At least it is not ADA and COBOL anymore that I work on.


r/Cplusplus • • 4d ago

News Day 2 of learning c++

Thumbnail
gallery
84 Upvotes

Day 2~ so far it's going good , made some basic progress, got to know about global/local variables,if/else statements;blocks, switch cases with the help of which I made myself my first basic calcultor , but the best one is probably the use of "cin" . Found out I can do much more with cin. Turns out floats are pretty useful too


r/Cplusplus • • 5d ago

Feedback My progress in learning CPP. Had fun learning basic classes.

Thumbnail
gallery
97 Upvotes

Hello guys,

Sharing my learning cpp daily progress---Basic classes. This is my work/mini project for the basic classes Chapter as well as making my own round function, and also improving my exponent function from last time. I also took everyone's advice regarding naming convention, and followed some, but I don't know if did everything right.

It took me 30 minutes with my TI to come up with an idea on how I would do a round function. At first, as was looking at recursion approach, but i think its not that necessary plus it could take up a lot of memory. I know you guys would say that "I could have just used the library", well as a beginner I really wanna come up with solutions on my own, and enhance my analytical ability while taking the challenge of learning C++.

I am all ears for all those who have suggestions/feedbacks. I am willing to learn. For anyone asking if i am still using that book---yes I am. Though I took the initiative to cross check everything I encounter.

I used NO AI whatsoever. Followed the book and used wikipedia, w3school, and cppreference (as for ShishRobot2000s' advice).

For now, the round func will be limited to unsigned short max limit of around ~65k. but as per logic wise, it will work.

Employee.h

#pragma once

class Employee
{

public:
Employee(unsigned short age, unsigned short yearsOfService,
unsigned short salary) :
_age(age), _yearsOfService(yearsOfService), _salary(salary)
{ }

unsigned short getAge() const { return _age; }
unsigned short getYearsOfservice() const { return _yearsOfService; }
unsigned short getSalary() const { return _salary; }

void setAge(unsigned short age);
void setYearsOfService(unsigned short yearsOfService);
void setSalary(unsigned short salary);

void reportSalary() const;

~Employee() { }

private:
unsigned short _age;
unsigned short _yearsOfService;
unsigned short _salary;
};

Employee.cpp

#include "employee.h"
#include <iostream>

unsigned short exponent(unsigned short x, unsigned short n)
{
if (n == 1)
return x;
else if (n == 0)
return 1;
else
return x * exponent(x, n - 1);
}

unsigned short round(unsigned short number, unsigned short place)// PLACE IS FOR DIGIT PLACE VALUE
{
unsigned short placeVal = exponent(10, place);
unsigned short nextDigit = (number / (placeVal/10)) % 10;

if (number < 1'000) return number;

if (nextDigit > 4)
{
number /= placeVal;
++number;
return number * placeVal;
}
else
{
number /= placeVal;
return number * placeVal;
}
}

void Employee::setAge(unsigned short age) { _age = age; }
void Employee::setYearsOfService(unsigned short yearsOfService) { _yearsOfService = yearsOfService; }
void Employee::setSalary(unsigned short salary) { _salary = salary; }

void Employee::reportSalary() const
{
std::cout << "\n\n**** Total employee report ****\n";
std::cout << "Age:\t\t\t" << _age << '\n';
std::cout << "Years of Service:\t" << _yearsOfService << '\n';
std::cout << "Salary:\t\t\t" << _salary << '\n';
std::cout << "Total Salary:\t\t" << round((_yearsOfService * _salary), 3);
}

Main.cpp*. Just paste this block into main*

{
Employee Mark(19, 1, 2'700),
Derk(30, 12, 3'400);

Mark.reportSalary();
Derk.reportSalary();
}

r/Cplusplus • • 5d ago

Question C++ and Data structure

Thumbnail reddit.com
1 Upvotes

r/Cplusplus • • 5d ago

Question Where to start with learning C++?

Thumbnail reddit.com
8 Upvotes

r/Cplusplus • • 5d ago

Tutorial Day 3 of learning c++

Thumbnail reddit.com
0 Upvotes

r/Cplusplus • • 5d ago

Discussion Fast C++ Compiler Update

Thumbnail
reddit.com
2 Upvotes

r/Cplusplus • • 5d ago

Question Transpiler for simplified C++

Thumbnail reddit.com
1 Upvotes

r/Cplusplus • • 5d ago

Feedback Started learning CPP. Hello World! :)

Thumbnail
gallery
153 Upvotes

Just started learning CPP by following the book i found in my dad's bookshelf. Its old and it have been printed from the 2000s. He has his name and date written on the date of purchase; 9th May 2000.

This is my exercise for the chapter tackling functions. I wrote a exponentiate function recursion. I got it hard at first and looked at the different examples from the book multiple times till i get it how i do exponents without loops. With a little bit of function polymorphism/overloading

Tbh, the book looks very weathered, its ancient. Most of the programs and code examples have been deprecated, so I just looked it up online what its modern approach. But the lessons taught are still relevant to todays standards.

Planning to read Modern CPP after finishing this.

Using Visual Studio for ease of use compiler wise. But I will move my learning journey to my linux setup after this to get a better feel for compilers and also Cmake.

NO AI is used to make this code nor supplement it---I just read the book. "It looks clean", yea because its my exercise project, the mess of it lives in the CPP_LEARNING_00 project as you can see in the solution explorer.


r/Cplusplus • • 6d ago

Question wanna get into sfml.

Thumbnail reddit.com
0 Upvotes

how do i start coding in sfml? which version is the best? i want to use visual studio but i don't like how hardware expensive it is and that it doesn't give easy descriptions of code before i use it. I think using code::blocks is good for me but i want to hear your opinion on which IDE i should use.

Also what is best i start learning using sfml? i already know basic things like shapes, .move, keyboard commands, and textures but where to learn more physique base code and make games similar to factorio and rollercoaster tycoon, sandbox games with a lot of work bts.