« first day (730 days earlier)      last day (2128 days later) » 

1:27 AM
Hello question, that may or may not be easily answered. If it is not easily answered I will just go ahead and actually post the question. We are trying to use a Win32 application with C++ in combination with WinSocket? It is because the recv() function causes the program to halt that I tried to making the client always send a message, saying whether or not a legitimate question is being sent. However, since this must always be done we tried inserting it into a while loop, but the while loop halts the program entirely. Is there a location within the win32 gui application where it would be po
 
 
2 hours later…
3:28 AM
@El8tedN8te The simplest advice is that you don't use recv directly. If you're mostly a Windows guy who happens to use C++, consider using WSARecv. If you're mostly a C++ guy who happens to be using Windows, consider ASIO. Depending on what sorts of things you're doing over sockets, you might also want to consider something like the C++ REST SDK.
 
I am reading C++ Primer and see this:
”If you use a smart pointer to manage a resource other than memory
allocated by new, remember to pass a deleter”.

What does it want to say? Can smart pointer manage static memory object, I don't think so.
 
@Rick Static memory, no. File handles, database connections, network connections, etc., yes. It's saying that when you use it to manage those you need to pass a deleter that closes the file, database connection, network connection, or whatever.
 
3:44 AM
Yes, it's taking a network connection as an example.
 
4:29 AM
@JerryCoffin Sir, what does it mean by saying "other than memory allocated by new"? Aren't all dynamic memory allocated by using new keyword?
 
@Rick locals (automatic storage) are sort of dynamic, and not normally allocated by new.
 
OK. thanks
:D
 
 
3 hours later…
7:57 AM
are the operators ++, --, - and + on pointers overloaded or special template functions to provide pointer arithmetic ?
 
8:13 AM
pointer arithmetic exists in C as well, so it should be a core language feature in C++ rather than some template gibberish
 
those are technically built-in
 
but what means that exactly ?
 
it means that in C++ even without templates (you can turn them off with a compiler switch) and in plain C you still can use this feature
 
Ron
8:31 AM
How does std::generate know when to stop?
Does it solely rely on setting the last iterator in container's constructor? The possible implementation suggests that it does.
 
o.k. thx @login_not_failed and @ratchetfreak
 
 
3 hours later…
11:26 AM
#include<stdio.h>

int main(void) {
    // print  EOFdirectly
    printf("%c", EOF);

    // print EOF after input
    char c = getchar();
    printf("%c", c);
    return 0;
}
in the first case I see the EOF character
but in case two I see nothing
in case 2 I press Ctrl-D (windows) as explained here. You may run this code here. Could anyone please tell me what the issue here is?
 
EOF is -1 and has type int
it's why getchar returns an int to have -1 as end of stream sentinal
usefulness of having a sentinel like that over a separate function to check end of stream is debatable
 
if it returns -1, how come there is no compilation error when I do char b = EOF; I would expect char to only be able to hold positive values...okay, I just noticed I can do char x = negative_integer; without any problem :/
why's that?
reading
 
11:43 AM
char is signed
 
@ratchetfreak most of the time
 
12:05 PM
yea, it could be signed en.cppreference.com/w/cpp/types/numeric_limits/is_signed, but if it's not, -1 becomes std::numeric_limits<unsigned char>::max()
 
@login_not_failed strictly speaking it's ~static_cast<unsigned char>(0)
 
that's how it's implemented, but the interface looks like that :)
 
@login_not_failed actually there is a functional difference, -1 is an int where as what I did is guaranteed to be at char size given it's unsigned the compiler won't sign extend (again important) this really bites people that make these assumptions on platforms that don't support native char size operations (like the alpha).
 
for me "where as what I did" implies that there are different ways, what are they? of course, you have to use unsigned types to juggle bits correctly, and in this case you have to use ~, but I don't see how it can be done differently
 
12:32 PM
@ratchetfreak then when I do printf("%c", EOF); why doesn't it print -1?
nvm
I got it
-1 is represented as 1111 1111 (or more bits)
that's a character with an ascii code 2^bits - 1
 
12:44 PM
@ratchetfreak Streams do provide a separate function to check for the end of the stream as well. But let's turn it around for a moment: right now, if you try to read when you're already at the end of the stream, it returns a value that can be distinguished from anything that could have actually come from the stream itself. If you don't like that idea, what would you prefer? That it return something that could have come from the stream itself?
@GaurangTandon When you're reading with getchar and company, you should not read directly into a char, and then test for EOF. To get correct behavior, you always read into an int, test for EOF, and then only if you haven't gotten EOF, store it in a string (or whatever).
 
 
1 hour later…
1:49 PM
@JerryCoffin K&R C is replete with examples of while((c=getchar()) != EOF) though :(
...so I guess that's wrong, isn't it?
 
2:16 PM
and
3 hours ago, by Gaurang Tandon
but in case two I see nothing
what was the reason for this? ---^
 
@GaurangTandon No, it's not wrong. If you look, you'll notice that in such cases, they've defined c as an int, not a char.
 
@JerryCoffin ahh, clever! the variable naming got me :P
"When you're reading with getchar and company, you should not read directly into a char, and then test for EOF." Why, though? @JerryCoffin
 
because EOF will be different from (char)0xff
 
2:32 PM
why? that SO answer says EOF=-1 but is stored in memory as 255, hence we can store it in char datatype. and (char)0xff should also evaluate to same result
they should be the same?
?
 
2:54 PM
this means that if you read 255 bytes, you'll get the same result as getting no bytes
take a look at a demo: wandbox.org/permlink/F5onLBJLVg6RvA8S first value is int value -1 interpreted as unsigned char value, the second one is the maximum amount a unsigned char type can hold; I casted them to int just to display their values as numbers
 
3:26 PM
@GaurangTandon Let me try to answer this at a little more abstract level. A stream can contain any value that can fit into a char. getchar returns an int specifically so that it can return two classes of things: 1) a char value that came from the stream itself, and 2) at least one other value that could not possibly have been content from the stream. If you take that value and immediately put it into a char, what you have then (by definition) is something that will fit in a char.
You can no longer identify whether it's one of the things that came from the stream, or that other value that couldn't have come from the stream.
 
