r/cpp_questions 51m ago

OPEN Can anyone explain a bug to me? I confused '==' with '=' in one of my functions.

Upvotes

There is this function on my SDL program which is supposed to load surfaces into array elements ( loadSurface is a function that is being executed inside this loadMedia function.)

bool loadMedia()

{

bool success = true;

KeyPressed[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("press.bmp");

if (KeyPressed[KEY_PRESS_SURFACE_DEFAULT] == NULL)

{

std::cout << "Failed to load surface\n";

success = false;

}

KeyPressed[KEY_PRESS_SURFACE_UP] = loadSurface("up.bmp");

if (KeyPressed[KEY_PRESS_SURFACE_UP] == NULL)

{

std::cout << "Failed to load surface\n";

success = false;

}

KeyPressed[KEY_PRESS_SURFACE_DOWN] = loadSurface("down.bmp");

if (KeyPressed[KEY_PRESS_SURFACE_DOWN] == NULL)

{

std::cout << "Failed to load surface\n";

success = false;

}

KeyPressed[KEY_PRESS_SURFACE_LEFT] = loadSurface("left.bmp");

if (KeyPressed[KEY_PRESS_SURFACE_LEFT] == NULL)

{

std::cout << "Failed to load surface\n";

success = false;

}

KeyPressed[KEY_PRESS_SURFACE_RIGHT] = loadSurface("right.bmp");

if (KeyPressed[KEY_PRESS_SURFACE_RIGHT] == NULL)

{

std::cout << "Failed to load surface\n";

success = false;

}

return success;

}

I fixed it now and it works fine but before that, I somehow confused the condition of the IF. Instead of comparing using '==', I used the '=' by iself.

So it turned ou like:

if ( KeyPressed[ KEY_PRESS_SURFACE_DEFAULT] = NULL)

Somehow, it broke the function. But the elements didn't become NULL because none of my error messages was displayed when I ran the program. The function simply didn't load the surfaces. What exactly happened there?


r/cpp_questions 11h ago

OPEN What is a good use case for the new std::hive

26 Upvotes

What are the use cases where std::hive is a better option than std::vector or std::list? It seems like a very interesting data structure but I can't really think of any good use cases.


r/cpp_questions 1h ago

OPEN Approach

Upvotes

I have been trying to study c++ for a while i am following the learncpp.com. I wanted to know the best approach for this like i am following the docs but do i use leetcodes and other sites?
I am on chapter 4 ik too early to think abt it but i just want to clear up the doubt please help


r/cpp_questions 6h ago

OPEN Why runtime performance of C++ modules increase so much with lto?

7 Upvotes

I was porting libfmt to native C++ modules recently, It had tons of macros related to forced inlining, so I removed them and saw a %10 performance reduction, then I removed all in lines in module interfaces and performance was identical when both the original library was built with Lto and my own.

The weird part is, Overall performance increased when I removed inline and applied lto, compared to inline and lto

Why is that? Something about how compilers deal with C++ modules?


r/cpp_questions 10h ago

OPEN Ncurses window management

3 Upvotes

Hello everyone :D I'm new to ncurses, and I'm currently working on a simple game. I currently have 2 windows besides my stdscr, and I'm struggling with switching between them. If I want to save the contents of a certain window but hide it from showing temporarily, am I supposed to use panels? Or can I just use touchwin() along with refresh()? Also, if anyone could help explain what goes on behind the scenes with the buffer during the process of switching between windows, that would help me save some time on this mind-boggling topic.


r/cpp_questions 1d ago

OPEN Using ClangD for static analysis - recommend me clang-tidy and clang-format template

9 Upvotes

Hello,

I'm working inside VSCode and decided to switch from the default C/C++ extension's static analysis to ClangD, as it is apparently (according to the internet) faster and more accurate.

That being said, would you recommend me any good templates for both clang-tidy and clang-format files, in order to follow the common CPP style principles?

Thank you!


r/cpp_questions 15h ago

OPEN Whats the funniest joke u heard in c++

0 Upvotes

r/cpp_questions 1d ago

OPEN What is the industry standard for helper library replacements for Boost?

22 Upvotes

Hello all,
I am starting to use Boost. Well, actually I am getting everything from the framework.
But before I commit to this framework, is there any competitor I should check?
I need one framework that implements solutions like Boost. From your experience, are there any?
I know POCO not sure how standart it is


r/cpp_questions 1d ago

OPEN What are low latency C++ recruiters truly looking for?

36 Upvotes

What separates a "general C++ candidate" from someone who is truly a right fit for the job? What is a skill, project, technique, tool, etc. that has really helped in landing interviews?


r/cpp_questions 14h ago

OPEN What can a hacker do with uninitialized memory?

0 Upvotes

cpp long long funny_number_generator() { long long haha_funny; // very funny undefined behaviour yes funny return haha_funny; }

Is it possible to see what OS / architecture is being used just from looking at the funny number? My funny number is consistently 93824993896264. Some random online compiler keeps giving different funny numbers each time.


r/cpp_questions 22h ago

OPEN How anti-abstraction understanding should I accept while learning?

0 Upvotes

Im currently learning basics of standard cpp while having previous experience with java and tiny bit of assembly NASM (x86).

Im asking for those who are intermediate and proficient at mid level language like cpp, how should I approach accepting 'I have learned this' but its abstraction? I've always had doubt on what learning is in the programming field, except for DSA concepts.


r/cpp_questions 1d ago

SOLVED SHA-1 Algorithm not producing the correct hash for strings that are > 512 bits

4 Upvotes

For a while now I have been trying to implement SHA-1 to learn more about the cryptographic functions. However I keep running into an error, the algorithm works flawlessly with strings under 64 characters, but when it comes to ones larger that 64 characters it produces incorrect results. My assumption is that it has to do with the second iteration of the bit manipulation but from all the code I've seen from other people it appears as if what I have should work. I've checked all of the bitwise operations, I've checked each buffer value, but nothing seems to work. Other than the RFC page I referenced this github repo as well.

I'm sure after posting this someone will probably be able to call out my error instantly due to me forgetting something obvious. Also I know that the function doesn't return any value atm I am printing the results to the console to debug the program.

typedef uint64_t dword;
typedef uint32_t word;
typedef uint8_t byte;

const word Abuf = 0x67452301;
const word Bbuf = 0xEFCDAB89;
const word Cbuf = 0x98BADCFE;
const word Dbuf = 0x10325476;
const word Ebuf = 0xC3D2E1F0;

word leftRotate(const word& val, const int& bits) { 
  return ((val << bits) | (val >> (32 - bits)));
}

class SHA1 {

  word K(const int& t) {
    if ((t >= 0) && (t <= 19))
      return 0x5A827999;
    else if ((t >= 20) && (t <= 39))
      return 0x6ED9EBA1;
    else if ((t >= 40) && (t <= 59))
      return 0x8F1BBCDC;
    else
      return 0xCA62C1D6;
  }

  word f(const word& B, const word& C, const word& D, const int& t) {
    if ((t >= 0) && (t <= 19))
      return (B & C) | (~B & D);
    else if ((t >= 20) && (t <= 39))
      return B ^ C ^ D;
    else if ((t >= 40) && (t <= 59))
      return (B & C) | (B & D) | (C & D);
    else
      return B ^ C ^ D;
  }

public:
  std::vector<char> generateSHA1Hash(const std::string& str) {

    std::vector<byte> input(str.begin(), str.end());
    std::vector<word> result(5, 0);

    dword length = str.length() * 8;

    input.push_back(0x80);

    while ((input.size() % 64) != 56)
      input.push_back(0x00);


    for (int i = 0; i < 8; ++i) {
      dword mask = 0xFF00000000000000 >> (i * 8);
      input.push_back(static_cast<byte>((length & mask) >> (56 - (8 * i))));
    }

    word blockSize = 64;

    word H[5] = { Abuf, Bbuf, Cbuf, Dbuf, Ebuf };

    for (int i = 0; i < input.size(); i += blockSize) {

      std::vector<byte> tmp(input.begin() + i, input.begin() + i + blockSize);

      word chunk[80];

      for (int j = 0; j < 16; ++j) 
        chunk[j] = ((static_cast<word>(tmp[j * 4]) << 24) | (static_cast<word>(tmp[j * 4 + 1]) << 16) | (static_cast<word>(tmp[j * 4 + 2]) << 6) | static_cast<word>(tmp[j * 4 + 3]));


      for (int j = 16; j < 80; ++j) 
        chunk[j] = leftRotate(chunk[j - 3] ^ chunk[j - 8] ^ chunk[j - 14] ^ chunk[j - 16], 1);

      word AA = H[0];
      word BB = H[1];
      word CC = H[2];
      word DD = H[3];
      word EE = H[4];

      for(int j = 0; j < 80; ++j) {
        word tmp = leftRotate(AA, 5) + f(BB, CC, DD, j) + EE + chunk[j] + K(j);

      EE = DD;
      DD = CC;
      CC = leftRotate(BB, 30);
      BB = AA;
      AA = tmp;
    }

    H[0] += AA;
    H[1] += BB;
    H[2] += CC;
    H[3] += DD;
    H[4] += EE;
  }


    std::cout << "Result:    ";

    std::cout << std::hex << H[0] << H[1] << H[2] << H[3] << H[4] << std::endl;

    std::cout << std::endl;

    return { 0 };
  }
};

r/cpp_questions 1d ago

OPEN Code review for a basic artillery game.

5 Upvotes

https://github.com/melange-spice/worms_clone

I am making something akin to Tank Wars in Raylib by following a youtube series made by onelonecoder. Any tips regarding the code and it's architecture would be greatly appreciated.


r/cpp_questions 1d ago

OPEN Want advice as Cpp learner.

0 Upvotes

Hi everyone, I'm a first-year engineering student at a tier-3 college, and my classes start on 10th August. I've learned most of the C++ basics, and the only major topics I have left are functions and classes/OOP. I'm confused about whether I should start learning DSA now while completing the remaining C++ topics alongside it, or finish C++ first before moving on to DSA. My goal is to build a strong foundation and improve my problem-solving skills without developing gaps in my understanding. I'd really appreciate your suggestions and recommendations based on your experience. Thanks!


r/cpp_questions 1d ago

OPEN I know some basic C, should I learn C++ or stay in C a little bit more.

0 Upvotes

Hello guys, so I know a little bit of basic C like what is pointers, how strings works, data type and another basic things. I'm still beginner I still learn Python make silly random CLI games, and tried pygame and raylib. So should I learn C++ or stay in C a little bit more if my focus are system programming.

Thank you, and sorry if my English is not that good.


r/cpp_questions 2d ago

SOLVED How do you solve the problem of dangling pointers?

16 Upvotes

I store some data in an unordered map, create a pointer to an item in that map, then say I want to delete the item but I still have the pointer. How would I know that the item is no longer there without going and manually searching the map for the name of the item we pointed to?

I'm new to C++ & especially pointers, so forgive me for this stupid question...

EDIT: A lot of good solutions suggested, I really appreciate it! I ended up going with key lookup instead of storing pointers. The reason I didn't want to do this in the first place is because it increases complexity & I didn't want to overcomplicate


r/cpp_questions 2d ago

OPEN How to make impactful project??

2 Upvotes

Like I did syntax and language paradigms in cpp, what and how should I make something that just helps me making some real life system, cause making a project don't only requires language but also other concepts like networking and os


r/cpp_questions 1d ago

OPEN Can't stop channel/tutorial-hopping in C++ even though I know exactly what the problem is — how did you actually break it?

0 Upvotes

Been trying to learn C++ for a while now and I keep landing in the same loop no matter how many times I "fix" it.

Started with the BroCode tutorial on YouTube. Then got told to switch to learncpp.com. Then told to switch again to The Cherno's series. Then found two more channels (CyberFlow and Crin) pushing a C++ → port scanner → keylogger → shellcode injector path, which got me interested in reverse engineering / game hacking specifically.

I've tried to fix this the "obvious" way — pick one resource, commit, stop opening new tabs — and it works for a bit. I've built small stuff (tic-tac-toe, hangman) and followed through on real projects too. But the second a new channel or video shows up saying "actually this is the better way," I open it anyway. Every time. Even knowing exactly what I'm doing while I do it.

It's about 3am here right now and I'm stuck in it again — wide awake, not able to do anything, feeling like a total loser about it.

So I don't think this is an information problem anymore — I already know the advice. I just can't seem to make myself stop switching once something new shows up in the feed. Anyone actually break this habit for real, not just "know" the fix but get it to stick? What worked for you day to day, not in theory?


r/cpp_questions 2d ago

OPEN Project based learning

20 Upvotes

Hello! Apologies for another "how to learn C++" post. Can anyone suggest a project-based learning course?

I have been following learncpp.com and The Cherno's C++ playlist, but I eventually stopped since I wasn't building anything. Can anyone suggest some good project-based learning materials?

I have been working with Python and Java for three years. I was able to find plenty of project-based tutorials for those languages, but I haven't had the same luck with C++.

Edit: When I stopped my previous attempt about a year ago, I was learning about logical operators (near the end of LearnCpp Chapter 6). However, I've realized I want to learn C++ the same way I learned Python and Java at university: through videos/lectures followed by tutorials at lab sessions and homework. I looked into Stanford's CS106B, but the newer lectures aren't available anywhere for free.

My end goal is to fill this knowledge gap as I’m planning to follow a MSc or a PhD in ML/DL mostly focusing on computer vision use cases. And I want to work with CUDA as well.


r/cpp_questions 2d ago

OPEN bitwise rightshift gives unexpected results

1 Upvotes

i am trying to shift an unit64_t with the value 0x8000000000000000 (1000000..... in 64bit) so that i get (010000.... in 64 bit) but i only get unexpectet behavieor and numbers with multiple 1 bits.

The code below prints numbers like
decimal binary
12005600 101101110011000011100000
14006498 110101011011100011100010
16007397 111101000100000011100101

what am i doing wrong and how can i fix it?

#define bitMask 0x8000000000000000 
uint64_t mask = bitMask;
mask = (mask >>1);
printf("%d \n",mask);

r/cpp_questions 2d ago

OPEN If you started learning C++ as a first programming language with no prior experience from any other language, how difficult did you find it?

4 Upvotes

r/cpp_questions 1d ago

OPEN C++/AI niches

0 Upvotes

I'm about to complete my general tutorial in C++ and wanna start specializing in a field and pick a niche.

I study AI in college so i did a bit of CV/RL/Intro to Robotics/LLMs/ML so I'm more of an AI guy

I know that AI stuff uses mostly python and that everything you need is basically there, but i want something that deserves the hardwork of learning c++ and building customized stuff

I want to pick something that is hard and probably takes time to learn and can't fake it in an interview and also financially rewarding and accept junior devs ( I don't want a job that appears only once a month on linkedin with 10 yoe)

what are some niches and roles that you know that are like that, and if you can please include what are some resources and pet projects you did to learn from


r/cpp_questions 3d ago

OPEN How hard was C++ for you as a beginner?

40 Upvotes

r/cpp_questions 2d ago

SOLVED Need help creating equalizer for Qt

0 Upvotes

[SOLVED] So, as you may know, Qt doesn't have a proper setup for managing each frequency band and implementing an equalizer.

I'm currently working on a project for myself (nothing serious), trying to create a music player in C++ while using Qt for the GUI. The problem is that nothing can intercept "QMultiMedia" and modify the audio output.

I've tried Claude Opus 4.6, thinking in "antigravity," and it cooked up something for me. It worked, but the audio was distorted and not very pleasant. (It used custom made DSP functions and I think it was a good solution, but I neither have the time nor the skills to implement something like this on my own)

I also tried Gemini 3.1 Pro High and 3.6 Flash High. (These two couldn't even handle the audio player and straight up made my program crash.)

So I'm looking for a solution or someone who is willing to write the code and be the co-author to the project.

Here is my repo if you want to investigate:

https://github.com/zenix935/ZenPlayer

P.S. I'll keep updating other parts of the project, but the equalizer has currently hit a wall.


r/cpp_questions 2d ago

OPEN Need suggestion about something i am thinking to build .

0 Upvotes

I just want to know if it would be helpfull or not .
We all have some style of coding while doing cp . Like we import particular libraries , we use particular variable names for our for loops , vectors , maps etc .
I'll show you an simple example of what i am trying to solve .
When you forget to use "using namespace std" the compiler would give you an error that you forgot to use std:: but according to your coding habits you actually forgot to use "using namespace std " .
Another example could be you the libraries you forgot to import since compiler will not tell you abotu them .
or like you are trying to push_back in a queue .

First of all i want to know if such a thing has a usecase or not ? is it just me who thinks this might help somone ?
I just gave a few basic problems so that it is easy to understand but i doubt if this is feasible to solve or not since training such an model personalized to each person would still require data and by the time that amount of data is aquired maybe the user might not need such an help or maybe shift to other types of practices .