Anonymous
the same code works with : for(unsigned int i = 0; i < raw; i++){ for(unsigned int j = 0; j < column; j++){ rotate_matrix[i][j] = old_matrix[raw - 1 - i][column - 1 -j]; } }
my (1) is identical to this except for the order of assignment. there is no reason for that to segfault and this to work fine. there is a mistake in some other part of your code
Anonymous
why did you write i-->0 and how did you understand that?
i-- > 0 i.e. check if the current value of i is greater than 0 while decrementing i. this way i inside the for loop starts at row - 1 and ends at 0 instead of starting at row and ending at 1. but the value that is tested in the condition starts at row and ends at 1 (fails when 0).
PO
For what this code
for rotating 180° a 2 dimensional matrix
Anonymous
I want to create small game in c
Anonymous
And i do it but one problem in that game
Anonymous
Anonymous
.
Wisenky
Wisenky
It’s called interop
Can you explain it a bit more
Patrick
Hi, all, could someone give me some pointers into reducing the size of this C++ code? I'll be adding more operations, such as bitwise, which end up being triplicated. I feel there might be an option using ASM?
Alex
hi. what do you mean size of code? lines of code or ram usage?
Alex
just add additional functions for hassign, isfloat, default case
Patrick
It will be a good practise for me to see how I can apply those practises to other parts of my codebase too
Patrick
as it's a VM, it has to operate with multiple different types of data frequently
Patrick
Because I feel it could be boiled down to three conditionals: the operation, the 32bits for the result, and which function to call for the value
Patrick
and the operation doesn't change between values so I'd like to optimise it
Alex
are you looking for performance optimization or just decreasing number of code lines?
Patrick
I suppose both
Patrick
And as I said it would be a good learning opportunity for me
Patrick
Anything above a g++ level 1 optimisation is unavailable to me at the moment
Alex
1. to decrease code lines: I would add 3 functions for each type of operation 2. to optimize performance: you should use profiler and get bottleneck
Patrick
I'm unsure how the functions would look like?
Patrick
Oh, three functions
Patrick
How would that be an improvement over the switch?
Alex
first function for isFloat case etc
Alex
How would that be an improvement over the switch?
I propose you to move switch inside new function
Patrick
Really I'd like to do away with the switch
Patrick
op is constant
Patrick
why?
Because it's switching on a constant
Alex
I can`t understand your reason, but you can use unordered_map<op, function> instead of switch
Patrick
It's nice to know unordered_map exists at least, but
Patrick
this is already quite a performant function, but I'm wondering how I can avoid switching on a const
Patrick
Hmmm, this sounds interesting
Patrick
I haven't looked into these concepts before
Patrick
Because I don't mind duplication in the binary
Anonymous
Because I don't mind duplication in the binary
oh i think you actually need duplication in the binary
Patrick
Yes, I don't mind it - it's fine
Anonymous
templates are compile time. so if a condition is not satisfied during compile time, the part related to it never makes it into the binary
Patrick
Essentially it's the += -= *= etc which is constant
Patrick
Would I be able to invoke those operators through templates perhaps?
Anonymous
Would I be able to invoke those operators through templates perhaps?
i don't think it would be possible to specialise the template on types that are so similar. maybe tag dispatch. but there is no way to do this compile time
Anonymous
Essentially I could write this whole function 5x with only one operation performed, but that's really bulky
i guess you can make an std::function object pointing to one of the .d32c(), .s32c(), and .u32c() functions. then only the modulus operator would need special syntax. others can be called through the std::function
Patrick
It's the operators that are the problem really
Patrick
I know that they are all discreet machine operations, that's why I thought perhaps an ASM route would help
Patrick
basically I'd love to be able to do if (isFloat) dResult OP_HERE v.d32c(); else if (hasSign) sResult OP_HERE v.s32c(); else uResult OP_HERE v.u32c();
Anonymous
Patrick
Mmmm
Anonymous
Mmmm
but you could do function(lhs) += function(rhs) function(lhs) -= function(rhs) ...
piggyho
Really I'd like to do away with the switch
switch is usually very efficient. compiler can set up look up tables which is faster than branching
Patrick
perhaps hypothetically D_OP S_OP U_OP
Anonymous
and make function point to .d32c(), .s32c(), or .u32c() depending on type
Patrick
and make function point to .d32c(), .s32c(), or .u32c() depending on type
The type changes per loop so I don't think this would be feasible
Anonymous
switch is usually very efficient. compiler can set up look up tables which is faster than branching
ye the program is basically C with classes atm. the C++ hacks would probably make it slower instead of faster.
Mar!o
Use computed goto i'ts much faster than a switch statement
@unchanted
Anyone CS bachelor in this in this group?
Anonymous
@unchanted
I mean can i discuss some of my queries in this group Of course that would be regarding to c++ but that maybe of college level
@unchanted
Or on an elementary level
klimi
Ok thanks 👍
And you can use offtopic group for non-c/c++
Mood of
Hello
abhi3700
Hello! Let's say there are 2 functions func1, func2 defined inside a class1. Now while defining func1outside the class, i have to use func2, now should i directly parse func2 or class1.func2 ??
@unchanted
how to know whether we have to use '->' or 'this' keyword?
olli
this is a pointer to the object, e.g. on which a non-static function is being called. You need to use it when you want to access the object. -> accesses the member of a pointer (you can also write (*ptr). )
@unchanted
this is a pointer to the object, e.g. on which a non-static function is being called. You need to use it when you want to access the object. -> accesses the member of a pointer (you can also write (*ptr). )
ok if we are using a class pointer and want to access a member of that class then we use '->' and when we are using a class directly then we use 'this' keyword, am i correct?