« first day (350 days earlier)      last day (2508 days later) » 

2:22 AM
en.cppreference.com/w/cpp/language/memory_model

"When an evaluation of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to conflict. A program that has two conflicting evaluations has a data race unless
both evaluations execute on the same thread or in the same signal handler, or
both conflicting evaluations are atomic operations (see std::atomic), or
one of the conflicting evaluations happens-before another (see std::memory_order)
 
@GreenTree What you have has undefined behavior. In particular, even though you don't see anything using (reading) the value, cnt++; actually evaluates to a read/modify/write cycle (at least on most CPUs). The final sentence specifies "an atomic object", which is the part that makes it defined behavior. If you started with std::atomic<int> cnt;, then your code would have defined behavior as well.
 
2:47 AM
@JerryCoffin but multiple threads modify that atomic integer concurrently in second example. is it ok.?
 
 
1 hour later…
3:52 AM
@GreenTree Yes, that's fine.
 
 
2 hours later…
6:00 AM
I've read once that sentence and kept my misunderstanding till today. my conclusion was that atomics are only to deal with reordering issues.
thanks.
 
6:12 AM
@GreenTree No--basically, there's no such thing as a race condition in accessing an atomic. That is, you can have race conditions at a higher level (like if you need to keep two atomic variables in sync with each other), but the access to an atomic variable always has defined results (except, perhaps, reading one that hasn't been initialized).
 
 
3 hours later…
9:28 AM
@MuhamedCicak OK. First thing, that you don't understand, is that how C++ projects and C++ modules are generally being linked against each other. There two types of linkage in almost all kind of programs, especially for compiled languages, like C++: dynamic linking and static linking. You need to read on both and how to implement both in VS for example.
@MuhamedCicak Congrats on getting things done
@MuhamedCicak you have chosen static linkage, there's also a dynamic linkage: msdn.microsoft.com/en-us/library/ms235636.aspx
 
9:49 AM
@PaulD Thanks. Do you have any good website where I can read about what linker is and what are static and dynamic linkage?
 
@MuhamedCicak I don't have any in my mind right now, I advice to just google
280
Q: How does the compilation/linking process work?

Tony The LionI've been programming in C++ for a while and I wondered how the compiler and linking process actually works? Can someone explain please? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on met...

327
Q: When to use dynamic vs. static libraries

Morten ChristiansenWhen creating a class library in C++, you can choose between dynamic (.dll) and static (.lib) libraries. What is the difference between them and when is it appropriate to use which?

 
@PaulD Thanks, I will have a read.
 
10:32 AM
can i get signed output if i subtract 2 unsigned int64
i dont want rolling over
I tried:
#include<iostream>
#include <cstdint>
int main()
{
uint64_t a=1498189560048225507;
uint64_t b=1498189571718833103;
uint64_t c = a-b;

uint64_t abs_diff = (a > b) ? (first - second): (second - first);
assert(abs_diff<=INT64_MAX);
int64_t result = (a > b) ? (int64_t)abs_diff : -(int64_t)abs_diff ;
std::cout << result;
//std::cout << "a-b=" << a-b << std::endl;
//std::cout << "b-a=" << b-a << std::endl;
//std::cout << "c=" << c << std::endl;
}
 
11:31 AM
@pythonRcpp You can cast uint64_t to int64_t, but you need to remember that there's a risk the value will be altered.
@pythonRcpp My suggestion is to use Boost.NumericConversion
 
@Toreno96 "You can cast uint64_t to int64_t, but you need to remember that there's a risk the value will be altered." when would that happen ? Any particular scenerio ?
 
Then you can do boost::numeric_cast<int64_t>(abs_diff) instead of (int64_t)abs_diff and if something during conversion goes wrong, your program will throw specific exception.
@pythonRcpp Assume that A is of type int64_t and B is of type uint64_t. There would be the problems, when you try to assign B to A, while B is greater than std::numeric_limits<int64_t>::max().
@pythonRcpp See for yourself: ideone.com/kD3VCw
Just change long to uint64_t and 'short` to int64_t.
 

« first day (350 days earlier)      last day (2508 days later) »