« first day (227 days earlier)      last day (2628 days later) » 
00:00 - 15:0015:00 - 00:00

12:06 AM
do you actually use getter/setter functions the huge majority of the time when dealing with objects?
(@nwp is it ok to ping you like that?)
 
nwp
@towc no
@towc yes (but I might not answer)
 
so you do actually use public properties?
@nwp ok, thanks 😀
(I understand you don't owe me anything and all that, dw)
do you have C++ projects that actually have a use, but are fairly easy to follow?
 
nwp
@towc not really, usually you call member functions on objects that do stuff beyond getting and setting
 
oh, fair enough
each object is responsible for itself, right?
 
nwp
@towc you are supposed to read a book
I don't actually have anything online worth learning from
 
12:12 AM
well, that's one way
I remember trying to read the primer quite some time ago
it was inclusive and perfect and all, really helpful
but I guess I'm still experimenting on what is the best way for me to learn stuff like this
 
12:26 AM
and the #ifndef stuff is also actually very rarely used?
 
nwp
@towc it tends to be in every header because of include guards and in some places where you use platform/compiler-dependent things to implement them platform-independently, but usually I use libraries that do that for me
 
oh
example name of library?
 
nwp
Qt
 
cheers
 
 
4 hours later…
4:19 AM
HI
i have some issue with BigInt c++ file
now it is working fine in 32Bit os but not workiing in 64bit.
can anyone help me.
 
Maybe, you should post the error and somebody might pass by and answer
 
Sure
void InvMod(BigInt &n, BigInt &ret)
{
//BigInt ret;
mpModInv(ret.data,data,n.data,MAX_DIG_LEN);

//return ret;
}
in this function its going infinite loop somthing
 
 
2 hours later…
6:33 AM
@NagRaj SSCCE please
 
in Game Development on The Stack Exchange Network Chat, 12 mins ago, by ramsay
Hello I am quite comfortable with programming (I have practiced from competitive programming websites) and now I want to learn a bit of game dev. And I read about some libraries like sdl, opengl but How do you start? I know all linking stuff etc etc.. But do you read whole source code to start using library??
in Game Development on The Stack Exchange Network Chat, 7 mins ago, by ramsay
I know there are many tutorials on youtube.. But for example a new library came up today how does a guy understand it?
@ramsay is my friend and he can't post it here
actually I haven't use any external libraries, so far :(
 
@DenisKa use unity
 
6:50 AM
so you don't need opengl or something or even coding for unity? @milleniumbug
 
what? you absolutely need programming skills there
but no, don't learn OpenGL if your goal is to write a game
 
so I should read all documentation stuff of unity to understand it?
 
what "all documentation"? Well, if you learn about the thing then you learn from documentation, tutorials, API references and so on
few people are able to use a thing "on-the-go", even text editors or movie editing software
 
so you don't need to read their source code(not unity i mean). Like for other libraries you don't need source code, just docs, tuts and api refs?
 
no, people don't normally read the source code of libraries to use them
If a library is not open source, that's not even possible
 
7:02 AM
so learning about a library is just like a new programming language(with same syntax as old one?)
 
7:59 AM
Do I need to use a unique_ptr for the following?
std::vector <Interface *> InterfaceList;
There are changed rarely
10-20 times each day
I possibly can't make a memory leak because the object is deleted and created only once..
but I do this dirty thing to check if there is an interface at a specific index
(InterfaceList[scriptKey] != nullptr)
around 1000 times per second
 
One of the problem that unique pointer "solves" is resource sharing. For example,
auto another_list = InterfaceList ;
 
sometimes 1,000,000 per second
I am not going to copy the Interface nor the InterfaceList
the interface stuff is hidden inside a box. The box provides a neat API for other parts of the code.
 
It is possible to write code in a non-paradigmatic fashion, but its also not a good idea :-)
 
all the code related to interface is ^
other parts of the program only call the functions in ^
they do not touch the code directly
 
The correct way is to do something like :
std::vector <Interface>
 
8:05 AM
I can't do that because I am using the vector index as an id for each interface
so when I need to delete an object, I shouldn't remove it from the vector
using Interface* allows me to set it to nullptr when an index is unused
 
You could use a zombie objects. For example, interface.enable(false)
 
