Emir
Francisco
You should do something like this:
int ** arr = (int**) malloc(sizeof(int*) * rows);
for (int i=0; i<rows; ++i)
arr[i] = (int*) malloc(sizeof(int) * cols);
Francisco
malloc returns a void*, so seems logical to me doing that. It might be verbose, but I don't see why you shouldn't
Francisco
I prefer being explicit of what I do rather than let the compiler do an implicit cast
Francisco
And that's why I hate C. I don't agree at all with those kind of implicit castings. I'm of the team that implicit casts are hell. Maybe those are my forms as a C++ programmer rather than a C one
Francisco
I basically don't agree. I've seen so many things going wrong because of implicit castings
Francisco
One example is when comparing integers with different sign types. Sometimes I want the comparison to happen between the unsigned type and some others I want it with the signed one, depending on the design
Francisco
Well, I'd like to live in a world where the STL has signed size_t, but we don't, and most of the time I prefer signed types in my design, but still have to compare with the unsigned type the STL chose
Francisco
The thing that unsigned types have that discontinuity in the 0 is a hell in many ways. In fact, C++20 added ssize to the STL in order to get the size of a container, but with a signed type instead of the good old unsigned one
Francisco
That hints that maybe choosing an unsigned type for containers' size wasn't the best one
Francisco
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1227r1.html
Francisco
Because I don't want to do signed vs unsigned comparisons, and sometimes my designs are easier with signed types, I cast to the type I want so I don't compare values with different sign-ness
Francisco
And also to avoid the warnings that the compiler throws
Mar!o
Guys chill out let each other have their own opinion. Sometimes there is no right or wrong...
Francisco
It's okay you don't like explicit castings, but I choose to use them wherever it's possible, so I'm completely clear about my intentions
Francisco
I guess we are not going to agree. There's also stuff in the cpp core guidelines I don't agree with
Francisco
And the proposal talks about more motivations other than the span one. The problem became clear with span, but it wasn't just because of span
Francisco
Guidelines aren't immutable. If they were, we'd be still following the "return const by value" that was present in the Scott Meyers one
Mar!o
Francisco
Scott Meyers’ Effective C++, Third Edition (Item 3, page 18)
Mar!o
Guys please just stop beeing so angry. Just agree each other's opinions even if they are right or wrong. Please stop!
Francisco
It implied that a signature like const ObjectType f(...); could be a good thing, which was obviously changed in later editions
Francisco
I'm not angry, I'm just trying to explain my point of view in a specific topic, that's all
Mar!o
Francisco
I guess it was an erratum then, but it was there at some point
Francisco
Francisco
Again, I said it turned out to be an errata fixed in 2009:
https://www.aristeia.com/BookErrata/ec++3e-errata.html
Francisco
11/ 4/07 nxd 18-19 The text implies that all by-value returns should 7/17/09
be const, but cases where non-const by-value
returns are good design are not difficult to find,
e.g., return types of std::vector<T> where callers
will use swap with an empty vector to "grab" the
return value contents without copying them.
Francisco
The thing is that guidelines might be good at some point, but turn out to be not so good in the future
Anonymous
Who knows penetration testing
Mar!o
Idk guys, but I think that sometimes we also have to take a look at how important something is in real world scenarios: I mean except from styles and suggestions, does it really matter if someone cast's a void* to an int* explicitly? I mean
seriously it might be harder to read, and in C it is more common to NOT cast it, but it does not matter in a small program. It might matter in a huge for readability but let's be honest even in companies many programmers sometimes use their own styles - they use the company style mainly and some smaller things like they want... so I think it's rather redundant to discuss this...
Mar!o
For the signed/unsigned size_t/ssize_t problem, it's more important!
Anonymous
Mar!o
"Please consider updating the answer. The cast is no longer dangerous, and repeating oneself is not necessarily a bad thing (redundancy can help catch errors)"
Mar!o
"Ok. I think it's bad to assume that anyone reading here has a particular compiler. Also, since C11 the entire "implicit function" concept is gone, I didn't know that" also that's true. Many people still use older C so no C11
Mar!o
Seems to be a hot topic
Mar!o
Yes I think if you want C11 on Windows clang is the way to go. MSVC is shit; it does not even fully support C99!
Each time I have to write:
#define restrict __restrict
I'm getting upset about it!
Mar!o
Oh really? I didn't knew that! That's a shame! I like the C11 multithreading library - is there no way to get it on Windows except for MingW?! I think they should all start implementing C11!!
Mar!o
I hoped I could drop pthreads for C11 but looks like I could not :(
Mar!o
Oh my 😐
Mar!o
Emir
int main(){
char * a = "emir ";
char * b = "alkal";
printf(strcat(a, b));
}
is this why illegal instruction?
Emir
The output is
Illegal instruction: 4
Anonymous
Emir
strcat function is writing location of a string of a+b , am i get right?
Isn’t that new memory locate for new string
Anonymous
Emir
okay i get it thanks
Emir
#include <stdio.h>
#include <string.h>
int main(){
char * a = "emir";
char * b = "alkal";
printf(strncat(a, b, sizeof(b)));
}
it’s illegal too?
Anonymous
Anonymous
Anonymous
i told you to use const char * for this exact reason
Emir
i want to 2 bss string combine, isn’t that possible?
Anonymous
doing char * doesn't mean their constness magically vanishes, they are still const. stop writing to them
Anonymous
Anonymous
Anonymous
Emir
Anonymous
okay
you can also use normal arrays
char a[10] = "emir";
const char *b = "alkal";
puts(strcat(a, b));
Emir
Anonymous
Emir
i get it stuff, thanks
Anonymous
if i write that a[6] it is buffer over flow, right?
technically the solution i gave before editing to strcat won't result in buffer overflow
strncat(a, b, sizeof(a) - strlen(a) - 1);
// make sure a is char[N] type and not char * type in this scope
Henry
hi my codeblock won't load
Henry
Visual studio is premium
Emir
Anonymous
this is kinda sorta outdated. should mention VSCode. and maybe KDevelop too
Anonymous
Anonymous
🤷
Emir
for macOs CLion too
@.!
Is there any good books for learning c++ as there are many but I don't know which of them is better?
@.!
Also any practice bokks for c++ for beginners?