« first day (1759 days earlier)      last day (1085 days later) » 

8:31 AM
https://stackoverflow.com/questions/5248915/execution-time-of-c-program

I'm using this method to measure one of my functions. Say time_elapsed is x. How can I interpet x to real world time in seconds?
What I mean is, what x actually represents here?
 
8:46 AM
@Eminem I don't even understand the question, if you're measuring milliseconds then you're measuring milliseconds.
if you mean the result of clock itself then it's only meaningful as a difference between two calls and relative to CLOCKS_PER_SEC
 
 
3 hours later…
11:46 AM
4 messages moved from Lounge<C++>
 
 
8 hours later…
7:17 PM
Hello, is my code ok? std::tuple<long, const char*> extract(const char* s) { long l = std::strtol(s, const_cast<char**>(&s), 10); return std::make_tuple(l, s); } I'm specifically asking about the const-cast. Also, what if I were to use plain C? I see the first two arguments have restrict but don't quite understand how it would apply here. The pointers point to different types so they already can't alias, which is even true in my code, no? I know I should use charconv probably ;-)
 
7:36 PM
@purefanatic no that's not ok.
 
Why not?
 
The str_end argument is an output parameter so must be writable (and will be written if not null). However, since you apparently don't error-check, you can just pass nullptr:
> The function sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is a null pointer, it is ignored.
 
ok ok, then let's put an assert(s != nullptr); in there. Would it then be ok?
 
If you used plain C, you'd pass NULL instead of nullptr. And yes I consider the fact that it required str_end to be a pointer to non-const char* an unfortunate interface gotcha
@purefanatic That's unrelated. The str_end is so that you can do further parsing/error checking. What if the input is "123XYZ". It depends on your application whether that's ok input
 
@sehe Well you're right, I can't really guarantee that I need a non-temporary as the argument, can I? But I think the assert makes it better in that it at least states my requirement of a non-null pointer explicitly.
 
7:42 PM
Yes
 
Anyways, I think we're more or less on the same page on that one. I also guessed that this non-const pointer is some kind of relic. But what about the restricts that were added in C11? I don't quite understand why they would add anything useful.
eh, C99 according to cppreference.com
 
Yeah. That's about aliasing. It's deep into compiler optimizations. I wouldn't worry about it, except for functions that take several pointers (often it implies they must point to overlapping memory regions)
In the C programming language, restrict is a keyword that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, only the pointer itself or a value directly derived from it (such as pointer + 1) will be used to access the object to which it points. restrict limits the effects of pointer aliasing, aiding optimizations. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior. The use of this type qualifier allows C code to achieve...
Isn't bad, on first glance
 
Yes, I skimmed that article, too. It absolutely makes sense for memcpy() where the compiler can't know that src and dst can't overlap by looking at the signature otherwise. But in strtol I would claim the two pointers can't overlap regardless.
 
8:02 PM
Maybe I'll put an actual question on stack overflow, seems like my type of rather superfluous fact-seeking :)
 
8:23 PM
@sehe Probably just a typo, but to be clear, it's usually the opposite-that they point to non-overlapping memory regions. The basic idea is to assure the compiler it can keep things in registers. Without restrict, keeping things in registers gets tough, because a write via one pointer can modify data seen by the other (but if it had that data in a register, the write wouldn't modify it).
Silly side-note: during the C89 standardization process, they actually had a better name for it: noalias. Unfortunately, the proposal was badly written, to the point that it prompted the one time the Dennis Richie got involved in the process--he wrote a letter titled "noalias must go", which tainted everything associated with it to the point that nobody even considered calling it noalias when they wrote a better proposal later.
 
8:43 PM
@JerryCoffin Oops. I accidentally a word. "must not". I edit too much.
@purefanatic It's about writing indirection, and I suppose that a lot of restrict may be just added for consistency
@JerryCoffin ouch. History
 
9:20 PM
@sehe Yeah seems this might be the case. But then again adding such stuff only confuses people like me ;-)
 
@sehe Yeah, I figured.
@sehe Yup. Such is life sometimes, I guess.
 
@JerryCoffin interesting stuff! I only stumbled upon VC++'s declspec(noalias) some time ago which is probably entirely different
 
@purefanatic Yes, it's quite different. Basically says if you pass a pointer to the function, it might modify what the pointer points at--but it won't "chase" pointers from there, and modify things that are only pointed at indirectly.
 
 
2 hours later…
11:13 PM
I really got shocked and amazed at the same time. I just knew that the number of array can be dynamically defined in the current c++. In the past, it must be a compile-time constant. Oh my ghost!!!!!!!
#include <iostream>

using namespace std;

int main()
{
    int n;
    cout << "enter the number of elements:";
    cin >> n;
    cout << "The number of elements: " << n << endl;

    int data[n];

    return 0;
}
@PeterT
malloc or new int[n] is no longer needed.
 

« first day (1759 days earlier)      last day (1085 days later) »