3:38 PM
Hi
can we debug through eclipse if SSH port is disabled?
 
I have a variable which is set only during initialization and de-initialization. The worker threads look into this variable and decide whether they should run or not. I have tried achieving this without a lock and it works fine but when the variable is being changed, it induces a data race which is technically undefined behaviour (?). Every worker thread holds the lock for a very short time: a simple loop running 64 times with two array checks.
What are my options?
Not using a lock seems to work but it is UB, hence bad?
Using a regular lockjust bloats the code to handle a situation which would occur during startup and shutdown, i.e. once every few days.
Using a read-write lock seems to be overkill since the lock is held for a very short duration.
 
What do you mean it bloats the code... what library are you using to handle this?
 
Unfortunately, pthreads in C++. (I don't have control over this)
 
Oh, I see.
Use the locking mechanism. It's safer and it's a best practice.
You can always write more code to encapsulate and hide the ugliness of pthreads.
 
It's useless for most of the time though. It only blocks threads for no reason and does no good except to prevent UB during startup and shutdown.
 
3:52 PM
It doesn't matter. Sometimes, good code is written just to handle those rare occasions where an event might happen.
 
an atomic variable will avoid the UB, though there is std::call_once which will do most of what you need
 
I think pthreads does not provide support for atomic variables.
(I am not allowed to use C++ multithreading library)
 
pthread_once then
 
What's the logic? I have an init function which creates the worker threads which must wait until the entire initialization procedure completes. During uninitialization, the initialization variable is set to zero which should make the threads exit gracefully. How do I use pthread_once here?
 
4:09 PM
ah no then you have to use a atomic variable, check your compiler docs to see how to get at them
 
I am not allowed to use that either. (if you were referring to the compiler builtins)
 
then you are ****ed
if it's a homework assignment of some kind just do the lock thing and be done with it
 
I am working on someone's code who does not know C++ but the libraries (dlib & opencv; I think they don't have a C interface?) being used are C++ which is why a C++ compiler is being used.
 
nwp
@Yashas They are in the standard library, not built into the compiler.
 
@nwp C standard library?
 
nwp
4:15 PM
C11 has some too.
Why is std::atomic banned?
 
because of the C guys...
 
nwp
@Yashas Fight the C guys. Show them the problem and the solution in C++. They have to use C++ anyways. If they still say they don't want to use it because it's not in C then you should find some way to protest against that decision.
 
 
4 hours later…
8:27 PM
yo
I have
QVector<myObject> mVec
QMap<QString,myObject> mMap

if I do say
mVec.push_back(ob)
mMap[ob.id] = mVec.last()

Then if I modify say mMap[id].setName(name) will the mVec obj also update or they were made a copies?
how can I synchronize the 2 containers?
fells to me like the only way to do it is to use QVector<myObject*>
 
Unfamiliar with Qt containers, but assuming it's somewhat like std::vector<myObject> and std::map<std::string, myObject>, you have some options.
If you won't be resizing the vector, you could do QMap<QString, myObject*>. You could reverse it and do QVector<myObject*>
You could do QMap<QString, std::size_t> (index into the vector)
I don't like the QVector<myObject*> route that much, as you lose locality guarantees.
Whatever you do, I'd suggest wrapping it in its own type
 
8:43 PM
Don't store data in two places. Typical Qt code uses map lookups for everything, so get rid of mVec. But also most QObjects have an associated parent, so containers shouldn't own the objects but rather the pointers.
You probably want: ` QMap<QString,myObject*> mMap ` especially if its a map of widgets, now typically layouts, are maps of widgets, so mMap is probably redundant compared to just accessing the parent container.
 
9:28 PM
hmm
myObject is just a struct
im using qt containers because app is qt based
but its not a must
I was just doing some tests and spend last 4h debuging why name of my struct did not save properly
just to realize at the end that I was setting name on map and saving from list
I keep 2 lists because I did some performance tests and map is very very slow comparing to vector. So I though that I could use vector for speed intensive tests and map for wild finding
thanks for the idea with *!
will try that
 

« first day (730 days earlier)      last day (2128 days later) »