Mohamed
Hello Any suggestions for C-based book on neural nets, ML, AI, statistics, or other relevant subjects, please?
Chat Boss
Notaxmar sent a huge message, it has been re-uploaded as a file Help me, why is this error happening to this code? #include <stdio.h> #include <stdlib.h> #inclu..
klimi
@Notaxmar What is supposed this to mean? xtr = malloc(10 * sizeof(*xtr));
Anonymous
https://brevzin.github.io/c++/2021/11/21/conditional-members/
Notaxmar
@Notaxmar What is supposed this to mean? xtr = malloc(10 * sizeof(*xtr));
I was allocating 10 bytes to the integer pointer. I don't know if that is correct because I'm a beginner
klimi
I was allocating 10 bytes to the integer pointer. I don't know if that is correct because I'm a beginner
Well... it is allocating 40 bytes. But i wonder, what is your compiler toolchain?
klimi
(under recent gcc and clang it seems to work)
Notaxmar
I'm very confused
klimi
I'm using cxxdroid for android
not sure what that is, does it use some standard compiler or it has some theirs?
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
@Notaxmar What is supposed this to mean? xtr = malloc(10 * sizeof(*xtr));
It's actually a pretty clever way to do it's job, because if you change xtr type you won't have to change the malloc, BUT this is a HUGE mistake, as malloc returns a pointer to a memory location. Xtr is not a pointer, therefore it's wrong
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
For instance, you could do this: Char *ptr= malloc(sizeof(*ptr)) Therefore If for some reason you decide to change ptr from pointer to char, to pointer to int, you won't have to change what's inside the malloc, but to do this you need what's on the left side of = to be a pointer
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Actually casting the malloc is (as far as I know) a bad thing, my professor gave very bad grades for such things whenever a student did so during an exam
#define CLEAR_BUFFER while ( getchar ( ) != ’ \n ’ )
Hi here
#define CLEAR_BUFFER while ( getchar ( ) != ’ \n ’ )
I have been programming in C for 5 years and I absolutely have to switch to C++. I am planning to write C++ by mixing it with C (loading the C libraries). Do you think this method should be avoided?
Anonymous
No. Casting the return value of malloc is a very bad practice.
#define CLEAR_BUFFER while ( getchar ( ) != ’ \n ’ )
Anonymous
No. It is bad practice because you are repeating code. That violates DRY principle. Isint it obvious from the sizeof that the returned value will be an int*? Secondly the cast is not required as a void* pointer can be casted implicitly to any pointer. And lastly, having an explicit cast will be undefined behavior if you forget to include stdlib.h
mito
Advanced C & C++ compiling, for example
Lol, my senior suggested this exact book yesterday for me..
Jose
Lol, my senior suggested this exact book yesterday for me..
There are few books cover this topic (how the compiler makes its magic under the hood) and explain a bit further what is not the programming language itself, but the tools are involved in the creation of an executable file.
Is it possible to implement the following code without "explicit this" in c++23, I want to implement it in c++17. https://godbolt.org/z/Ghcc3jWq5 derived class call a method in parent class, and than chain call the method of itself.
In my case, the func does not return self in fact, but create a new object after some operations, so this cannot be used: https://stackoverflow.com/a/27222362
solved... https://godbolt.org/z/9PzG8sKhe
Anonymous
Madhu, what’s the current consensus on reusing code?
Anonymous
Not casting, but code reuse.
Anonymous
As a summary for everyone else, here is a discussion on Reddit on casting return values of malloc https://www.reddit.com/r/C_Programming/comments/7rk19b/revisiting_malloc_and_cast_in_c/ Rob Pike (who has worked closely with both K and R of K&R C) also chimes in to say casting is a very bad idea. And here is the Stack overflow QandA on why casting return value of malloc is bad - https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc And here is advice from Jens Gustedt (C Standard Committee Steering member) on Modern C practices. 2.13.1.4: Don't cast the return of malloc and friends. ... So please stay clear of dinosaurs and ignoramuses claiming to have 28 or more years of programming experience and offering bad advice under the pretext of knowledge or experience.
Anonymous
Not casting, but code reuse.
Code reuse is obviously good if the code you are reusing is well designed. If not, it makes better sense to refactor it or at worse write it from scratch even instead of continuing to reuse badly written code.
Anonymous
Anonymous
Thanks. Now, is there a way that I can declare a matrix in C++? By matrix, I mean a 2-dimensional array of numbers, as in linear algebra.
Depends on your needs. Do you want this matrix to be dynamically allocated or do you know it's dimensions at compile time?
Anonymous
Depends on your needs. Do you want this matrix to be dynamically allocated or do you know it's dimensions at compile time?
Its size is fixed. Let’s say that it’s known to be 5 × 5 (25 numbers in all).
Anonymous
Its size is fixed. Let’s say that it’s known to be 5 × 5 (25 numbers in all).
Are you asking how you would declare a 2 dimensional array of integers in C++? The answer would be to use a std::array of a std::array. Or a simple - <typename>[5][5] But if your question is on how you would design a Matrix class with the associated operations, then the answer would be entirely different.
Anonymous
Anonymous
Like this? int[5][5] X;
How am I supposed to know what your requirements are? If you are going to be working on Matrices of different dimensions, it makes better sense to refactor Matrix related operations like subtracting a row from another, adding a multiple of a row to another, multiplying a row by a scalar and so on and encapsulate them within a Matrix class rather.
Anonymous
Why don't you go ahead with RREF implementation instead of asking random questions here
I need to figure out the details first. No, my questions are not random.
Anonymous
All the matrices are of the same dimensions—5 × 5.
If the dimensions are not going to change, you can still allow for it to be changed later. And reducing a matrix to row echelon format is a straightforward operation.
Anonymous
If the dimensions are not going to change, you can still allow for it to be changed later. And reducing a matrix to row echelon format is a straightforward operation.
1. No, they must stay the same. 2. No, it is not straightforward, because I need to make sure that I do it right, and it has many steps in C++.
Anonymous
I need to figure out the details first. No, my questions are not random.
Is there a specific C/C++ question that you have in mind? If yes ask it please. If not, read the rules and see if the question you have in mind is a good fit for this community. If yes, ask the question directly. If no, probably you should be asking your questions elsewhere
MaaKa
Please is it possible to write a single code that tells if a number is palindrome and Armstrong number in c ?
MaaKa
yes
Please any tips on how to go about that ?
Alik
Googling
klimi
Please any tips on how to go about that ?
well they are so easy P-problems that you could even just write the algorithm to calculate it, or you could do some m-reduction but that seems like a little bit of overkill :D
MaaKa
MaaKa
well they are so easy P-problems that you could even just write the algorithm to calculate it, or you could do some m-reduction but that seems like a little bit of overkill :D
Let’s say a pseudo code of how it works will help Please I need to program it myself so I understand everything perfectly
klimi
Let’s say a pseudo code of how it works will help Please I need to program it myself so I understand everything perfectly
yeah, you can write TM or while-program to prove that it is computable ( and therefore program that calculates it exists)
klimi
Any help using a pseudo code?
I am not really sure how would pseudocode be formally described and therefore used for the proof; while-program would be better
klimi
Learning "Structure and Interpretation of Computer Programs" at college? 😉
just finished the course "computability and complexity" :D (quite amazing ngl)
klimi
Same course, different names everywhere 😂
well it's translated from czech :)
MaaKa
Learning "Structure and Interpretation of Computer Programs" at college? 😉
It’s kinda fun but limited as you need some extra work to fill the gaps Teacher gives you A and assumes you know xzy Crazy 😅😅
klimi
MaaKa
With what program you need help?
Write a program in c To tell if a number is palindrome and Armstrong number
klimi
Write a program in c To tell if a number is palindrome and Armstrong number
ask chatGPT it will gave you the code in c, i won't code your assigment for you, i am not stupid
MaaKa
It’s probably a group task It’s been done by someone I want to get the complete sense of joining the two codes together and making it work as one
MaaKa
And I can’t use gpt in my country Since I’m in Cameroon 🇨🇲 ( Africa )
Chat Boss
Write a program in c To tell if a number is palindrome and Armstrong number
Sid Xid sent a code, it has been re-uploaded as a file
Olivia
heres ur code for palindrome
Olivia
i wrote ur code for palindrome just check once
MaaKa
Okay will check now But I need the two codes combined to work as one
MaaKa
That's my problem 🥲
Olivia
do u need it as a function
Olivia
fucntion overloading?
MaaKa
I need it as a function
MaaKa
It should be able to print a result like This is not a palindrome or Armstrong number