« first day (1474 days earlier)      last day (1595 days later) » 

user13168561
03:45
Heya!
user13168561
How're you guys doing?
user13168561
Umm... I was solving a hacerrank challenge and passed 4 cases but failed in 5th
user13168561
And I don't know why...
user13168561
And here's my solution: #include <stdio.h>
#include <iostream>
using namespace std;

void update(int &a,int &b)
{
int x, y;
x = a + b;
y = b - a;
a = x;
b = y;
}

int main() {
int a, b;
cin >> a >> b;
int *pa = &a;
int *pb = &b;
update(*pa, *pb);
cout << a << endl << b;

return 0;
}
03:49
whut? the challenge says the signature is with int*, not with int& ?
also, you didn't take the absolute of the difference
if a is larger than b then y would be negative with your solution
user13168561
Yeah! You are right
user13168561
I just put abs(y); in the solution and it worked
user13168561
Sometimes I miss simple things too...
user13168561
Thanks for your quick response anyway :)
08:55
Are there any advantages for using a static library .a , .lib over a dynamic library .dll, .so?
09:33
@Rick easy deployment
09:48
...
Ok
10:43
I have created an allocator for arbitrary type T, which I supply in order to override the built-in STL allocator. However, I would like to actually pass some run-time parameters (non-scalar) to the allocator of arbitrary type T when I create it. Is this possible to do?
@Micrified You have to pass an instance of your allocator to the object being constructed, and that instance can receive those parameters in the ctor.
So I would need to subclass every class I want to use with this allocator in order to support these additional parameters?
I think I'm reading it wrong. You're saying I can pass a pointer as a template argument.
No.
You pass the type of the allocator as a template argument, but you can also pass the value of the allocator as the ctor argument.
Look at the constructors. Either the allocator instance is being passed directly, or default-constructed.
Okay. Excellent, then I understand
Thank you!
11:42
__MAIN__, what do you all think?
11:57
Greetings fellow devs

I'm having a kinda weird problem with compilation of a customer project on an old solaris 10 machine.
Its written in C++98 and compiled with the Sun C++ 5.5 compiler so its quite old.
Problem is, everything works in debug mode, that is with option -g but as soon as I compile in release
mode without -g but with -O2, the linking fails:
undefined symbol                                            first referenced in file
void BaseColumnInfo::setMapped(bool)                        solaris/TableConfigManagerr.o
void AggregatorDispatcher::printData(char,TableAggregator*) solaris/vcdumpr.o
Any ideas?
no idea, probably some macro conditional compile
Good day, I'm stress testing malloc allocations vs my custom memory pool, which preallocates a big chunk of memory and then I write on it sequentially. I expected sequential writes to be more efficient than N malloc allocations and then writing on each malloc-allocated pointer in sequence.. But for some reason they're almost the same and on average the malloc version is slightly faster.
I'm not clear on why, can it be that the OS handles pages and cache better using malloc than manually using a contiguous memory block?
@SergioBasurco because it does use a contiguous memory block in many cases
12:16
Yep I figured, I'm testing 100 times each allocator. I just noticed the order of the test matters, the second allocator being tested is always a bit faster on the writes.
@SergioBasurco You'll need to test a lot more than 100 times
that's not statistically stable
You also should understand how each one works internally because many keep a cache of available pages
you also need to quantify what sort of allocation size you're looking for
and locality
Games use Pool allocators for example to keep track of what is allocated, but also to ensure that it's all colocated
@Mgetz What would be considered statistically stable? Yep I'm actually basing my implementations on Jason Gregory's book "Game Engine Architecture". My allocator just assigns contiguous memory by increasing a pointer. I'll read on locality and colocation, I'm guessing you just mean that it's actually contigous (?) Thanks
bad, there's one standard entry point and it's main
stuff like DllMain or WinMain or custom entry points can be dealt with in the platform specific way they're defined
@SergioBasurco I'd have to go grab a statistics book, but it depends on the deviation you want
you can just add main and call it from you custom entry point
12:30
100 samples probably won't tell you crap because the noise of the system itself will likely override any data
@KevinKuegler you can just dump the symbols in the debug version object files to see where they are in
But what of interfaces that don't know what the entry point is but want to expose their features nonetheless, like:
and then compare with the release object files to see if the symbols are in there or with a different name
class Object { friend __MAIN__;
    private:
    void call(void) { /* ... */ }
};

int main(void) { Object().call(); }
you make an intermediate function, friend that and call it from main
12:33
I guess that works..
As a quickfix I solved it by specifying -g0 to allow for inline optimizations.
Still this is very weird as the (stripped) debug information should not cause such behaviour
8 messages moved from Lounge<C++>
 