I don't like it lol
 
Alternatively you can write complicated, non-paradigmatic logic. It will also run slower because the pointers go to non-continuous memory locations.
 
I like to stuff related things in one file and never open it
I am already having issues that the interface code does not completely abstract the interface
It doesn't let me sleep
 
8:16 AM
//Interfaces are added/removed on server startup/shutdown
//Interfaces added/removed at runtime needn't be thought about as nobody loads too many AMX excutables at once
//Therefore, we will avoid reusing of objects for the sake of readablity. (performance gain is negligible)
is that stupid?
 
So, nothing wrong with having interfaces. Interfaces might contain pointers to big immovable objects, but its weird that they are themselves pointers.
 
The outside of the interface implementation does not know anything about interface. The outside world talks to the interface using unique numbers assigned to each interface. The pointer stuff is hidden in interface.cpp. So is it really bad?
I also use the nasty new and delete operators.
 
Worse than doing it The Right Wayâ„¢
 
Aw.
What should I do to correct it?
 
std::vector<Interface>
 
8:31 AM
:|
 
:/
:–
:\
:|
:/
this works better with ncurses
 
@Mikhail other than that?
 
One way to improve your code is to see the results of a static analysis tool, for example, ReSharper has something similar to grammar check. Its also free for students.
Also PVS Studio resets the click count on major updates, effectively make it free :-)
 
why is ++iterator more effcient than iterator++?
 
nwp
8:50 AM
@YashasSamaga ++iterator returns the iterator itself, whereas iterator++ returns the iterator before the increment, so there needs to be a copy
 
the ++ operator is not inlined?
 
nwp
compilers cannot always optimize iterator++ into ++iterator when you don't care about the returnvalue
 
I use iterator++ in these situations
while(itr != end)
{
itr++;
}
 
nwp
it probably doesn't matter
 
9:05 AM
char [] vs string inside an object
what should I use?
using char[] will cause memory to be allocated when the object is being created
if I use string, wouldn't there be two calls for memory allocation?
 
9:29 AM
what "two calls"
I'm assuming you have a fixed length string or otherwise we wouldn't have that discussion
 
I have a fixed length string.
How is memory allocated if you have a string inside a class?
 
then you can either count on std::string's SSO
or have your own ShortString<N> class
 
If I use new to create an object of the class, that new will allocate memory to hold the class. When the constructor is called, the string's internal allocator will do another new, right?
or use char []? :P
 
std::string's SSO can easily avoid dynamic allocation if your string is 20 bytes or less
 
using char [] forces me to use strcmp which I hate very much
The string size is 32
 
nwp
9:35 AM
hmm, std::array<char, 32> together with std::string_view when needed could avoid the dynamic memory allocation and the C string interface, but it sounds like premature optimization to me
 
if I use a C string, people will start shouting
if I use std::array to store a string, some people will start scolding for not using std::string -.-
 
1
Q: Short string class

nullw0rmOn one of the projects I'm working on, we have many objects that store (small) strings, loaded from database. I know std::string's have small string optimization (SSO), but the overhead, in my opinion, is too big; most strings are < 16 characters, and on my platform sizeof(std::string) is 32, was...

 
nwp
just use std::string until you can prove that avoiding dynamic memory allocation provides at least x% performance benefit
don't forget about development overhead
 
in most cases, I won't have more than 20 characters
the maximum limit is 32
so it shouldn't matter :D
 
Sep 11 '16 at 12:21, by milleniumbug
No one likes C strings
 
9:42 AM
I make too many premature optimizations
is it bad to mix time.h and chrono? :/
 
hello?
hi guys, I was wondering how the memory is allocated in a class that doesn't have data members
so a class the does only have methods
 
sizeof an empty struct/class is 1
 
The size of an empty class is not zero
 
ok but I mean
 
it has to do with getting distinct pointer to each separate instance of it
 
9:50 AM
data members would be put in the stack
 
the compiler can do all kinds of empty class optimizations
 
is there memory allocated or used in general (heap/stack) when I have only functions?
 
if it's used as a base class then there is not extra size growth (aka the empty base class optimization)
when malloced it gets rounded up to the smallest allocation the allocator can handle (typically 16 or 32 bytes)
on the stack it doesn't really matter because adding 20 or 21 to the stack pointer doesn't make a real difference
 
