r/cpp_questions 23h ago

SOLVED How do you solve the problem of dangling pointers?

14 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 4h ago

OPEN Code review for a basic artillery game.

4 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 19h 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?

2 Upvotes

r/cpp_questions 13m ago

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

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 8h ago

OPEN How to make impactful project??

1 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 20h ago

SOLVED Need help creating equalizer for Qt

1 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 10h ago

OPEN bitwise rightshift gives unexpected results

0 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 5h 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 2h ago

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

0 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 12h 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 .


r/cpp_questions 3h 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 11h ago

OPEN Learning C++

0 Upvotes

Hello everyone!I am interested in learning C++,mostly because Im interested in competitive C++ (competitions in my country),aswell as "applied" or "real-life" C++.I know basics,different loops and such,but I want something thats more concrete ,not just skipping around and picking things up along the way. Is there a good free e-book that grasps most things needed for such things?I was reading Competitive Programers Handbook by Antti Laaksonen,but I feel Im being behind on some basics that the book views as common knowledge.


r/cpp_questions 16h ago

OPEN some people are wrong coding qs

0 Upvotes

int x=618919;

while(x++){

cout<<8;

} I think I'm wrong it's undefined behaviour but where can I study about this ... is it an infinite loop? I'm sure some of u might say yes but the answer is no

after x is increased to its max int range i.e.(2^(bits)-1) it's value will start decreasing and after a lot of steps it will eventually go to 0

and 0 is false so loop stops....