c++

  1. S

    Xor'ing float value & general cast question concerning compiler output

    Well, both questions are concerned towards my compiling output, since I try to remove all warnings.. To the first question: I'm xor'ing float values, compiler output: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] inline float...
  2. P

    Why does C++ disallow the creation of valid pointers from a valid address and type?

    If you know two pieces of information: A memory address. The type of the object stored in that address. Then you logically have all you need to reference that object: #include <iostream> using namespace std; int main() { int x = 1, y = 2; int* p = (&x) + 1; if ((long)&y ==...
  3. M

    Std::normal_distribution result for the same generator value?

    I noticed a very weird behavior with GNU C++ standard library. When using a custom generator with normal distribution, it seems the distribution might return different values even for the same generator result. Example code #include <iostream> #include <string> #include <random> template <int...
  4. B

    Linked List appendNode method “read access violation.”

    My appendNode method returns read access violation if I add more than 1 node to my linkedlist. I've found that almost every function that has the while (node->next != null) also says read access violation when I run the program. I am not sure why, and I have checked my program against other...
  5. U

    How do I convert an std::stringstream to a uintptr_t and maintain the hex value?

    Currently, I am attempting to calculate offsets from a pointer address, and the number of offsets to be calculated can change, so the approach must be done dynamically. I start by looping for the number of offsets I am trying to calculate, each offset is 4 bytes apart, thus I multiply the...
  6. G

    C++ number manipulator programer

    I'm a beginner at C++ and function, Been getting the error "linker command failed with exit code 1" wondering anyone could give me some insight on how to resolve this problem, and on my overall code organization. Thank you very much for your time and opinion on this matter :) ignore the below...
  7. C

    Weird Behavior of std::cout

    I have a class for representing nodes in C++ but when printing the output out to with std::cout I am seeing a weird issue. Here is the class: class NodeInt { public: int value; NodeInt* left; NodeInt* right; NodeInt() { left = NULL...
  8. R

    What's the possible result of the following code in concurrency situation?

    I have seen an interview question as below: What's the possible range of the result of the following code: void ThreadProc(int& sum) { for (int i = 1; i <= 50; i++) { sum += 1; } } int main() { int sum = 0; thread t1(ThreadProc, std::ref(sum)); thread...
  9. D

    Dealing with tabs with ifstream and reading an arbitrary number of values into a vector

    I'm basically working with fragments of a text file like this: 6 Jane Doe 1942 90089 3 1 5 12 There are tabs on lines 2-5. I'm trying to hold each of the values in an appropriate variable and I would like to have the numbers on the bottom line stored in a vector called friends...
  10. C

    In C++, given namespace-wide const float dependant on a global const float, is their initialization order guaranteed?

    If there is a pair of .h/.cpp files with something like, respectively, extern const float ge; and const float ge = 2.2f; in them, and the other .cpp file with something like namespace { const float upperLimit = 2.0f * ge; } // namespace bool foo(float a) { return a < upperLimit; } in...
  11. G

    Correct programming with namespaces

    I wonder what is the right way to use namespace while calling functions or using classes from libraries. Should I always call a function in the format namespace::func()? I'm a little bit confused because when I'm trying to use the function cout from <iostream> I always need to add the library...
  12. A

    Callback in C++ struct

    I have been trying to implement a callback function in c++. Within a class, I have a struct, a number of methods, and a method that creates an instance of the struct with one of the other methods as its argument. The struct has many other variables, but an illustration is depicted here: class...
  13. M

    C++ Program doesn't terminate because of memcpy

    I'm currently testing memcpy function. I've checked documentation and everything applies when I don't dynamically allocate memory. But when I do, the program just doesn't terminate. Like it enters infinite loop. Here's the code, I can't get to the point of understanding why it happens because...
  14. B

    Doubly linked list constructor with vector inside c++

    I am new to c++ and am working on an assignment involving vectors and a doubly linked list. I am given this struct as such. struct Node { std::vector<T> data; Node<T>* next; Node<T>* prev; Node(): next(nullptr), prev(nullptr){} }; I am now required in another class to create a constructor for...
  15. R

    Constant value changing

    Suppose I have a struct definition: struct thing { thing* x; int z; thing() : x(this), z(0) {} void foo() const { this->x->z++; } }; Note that I create a mutable pointer to myself (evil laugh) And then I can use this later like this: int main() { const...
  16. P

    Objective difference between register and pointer in AVX instructions

    Scenario: You are writing a complex algorithm using SIMD. A handful of constants and/or infrequently changing values are used. Ultimately, the algorithm ends up using more than 16 ymm, resulting in the use of stack pointers (e.g. opcode contains vaddps ymm0,ymm1,ymmword ptr [...]instead of...
  17. C

    How to check if a value exists in a bit-enum?

    I have an enum: enum Rule { BIT_NONE, BIT_ONE, BIT_TWO = 1 << 1, BIT_THREE = 1 << 2, BIT_FOUR = 1 << 3 }; Now, how do I verify that a specific int value belongs to the above enum? Note that the idea is to also allow any combination of BIT_TWO, BIT_THREE, and BIT_FOUR, ergo...
  18. S

    When should I use std::any

    Since C++17 std::any is introduced. One can now write code like this #include <iostream> #include <any> #include <string> int main () { const double d = 1.2; std::any var = d; const std::string str = "Hello World"; var = str; } A double is assigned to the variable var and than...
  19. C

    Understanding how this works

    I'm currently learning C++ at my school and we got a assignment to translate binary to ASCII. Me and my friend found this code here on this site. #include <bitset> #include <fstream> int main() { std::ofstream fout("ASCII.txt"); std::ifstream fin("Binary.txt"); char ic; std::bitset<8>...
  20. Y

    Writing a static cpp library for a haskell stack project

    I'm trying to write a debugger for windows with haskell stack. Because there is no other way to pass the right flags to CreateProcess using haskell packages I've decided to write a wrapper for it. Here is what I've done: // process.h #pragma once // Headers #include <Windows.h> // Functions...
Top