nwp
10:11 AM
@user8469759 yes. Consider using a namespace instead.
 
I basically have an abstract class
with methods
and then I derivate such class
 
nwp
@user8469759 then it doesn't matter
 
what doesn't matteR?
 
nwp
the size of the abstract class. You cannot instantiate it because it is abstract and if you instantiate a derived class the empty base class optimization kicks in and removes the 1 byte overhead.
 
How about the subclasses
?
(still assume no data members)
I actually have some in some subclasses
but anyway it's just for general knowledge
 
nwp
10:20 AM
if none of your classes that form the object have any data members you may have wasted a byte
I would not worry about that
 
But the thing of having an abstract class with no datamembers and then subclassing make sense right?
because of the strategy pattern
 
nwp
for general knowledge it may be interesting to think about what would happen if objects didn't have at least size 1, especially how an array of those objects would work and how one would iterate over them
 
or the state machine pattern or some virtual constructor etc
 
@YashasSamaga unfortunately <chrono> can't replace <ctime> because there are still some APIs which are missing
for example, no localtime equivalent AFAIR
 
but you can't create array of "objects" I have necessarily to instantiate an array of pointers
right?
 
nwp
10:24 AM
@user8469759 virtual constructors don't make much sense
 
why not?
 
because virtual dispatches on an object
there is no object until a constructor creates it
"virtual constructor" is literally an oxymoron
 
I mean't the pattern
meant*
 
nwp
@user8469759 sure you can, though they will all have the same type
 
std::vector<std::unique_ptr<YourBaseClass>> and stuff
 
10:31 AM
with virtual methods you need at least a pointer for the vtable
so if you have class Foo{virtual ~Foo(){}}; class Bar:Foo{} then sizeof(Bar) >= sizeof(void*)
 
auto amx_interface = std::find(InterfaceList.begin(), InterfaceList.end(), [](Interface& intrf) { return intrf.empty(); });
is that bad?
 
just asking
I don't know many of the technical details of C++
 
non-const reference feels bad
 
10:41 AM
since you're not modifying the values
oh, and it's std::find_if, not std::find
 
done
made empty const and the lambda accepts const Interface&
 
unless empty is not const qualified (even though it should be)
 
most of the STL algorithms get inlined, right?
Are compilers today smart enough to inline a function inside an inline function?
 
it will inline all the way down if it makes sense
 
Is it bad to define short member functions in the header?
 
10:44 AM
if a function is complicated enough it will not inline it but then the function overhead is negligible compared to the actual work in the function (hopefully)
 
bool empty() const;
or
bool empty() const { return (this->bla); }
I believe that my linker inlines between files but I am overly concerned about these things
 
a compiler can only inline functions it has the definition of
and I think you have to activate LTO explicitly before the linker will do opts
 
I have maxed out on all speed optimizations for both the compiler & the linker
but I have encountered dozens of cases where even one or two line functions wern't inlined
is there a C++ equivalent of cstdint?
I have uintXX_t scattered all over the place... it is a C thing
 
<cstdint> is the C++ equivalent of <stdint.h>
 
void SetFlag(INTERFACE_FLAGS flag) const { this->flags |= flag; }
 
nwp
10:56 AM
@YashasSamaga you should just check
 
expression must be a modifiable lvalue
pff
I used const
int square()
{
int arr[] = {1, 2, 3, 4, 5,6 , 7, 8,9};
auto itr = std::find(arr, arr + (sizeof(arr)/sizeof(int)), [](int& i){ return i%2});
return 0;
}
error: expected ';' before '}' token
 
{ return i%2; });
 
AH
What's it saying?
 
Ven
lmao that sizeof
 
What's wrong with it? o0
 
11:21 AM
std::begin(arr), std::end(arr)
40 mins ago, by milleniumbug
oh, and it's std::find_if, not std::find
 
What on earth am I seeing lol GCC converted the find_if algorithm into some code without loop
it checks each index manually lol
 
nwp
yay for loop unrolling
if you turn up optimizations it should compute the value at compile time
 
I used O3
 
your previous screen says -O0 so hopefully you changed this in the meantime
 