2 hours later…
15:07
hello ! could you please help me understand two approaches when talking about writing code in multiple files?
1st question: Why would anyone ever write the functions below the main function and then declare them at the beggining ?
2nd question: If I have a project, and 2 cpp files (one containing a function initialization and the other one containing main function along with the call of the anterior mentioned function. Is it better to let the linker link and just declare the prototype? Or is it better to #include the cpp file?
3rd question. How about writing the declarations in header files and the definitions in cpp and then include the header? I can't make the difference between approach 2 and 3
nwp
nwp
1st I have no clue. It makes no sense to me either. I guess it was introduced by example code explaining declarations and stuck.
1. because having your entry point at the start of the file helps makes sense of what is going on and lets you keep your high level stuff more easily visible
the declaration is because C compilers used to require them
nwp
nwp
2nd You're not supposed to include .cpp files. If you have another file that needs to use the function and include the .cpp file a second time you get errors about multiple definitions. You're supposed to put declarations in headers and definitions in .cpp files and only include headers.
I guess that also answers 3.
but there is nothign stopping you from #including cpp files as long as each definition is only ever in a single translation unit
so what I kind of get wrong is why it works including header files and also works without including them (just by declaring the prototype)
15:15
including cpp files just breaks most build-systems. Simple Makefiles, globbing patterns etc.
nwp
nwp
Arguably the main point of headers is to not having to re-declare the same functions multiple times which gets annoying when updating a function.
But you could do it manually.
because they assume the convention that header files are for inclusion and cpp files for translation units
but why in my cpp i have to include my header?
nwp
nwp
You don't have to. You can also type out the content manually.
so that you don't have to repeat the forward declarations every time you want to use a function
if you use the function from a.cpp in b.cpp and c.cpp you would have to repeat the declaration
15:19
Is there any page I can learn to write a project?
to create in the right way a project?
if only there was a right way :P then we wouldn't have 20+ build systems
then I want to assure this is right
so this is my header file dec.h
i do not need any further includes in this file
looks normal to me
and this is my .cpp file
#include effectively copy pastes the content of the file in place of the #include declaration
15:24
as @ratchetfreak said, it is actually like this my cpp source file
so now comes my question. What about next? What do I include in main and why
you include "dec.h" in main and then just call add there?
but why do I include the header in my cpp (not in main, in the definition cpp file if I also include it in main)
for simple functions you don't have to, but if stuff gets more complex you have structs there and type definitions
in the C days people probably did it for simple functions because if you had wrong arguments you would get a compile error
so my main would look like this ?
#include <iostream>

int add(int a, int b);

using namespace std;

int main()
{

    return 0;
}
right
15:28
yes, for the moment I see as a redundant step including the header in the cpp file (maybe because of the simplicity of the code)
I though that was the linker's job
then extend the example for it to make sense and add a struct type that the function takes as an argument
Oh so for the function does not make sene having both declaration and definition in the same file, but for structures it has
well I think it's stilluseful for functions, but it's not necessary for the program to compile and link
15:50
can i ask a questions about string transformations, more problem related than c++?
16:15
is there any way having in main() a variable called "int sum" and inside a function another variable aclled sum , to specify that we want to use the external sum ?
the sum which belongs to main?
16:46
@CătălinaSîrbu nah, you're best off just passing a reference or pointer to that "sum" in main into the function as a parameter
or if it's relevant to multiple operations you can put "sum" as a data-member into a class/struct and access it that way from both main and from the member-functions of that class
 
2 hours later…
18:54
who deletes variables when they exit their scope ?
I mean when you allocate memory with new and delete with delete, you do the job. But when it is automatically handled by _____ who does that ?
The compiler inserts the code for the destructor automatically on every exit of that scope
the compiler still works even after compilation ?
so if you reach the end, if you return, if you break out, the compiler will insert the code to call the desctructor.

The actual memory is usually just allocated on the stack, so allocating or deallocating for those variables is just moving the stack pointer
no the compiler inserts the code there while compiling the code
but how is it able to know what happens at runtime?
it doesn't need to, every exit out of the scope is defined statically (minus exceptions, but "goto exception handling" can be generated statically)
look here: gcc.godbolt.org/z/1x7fvo you'll see "call C::~C" in the generated code for main
Ron
Ron
19:04
@JerryCoffin @nwp @PeterT @milleniumbug Guys, I just wanted to express my gratitude for all your help throughout the years. It helped me put together a small C++ book for beginning levels. Thanks again.
Ron
Ron
Here is the Table of Contents. I would like to know what you think.
@Mgetz Forgot to mention you too.
 
2 hours later…
20:53
How wholesome, @Ron
21:04
congratulation on getting a book done. I don't think I'd ever have the patience. TOC looks comprehensive for an introduction.

« first day (1474 days earlier)      last day (1595 days later) »