« first day (1488 days earlier)      last day (1348 days later) » 

6:05 AM
Hi, I asked a question which looks like duplicate but I still dont know the answer because it seems to me that my question is slightly different
0
Q: Initialization list call order C++

Cătălina SîrbuHaving this struct: struct Coordinates { int x; int y; Coordinates(int x_axis, int y_axis) { x = x_axis; y = y_axis; } Coordinates() { std::cout << " CALL " << std::endl; } }; And having this class: class Character { public: bool canS...

The conclusion was that it is UB, so I will take it like this because I didn't find anything related to :)
 
6:31 AM
@CătălinaSîrbu it's pretty simple though, you had it right in your question.
The members get initialized in the order you declare them
if you access a later member then you're reading uninitialized memory
 
it is uninitialized memory, I agree but that means that memory was declared, right ?
 
it was allocated
 
ok this was the step that I was missing. So chronologically speaking, how does this happends?
 
memory for sizeof(Character) is allocated
 
before calling the constructor ? or before starting actually executing the initializing list
 
6:34 AM
then ininitializers are called and then it enters the body of your constructor
this only works because "position" is stored by value, it's still undefined behavior
if "position" was like a vector that allocates its memory dynamically then you would just fail if you tried to access it
 
ok, thank you
yes, that was what I expected, a failure
 
@CătălinaSîrbu if you want to make the order of initialization visible you can abuse the comma operator: ideone.com/GVtKDk
 
It;s new to me, what exactly happens there?
 
(E1, E2) the build in comma operator evaluates E1, then E2 and returns the value of E2
 
Umm, Why doesn't the following construct work?
try {
    Node* newNode = new Node();
    newNode->ivar = ivar;
    return newNode;
}
catch (const std::exception& e) {
    return nullptr;
}
 
6:44 AM
return in a constructor?
 
Uh, no it ain't a ctoe
*ctor
LinkedList::Node* LinkedList::createNode(int ivar) {
    try {
        Node* newNode = new Node();
        newNode->ivar = ivar;
        return newNode;
    }
    catch (const std::exception& e) {
        return nullptr;
    }
}
 
ok, so what's the error?
 
'e': unreferenced local variable
 
that's a warning
catch (const std::exception& ) {

this will fix it
 
Yeah, but in my case all warnings have been treated as errors
 
6:46 AM
if you don't use an argument, don't give it a name
 
Shouldn't it have a name?
Ah! okay
Thanks :)
 
you can also add (void)e in the body, but I prefer just not naming it
 
7:10 AM
Just a note on using printf instead of puts: never, ever do a printf(variable) to print a string. Use puts(variable) or printf("%s', variable). There's a security risk in using a variable format string: if the variable can be written by an attacker they can attack the program by using format strings.
what does he means?
 
What does this mean:

> You have stripped libpthread.so.0 (don't do that).
9
A: GDB debugging warnings

Employed Russian Is this because my executable built without debugging symbols or is the problem about glibc? This has nothing to do with your executable. GDB needs a version of libthread_db.so.1 that matches your libpthread.so.0, and is not finding such version. Probable causes (from most to least probab...

what is striping a shared library?
 
might mean stripping debug symbols or stripping unused symbols maybe?
 
Let me say my problem first.
I have this problem when trying to debug a multithreaded server program on Linux
warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
 
did you install gdb from the distros official packages?
 
I don't know. It wasn't me setting up the server
CentOS
Then I check the pthread version. Let me show you.
[root@richest tools]# ldd battle17_1
	linux-vdso.so.1 (0x00007fffcafff000)
	libsg17base.so => /data/release/sg17/pi/cgi/libsg17base.so (0x00007f390c893000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f390c675000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f390c675000)
[root@richest lib64]# ls -l libpthread*
-rwxr-xr-x 1 root root 143280 Apr  9  2019 libpthread-2.12.so
-rwxr-xr-x 1 root root 806517 Dec  2  2018 libpthread-2.17.so
lrwxrwxrwx 1 root root     18 Jul 29 07:35 libpthread.so.0 -> libpthread-2.17.so
[root@richest lib64]# ls -l libthread_db*
-rwxr-xr-x 1 root root 34488 Apr  9  2019 libthread_db-1.0.so
lrwxrwxrwx 1 root root    19 Jul 29 07:35 libthread_db.so.1 -> libthread_db-1.0.so
 
7:18 AM
well centos has that neat habit of linking libpthread_nonshared.a with newer features linked in statically
same for redhead
 
libpthread.so.0 -> libpthread-2.17.so
So does it mean that my libthread_db.so.1 -> libthread_db-1.0.so need to be libthread_db.so.1 -> libthread_db-2.17.so ?
 
not sure, never had that issue
 
OK, I will try on StackOverflow
Let me check what stripping means at first
 
if I hear shared library and stripping I typically would assume debug symbols, but I'm not sure why you would need debug symbols in this case, I don't think they're in most system libs
 
What happens if you strip an static linked archive?
strip --strip-unneeded libtest.o
The static function and variable get removed, then what's gonna happen? Still works? Or is it the main progam can not call those static function and use the static variable anymore?
Damn forget it. It's not related to my problem. Anyway I didn't strip the shared library.
 
8:26 AM
can you pass to a function parameter list another function that has parameters?
callback function
with parameters?
 
@CătălinaSîrbu onlinegdb.com/HJt1Zq9Wv
 
and if my fuction was
void func_c(int a) {
    printf("%s %d", "Hello Romania ", a);
}
 
then you'd have to modify the typedef of the callback
typedef void(*callback)(int);
 
9:04 AM
Hey guys, can I build a string object with 2 iterators (start and end) to another string object?
std::string str1 = "d4rk4ng31 is hungry";
auto left = str1.begin() + 1;
auto right = str1.end() - 3;
std::string str2(left, right);
 
I think you can
you can try directly in an IDE
template< class InputIt >
basic_string( InputIt first, InputIt last,
              const Allocator& alloc = Allocator() )
and the example below
  {
    char mutable_c_str[] = "another C-style string";
    // string::string(InputIt first, InputIt last)
    std::string s(std::begin(mutable_c_str)+8, std::end(mutable_c_str)-1);
    std::cout << s << '\n'; // "C-style string"
  }
 
Also, can we not use std::find with reverse iterators?
auto comma_pos = std::find(vec.rbegin(), vec.rend(), ',');
 
@d4rk4ng31 You know you can add them? AKA auto AB = A+B;?
 
:| of course
 
...
 
user13088241
9:34 AM
Hi everyone, I am facing some problem with CTest. Can someone please help me? stackoverflow.com/q/63131708/13088241
 
Hi there, I want to make a c++ shared library for python 3.x. I used the command g++ -fPIC -shared -static -o mycppLib.so cpp_source1.cpp
The problem is that the .so file becomes more than 2000 kb in size only when I have used iostream
Is there any optimisation possible so that my file size becomes smaller than 100 kb in size?
Hi there, I am trying to make a C++ shared file for python 3.x. I used the command g++ -fPIC -shared -static -o myCppLib.so cppSource.cpp
The problem is that the .so file is > 2000 kb in size only when I am using iostream.
 
Is that really a problem?
 
Is there any optimisation possible
I want my file to be less than 100 kb
There is only one function I am using and it uses only one cout statement
That's all.
 
9:51 AM
Use printf or {fmt} instead
You can also turn on optimizations with -O2/-O3, or even specifically size optimizations with -Os
 
also turn off exception handling
-fno-exceptions
 
Ok I will give it a try. Thanks! :)
 
10:11 AM
smaller than 100kb, that seems challenging without -nostdlib
even for C
 
-rw-r--r-- 1 root root 252 Jun  5 01:25 /usr/lib/x86_64-linux-gnu/libpthread.so
Why do I have this libpthread.so so small but not a symbol link?
ubuntu@VM-16-7-ubuntu:/usr/lib/x86_64-linux-gnu$ sudo find / -name "libpthread*"
/usr/lib/x86_64-linux-gnu/libpthread.so
/usr/lib/x86_64-linux-gnu/libpthread.a
/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a
/usr/share/gdb/auto-load/lib/x86_64-linux-gnu/libpthread-2.27.so-gdb.py
/lib/x86_64-linux-gnu/libpthread-2.27.so
/lib/x86_64-linux-gnu/libpthread.so.0
fresh, latest ubuntu install
 
10:48 AM
I don't know why it even exists anymore anyways. Non glibc implementations like musl just put it all in libc and create a small dummy libpthread, libm, etc
 
11:21 AM
12 messages moved from Lounge<C++>
 
 
3 hours later…
2:14 PM
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {

    }
};
with this construction if you create a linked list with this, you get a series of nodes
but how would you delete the whole linked list at the end?
 
@Permian loop
 
oh ok
 
unless you allocate the memory for the nodes all together in vector or block
 
if you do it recursively you can easily blow up your stack
 
2:16 PM
so, like Mgetz said, do it in a loop
 
here is another implementation of a linked list in c++
 
Normally linked list implementations use block memory in C
 
how would that linked list (in dpaste) be deleted?
 
also that class isn't exception safe
 
why?
 
2:18 PM
Node* head=new Node(0);
 
and?
 
that runs outside the constructor body, so it happens prior to the object being fully constructed
meaning that if it fails your destructor won't be run.
 
ok thanks
/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */
 
so one of the things you need to be careful about when constructing is to always ensure the object is fully constructed before allocating memory
 
delete obj; ?????????? would that delete the linked list
 
2:19 PM
@Permian no
 
@Permian you don't need to use new here
 
ok
 
not with the default destructor
 
im sorry you are losing me
 
you have to write your own destructor if that's what you want
 
2:20 PM
i only want to delete the object
nothing special
 
why are you trying to delete the list when RAII would work?
also I'd never encourage that sort of usage
 
~MyLinkedList(){
while(head!= nullptr){
auto temp = head->next;
delete head;
head=temp;
}
}
 
i thought you had to delete the list if you allocate it with new
 
nwp
Then don't allocate it with new.
 
@Permian you do, but you shouldn't be using new
The nodes need to be allocated with new
 
2:22 PM
@nwp why have they chosen to do new?
 
because they learned C++ in 1990
 
nwp
Probably because they are used to Java or something.
 
@PeterT so did i
also i knew c++ 03
 
@Permian so C++98
the language has changed considerably in idiom since then
 
yeah omg
so post 2010 do you just put everything in the constructor and it will be automatically destructed?
 
2:27 PM
So for example I'd probably still use std::unique_ptr for each of the nodes. Then in the destructor I'd call reset to delete each node. Or just use a std::vector to hold all the nodes.. probably do the latter.
 
why have a destructor at all?
shouldnt it be automatically deleted
 
@Permian No. you need to use RAII
 
@Mgetz i thought that was RAII
 
@Permian if you're using rule of zero.. it is
 
@Permian if you have a linked-list with unique_ptrs it will get deleted automatically, but with long lists you risk a stack overflow
 
2:29 PM
@Permian RAII is also known as stack based resource management
 
ok cool
/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */
 
e.g. it prefers MyLinkedList foo; instead of MyLinkedList * foo;
 
with this code though, would you call delete obj; ?
 
@Permian no I'd just do MyLinkedList obj; so it has automatic duration
 
ok could you?
@Mgetz that is foreign to me haha
 
nwp
@Permian It's just as foreign as int *ip = new int();. Just use int i; and don't screw around with memory management.
 
@Permian if you write MyLinkedList* obj = new MyLinkedList(); what happens if an exception is thrown on int param_1 = obj->get(index);?
 
ive got the latest c++ primer but you dont pick this stuff up from it
 
so the answer is that obj is leaked
if you write MyLinkedList foo; and then call int param_1 = obj.get(index); and it throws an exception what happens?
 
i have no idea
i still thinking about it
 
2:35 PM
well assuming I didn't mess up the naming the MyLinkedList would be destroyed
and nothing would be leaked
 
@Mgetz why?
 
if I write MyLinkedList foo; then the object is considered to be automatic duration and thus is destroyed when control leaves the scope.
 
i dont get why it leaks just because of exception is thrown?
 
or when it's parent object is destroyed
@Permian because C++ doesn't have garbage collection? There is no way to call delete without using a try block in that case
which isn't needed honestly
 
nwp
@Permian Technically it's not. The reason it's leaked is because the delete obj; which is assumed to come later is not executed because the exception prevents further execution of the function.
 
2:43 PM
@nwp thats it
thanks guys
you have to find the subtree with all the deepest nodes (in a tree)
how can you compare lpair and rpair it the return at the start returns NULL?
 
If I had a situation like this:
void f(int x){...}
void f(const int& x){...}
Would it be possible to call any of the listed functions ? I'm getting ambiguous for every try so far.
 
nwp
The first version is supposed to be int &&x. Though it doesn't really make sense with int.
 
I was just playing with overloading functions and thought of this example. I know it doesn't make sense since all my tries of calling it failed, but I'm still wondering.
 
nwp
If you really really wanted to call one you could declare a function pointer of the appropriate type and assign f to it and then call it. Or cast directly. But that kinda defeats the purpose of function overloading.
 
It wouldn't be possible to do it "the normal way" ?
 
nwp
3:00 PM
I don't think so. You'd have to read through the gory details of function overload resolution and see if somehow you can make one be a better match than the other. Try f(3u); maybe.
 
Thanks for the advice. 3u didn't work sadly :c
 
Hey guys, any idea hows this works?
Getting compiler warnings, here:
#include <stdarg.h>
#include <stdio.h>

// Stops printing at 0
void puts(int argument, int...) {
  va_list arguments;
  va_start(arguments, argument);

  do printf("[%i]", argument);
  while (argument = va_arg(arguments, int));

  va_end(arguments);
}

int main(void) { puts(42, 69, 420, 1337, 0); }
 
nwp
Why did you name it puts?
Also what are the warnings?
int... is not valid syntax.
Once you fix those it seems to run fine.
 
second parameter of va_start not last named argument [-Wvarargs]
Compiler disagrees with you on int..., @nwp
 
nwp
@Lapys But it is ... weird.
 
3:14 PM
@nwp Granted, but how do you make it work with int...?
Yea, just as befuddled as you here
 
nwp
Ah, I see.
Check the notes at the bottom of this.
What apparently happens is that void puts(int argument, int...) is equivalent to void puts(int argument, int, ...). That second int just doesn't have a name, but that's what goes into va_start.
 
Ah
So for all purposes the code is botched and int... is not really an intended feature for use?
 
nwp
If you give it a name it becomes useful.
And maybe there is some niche use where you just want to swallow parameters, but the first one needs to be an int.
 
Ah, so from (int argument, int...) to (int argument...)?
 
stop trying to make C variadic arguments typed, it's not going to happen
 
3:26 PM
Lol
 
nwp
@Lapys That should work in C++98. Not sure why you'd want to though.
Also it's still not a list of ints.
 
3:54 PM
Hello I am a bit confused with the declaration and use of templates
template<typename ForwardIterator>
ForwardIterator copy_data(ForwardIterator a0, I i);
What exactly does this mean ?
this is being declared in the protected section of a header file
is this an example of a function template ?
 
4:11 PM
@gansub yes, that is a function template declaration
and by the very generic type I there and you saying it's protected it seems to be a member-function template of a class template
 
@PeterT Ok thats interesting. Can you explain more what that means exactly - member function template of a class template ?
I understand what a function template is. What is a function template of a class template ?
 
it's like any other member-function template
MyClass<int>::copydata<someIter> would be one example of a fully instantiated function then
 
So when I instantiate the class which itself is a template
I can pass the required argument to that function
template<typename T, typename I, bool aligned=false>
class er_array : public base_array<T,I> {
this is the class template and inside that class template the function template is being declared
 
yeah, if you have an instantiated er_array somewhere you can specialize copy_data on it.
 
@PeterT Does a function template have to belong to a class template ?
or is that not necessary ?
 
4:21 PM
no, you have them as free functions or as members of normal non-template classes too
 
ok
 
I'm sure you've used some like std::max
 
yes
that is a function template right ?
 
yes
 
template<typename T, typename I, bool aligned>
template<typename ForwardIterator>
ForwardIterator er_array<T,I,aligned>::copy_data(ForwardIterator a0, I i) {
in the definition of that function
There is no other definition of ForwardIterator
except what is shown here
OK I got it
ForwardIterator can be an array or vector
@PeterT Thanks for your help. Very useful
 
4:41 PM
What is the typical usage for std::optional, aside from the example given on cppreference (return value that may or may not be there)?
It doesn't look much more useful using std::optional than a std::unique_ptr and checking for nullptr..
 
Can anyone help me with std::string error in SWIG?
 
@eminem Don't ask if you can ask, just ask...
 
I dont know where i need to put %include <std_string.i> in the .i file
no matter where i put it, python wont recognize it
 
@Vaillancourt non exceptional failure or anywhere null might be valid
 
@Vaillancourt Any ideas?
 
4:52 PM
at the end of the day, int *a is always the right choice
 
@VioAriton wut
@Vaillancourt yes but with value semantics and NRVO
 
@Mgetz Do you know how to solve std::string problem with SWIG?
 
@eminem Please don't randomly ping people. If people know they'll respond.
 
You are right, sorry
 
for what it's worth, if you post more information you're more likely to get more responses
 
5:00 PM
@eminem No ;) But a lurker might and that's why you just need to ask what you need :)
@Mgetz Ok, thanks I'll read on that :)
 
 
1 hour later…
ABC
6:09 PM
I'm using pthread_cond_timedwait to create a timer. How can I set this timer in microseconds? Currently I can only set seconds.

Thanks in advance
 
@ABC the timespec should support ns
 
ABC
I know, but when I set ns and sec pthread_cond_timedwait return immediately
pthread_cond_timedwait supports ns?
 
@ABC well if you take the wait then that makes sense
 
ABC
wait I can show you
void *reset_watchdog(void * tortini) {
pthread_mutex_lock(&watchdog_mutex);

reset_watchdog_val = 1;
pthread_cond_signal(&watchdog_cond);
pthread_mutex_unlock(&watchdog_mutex);
}

void * watchdog(void * tortini) {
struct timespec sleep_until = { 0 };
sleep_until.tv_sec = time(NULL) + TIMEOUT;

pthread_mutex_lock(&watchdog_mutex);

// Loop until a time out.
while (!pthread_cond_timedwait(&watchdog_cond, &watchdog_mutex, &sleep_until))
{
if (reset_watchdog_val == 1) {

sleep_until.tv_sec = time(NULL) + TIMEOUT;
If I remove sleep_until.tv_nsec = time(NULL) + 1000*1000; all ok my timer work well included the resetting
when I have that line when I try to reset the timer, so when I send the signal my timer return immediately
 
 
4 hours later…
10:06 PM
template<typename T> void f(const T& t);
bool a = false;
f(a);
In cpp reference it says that "deduced A = const bool". Does that mean we're getting temporary object created and then binding a const reference to it ?
 
why do you think there's a temporary object
the types match, so it can bind to a directly
 
I'm confused with "deduced A = const bool".
We're sending non-const bool type into the function so how do we have const bool as deduced A ?
 
yup
it also says "P = const T&, adjusted to const T" which I don't get either.
That should have been my first question when I think about it...
 
well, the entire thing is quite long, but from what I understand, the entire purpose of this, is to make sure this rule gets satisfied
> If there are multiple parameters, each P/A pair is deduced separately and the deduced template arguments are then combined. If deduction fails or is ambiguous for any P/A pair or if different pairs yield different deduced template arguments, or if any template argument remains neither deduced nor explicitly specified, compilation fails.
 
10:18 PM
I don't get it. We don't have multiple parameters in the given example.
 
ok, disregard what I just wrote
 
Ok sure
 
okay, so
> P = const T&, adjusted to const T
refers to this
> If P is a reference type, the type referred to by P is used for deduction.
 
So that's only for the process of deduction ? I mean, after the deduction the parameter type remains the reference ?
 
yeah, the entire section describes how template type deduction works
 
10:29 PM
Ok I see
 
I'll be honest in that despite me sometimes dabbling in C++ standardese, this section rots my brain
but anyway, the end result of the deduction, is that a function that accepts a reference to const bool gets called
and you pass it a variable of type bool
 
So there are no temp. objects created ?
 
there aren't
 
Ok one more thing which actually led me to ask that
In the example below with pointers
template<typename T> void f(const T*);
int* p;
f(p);
It says: "qualification conversion applies (from int* to const int*)"
The word conversion hooked me apparently.
 
well, in case of a non-template function the same would happen, wouldn't it
 
10:34 PM
yeah I agree with u on that
 
void f(const int* p); int* p = nullptr; f(p);
 
and that was my original reasoning tbh
But it says that some kind of conversion happens, right ?
Doesn't that mean that we're getting temp. object here ?
 
consider that a function can accept a reference, and then take an address to the referred object with an & operator inside, and store it somewhere, so the distinction between creating a temporary and not creating a temporary is significant
T* p = new T(); // dynamic storage duration, so the object won't die before deleting it
void f(const T& t); // function accepts a const reference
f(*p); // here it's significant whether a temporary is created, let's say that f stores an address...
g(); // and g uses it - creating and not creating affects program's correctness
OTOH, I don't know whether conversion from int* to const int* creates a temporary object because the function accepts by value either way
 
@milleniumbug very good point, I wish I have thought of that
And regarding the second example with pointers
When I added the following line inside the function:
template<typename T>
void f(const T* x){
std::cout << x;
};
0 printed out on the console
Does that mean the temp. object was actually created ?
Since x should be an adress
 
?
when you do std::cout << x;, then you print out what the pointer is pointing to
 
10:48 PM
Nevermind
0 printed out cuz I hadn't initialized the pointer
But theoretically, if there was actually a temp. object std::cout << x should print out something weird or not print anything at all, right ?
cuz rvalues doesn't have addresses
 
no
the pointer is a thing on its own
given an void f(const int* p);, you can't write f(&something_that_is_an_rvalue) anyway
 
well
you can't even have a pointer pointing to a temp., can you ?
 
exactly
 
it's late, forgive me
and ty for help
 
the summary is that references are weird things with their own rules, and aren't objects themselves
but the pointers are
so, when I write f(&x); then &x itself is an rvalue, because it's an expression with no name
but the entire thing is passed by value to the function
just like you would write g(42 + 5)
 
10:55 PM
yea I know these stuff, it's just late I guess xd
 
where g is void g(int x);
 
now I'm legally confused
void f(const int& x){
std::cout << &x;
};

int main(){
f(3);
return 0;
}
Why does this work ? .-.
I think I get it. If you extend the lifetime of a temp. object, then you are able to get his adress.
Am I right ?
 

« first day (1488 days earlier)      last day (1348 days later) »