yes I did
O0 code is obvious
it had dirty code
it does the loop unrolling in O1 too
 
12:10 PM
// main.cpp
...
#include "Test.h"
...

int main(){
  ...
  Test test( something );
  ...
}

// Test.h
...

// Test.cpp
#include "Test.h"
...
according to this video, that's the right way to do it...
but I can't get it to work
only works if in main.cpp, I #include "Test.cpp" instead. I have no idea why would it be otherwise
I understand that there are a lot of bad practices in that tutorial, but still
@nwp ^
 
the critical parts are hiding in the parts you've dotted out
 
ok, is there a preferred way of sharing C++ code? I feel bad just dumping it here
pastebin?
 
Use gist or coliru
I recommend gist since you're dealing with multiple files
 
yeah, doing that
oh, gists onebox 0.o
 
you need to compile test.cpp as well and then pass the obj file to the linker as well, if you pass all cpp files to the compiler it should do that automatically
 
12:20 PM
If you use MSVS, then make Test.cpp and main.cpp a part of the same project
 
I'm using vim..
@ratchetfreak wait wut
 
cl main.cpp Test.cpp
 
is it some kind of added flags on g++?
 
g++ -std=c++14 -Wall -Wextra -pedantic main.cpp Test.cpp -o executable
 
what's cl do 0.o
doesn't seem to be any kind of linux command
 
12:22 PM
it's MSVC so not applicable to your case
 
cl is the windows compiler
 
oh...
oh heh it works
now, time to try to make sense of it...
so, the compiler compiles main.cpp and test.cpp separately, and puts them in the same executable and... that's all?
as in, main.cpp only needs the header to compile without weird optimizations
 
each cpp file is a separate "translation unit" they get translated to separate object files that
 
a good C++ book should explain multi-file projects
 
then the compiler invokes the linker and passes it all the object files it generated (plus the ones you passed only in the options) and it creates the executable
 
12:26 PM
@milleniumbug I'm reading the accelerated one
practical programming by example
I just like to mess around a bit first
 
check if it explains multi file projects
 
@ratchetfreak I guess that makes sense...
looks like it
 
messing around in C++ isn't the best way to learn
given all the pitfalls it has
 
I'm starting to see that
 
12:59 PM
How do I make a namespace to work with this code
namespace Interface {
class Interface;
}
I want to use Interface::Interface to access the class
 
hello again
 
Intellisense is complaining that the member variable types are undefined
 
say I have a function void my_func(int * const& i);
 
@user8469759 hi
 
and I wanna use it in the following snippet
int j;
my_func(&j);
what I wanted to do is to specify i don't want to change the pointer but I wanna change the content of value pointed
is that correct?
 
1:20 PM
yes
might as well do void my_func(int* i);
 
but in that case
it would perform a copy of the pointer right?
 
nobody cares about copying 8 bytes
 
why not?
ah it's because of the space
 
1:42 PM
that code looks bad
 
why pass the c_str?
 
void Interface::Trigger_OnScriptInit(int scriptKey, char * scriptIdentifier)
{
if (this->cbidx_OnScriptInit == -AMX_ERR_NOTFOUND) return;

cell addr;
amx_PushString(amx, &addr, NULL, scriptIdentifier, NULL, NULL);
amx_Push(amx, scriptKey);

amx_Exec(this->amx, NULL, this->cbidx_OnScriptInit);
amx_Release(amx, addr);
}
I can make the char * const in Trigger_OSI
I'll have to do a cast in amx_PushString then
so it does not help
using iterators would be faster, right?
 
negligible unless InterfaceList is a linked list
 
It is a vector
ok so iterators are of no use
is there anyway I can change the code completely to make it look nice?
I don't like to have casts
 
why does your interface take char* instead of const char*
does it modify the string
 
1:51 PM
or why not a const std::string&
 
@milleniumbug because the SDK takes char *
even if I make the Interface member functiont ake const char *, I will have to cast it in the member function
 
well then no way around it
 
@milleniumbug the string is not modified, a copy of it is placed in the heap
but the SDK sucks :/
 
SDK is shitty then
const_cast is precisely for dealing with shitty non const correct APIs
 
even then why not pass a string ref into the member function and have the member fucntion be responsible for the cast
 
1:54 PM
@ratchetfreak yea, I did that... makes more sense to do the cast there
 
also your const_cast should be as low down the call graph as possible so it won't infect the callers
and delay doing it as long as possible
 
also counts for the c_str()
 
@ratchetfreak ?
c_str returns a pointer to the internal char* pointer, right?
 
which is tied to the lifetime of the string object
 
delay calling c_str() too
 
1:58 PM
so if the operator[] is defined to return std::string you don't end up with a bad pointer
in this case it still works because temporaries get cleaned up after the function call completes
 
It has been 12 years since the SDK was released, nobody has bothered to make a C++ SDK -__-
 
@towc don't use using namespace std;
especially in a header file
 
is it important to use const for non-pointer/non-reference arguments?
for example, in void Interface::Trigger_OnScriptInit(int scriptKey, char * scriptIdentifier), scriptKey is not modified
other then stopping someone from accidently editing it inside the function, it has no other use I believe.
 
2:15 PM
It is wrong to call a destructor inside another destructor right?
I have an abstract class C with virtual destructor ~C
which has as data member an array of point to object of class D
(which is also abstract)
So there's something like
C::~C {
   for all el in array
       el->~D
}
(pseudocode of course)
this is wrong right?
I should just do
C::~C {
   for all el in array
       delete el;
}
the delete would call the destructor
each el is of class D1, ..., Dn all derived by D
derived from* D
 
2:37 PM
calling delete and destructors arent the same thing. if you allocated an object (pointer) you should call delete. call destructor otherwhise (delete will call the destructor)
 
but if array is a member field with a destructor then array's destructor will be called automatically and it should then call the destructor of each element it contains
 
It's killing me.
std::string scriptIdentifier = scriptIdentifier_cstr;
amx_GetString accepts a C string only :(
What are my options?
 
scriptIdentifier.resize(SCRIPT_IDENTIFIER_SIZE);
amx_GetString(scriptIdentifier.data(), addr, 0, SCRIPT_IDENTIFIER_SIZE);
 
o0
data returns the internal pointer?
what is the purpose of c_str then?
c_str is const and data isn't const?
 
data and c_str return the same thing and both can be const and non const
IMO but data documents that you will be changing it
and after that you scriptIdentifier.resize(strlen(scriptIdentifier.c_str())); to truncate until the first null char
ah correction c_str is always const
 
2:51 PM
yay bonus! amx_GetString gives me the length.
 
@YashasSamaga historical reasons. Also data() returns char* only since C++17
 
argument of type 'const char *' incompitible with char *
ok that explains
 
@ratchetfreak if a PLAIN array contains allocated objects (pointers), you cant even destruct it, however if youre talking about a STL array, it wont delete the allocated objects either. you will end up having to delete if you are dealing with pointers
 
so don't
friends don't let friends use owning raw pointers
 
@YvesHenri the elements should be smart pointers then
 
2:53 PM
std::unique_ptr FTW
 
damn
so I'll have to copy twice
or I'll have to use strcmp
 
&s[0] is a common C++11 workaround
 
@milleniumbug it would corrupt the string?
string size?
*length
 
It's assumed you won't write beyond the end
 
you also pass the length explicitly
that's what I assume the SCRIPT_IDENTIFIER_SIZE param is for
 
2:55 PM
yes
that is the max size
std::string.length returns the number of characters in the string, right?
so if I modify the internal char array, I won't be updating the internal length counter
 
yeah
that's why you need an extra resize after the call to truncate the string
 
I can use shrink_to_fit to fix the length
but I am not sure if shrink_to_fit uses the length variable
 
it does
 
@ratchetfreak exactly
 
you should call resize with the return value of amx_GetString (which you said return the length of the string)
 
2:58 PM
yea, I'm doing it
 
shrink_to_fit() should not be necessary unless you really care about the memory overhead
 
hmm I was mistaken
I don't get the length -,-
 
speaking of smart pointers... can someone explain me, for the love of god, whats the different between using the .get() from an uninque_ptr in multiple different pieces of code and using an shared_ptr? using the unique_ptr::get() is kind of sharing the unique_ptr among others. no?
 
would the length affect the == operator?
 
00:00 - 15:0015:00 - 00:00

« first day (227 days earlier)      last day (2628 days later) »