Asad
Ohhh, that's why
Vlad
After the call of malloc you must call placement new to start the lifetime of an object
Pavel
Well, I have a question. What is the difference between memory allocated using new and malloc. When I did new and then used realloc to the same memory, it gave me an error saying that I can only use realloc after malloc. Why? Memory is memory! Ain't that true?
If in short, new also calls a constructor for you. But you can't just replace new with malloc and placement new because then you would need to also call destructor explicitly and use free. I mean, never mix new and free, malloc and delete
Vlad
Also new tied to an allocator
Vlad
And only allocator that gave that memory can also free it
Anonymous
malloc doesn't call a constructor
Constructor is the one that's invoked when the object is created right ?
Vlad
You mean the source of malloc implementation or what?
Vlad
https://github.com/lattera/glibc/blob/master/malloc/malloc.c
Anonymous
Hey
Anonymous
Who can help me with my English exam tomorrow?
Dima
lol
Anonymous
:)
Asad
unbelievable
Anonymous
Anonymous
If you what to be a C++ programmer, learn C++
Okay but I don't know where to start
Anonymous
Though everyone told me how so I'm good for the time being
Mar!o
Just start by programming! I don't know how often I have to repeat this but you only really learn C++ by WRITING projects in it. Set yourself a little goal and off you go!
Harry
## Rules #pinned * You are not entitled to an answer, getting angry about not answered questions will get you warned. * Not checking your problem in google (or any other search engine) first will get you a warn. * Asking to solve an assignment/test/whatever without trying it first yourself will get you a warn or will get you BANNED. We won’t write a code for you, but we can push you forward and suggest something. * Before posting a long code snippet think twice and read the Resources section below. * No “best book” or “best youtube channel” requests, use /get cbook and /get cppbookguide chat commands. * No “best ide” requests, use /get ide chat command to see the suggestions. * Asking for something that is in the pinned message right after joining WILL GET YOU BANNED. * C/C++ discussion preffered (assembly also allowed), asking about other languages (or groups for other languages) right after joining will get you BANNED. * Reverse enginnering, hacking and related topics are allowed, but asking to hack facebook, instagram, etc. is NOT allowed. Also if you ask "how to become a hacker" and obviously have zero knowledge on anything related to that you may get warned or banned. * Legitimate requests for help on code and programming questions are welcome. A request is considered legitimate if it describes your problem, what you've done so far and what went wrong. Requests such as "write my program" or "do my homework" are never considered legitimate. Asking not legitimate questions will result in a warn or ban. See https://stackoverflow.com/help/mcve on how to write good questions. * Asking for book recommendations is ok, but "pls give pdf book" is not allowed, find books by yourself. Posting illegally copied books (or links to those books) is also not allowed and will get you BANNED. * Only English language is allowed, if you speak shitty English or don't understand anything in English YOU WILL BE BANNED. * A little bit of programming related memes, jokes, shitposting are allowed. * NSFW content (porn, nudity, etc.) is not allowed. * Spamming/advertising (job posts are also ads, so ask an admin before posting) will grant you a warning or an immediate ban if you do it right after joining. * Religion, politics and ideological topics are forbidden. * Long code snippets must be posted via a snippet website(links below), posting pictures of code and posting long snippets in the group is not allowed. * If you encounter a problem, while using dev C++ or turbo C/C++, you will not be helped, use another more modern IDE. * Don't post compiled executables, that is obviously a security risk. * Personal messages without asking beforehand are not allowed. * If you want to post a link or some article in this chat you will need an admin approval first ## Resources C/C++ group India: http://t.me/c_cpp_india About asking good questions: * [English](http://www.catb.org/esr/faqs/smart-questions.html) * [Translations](http://www.catb.org/esr/faqs/smart-questions.html#translations) For posting long code snippets: * [GitHub Gist](https://gist.github.com) * [Ubuntu Paste](https://paste.ubuntu.com/) * [Pastebin](https://pastebin.com) ## Reports If you notice any forbidden content, please use the /report command while responding to the offending post to report it to the admins.
Is there any offtopic group?
Hmaada
hi i have some truble about how to remove spaces from a char* string but with the optimised way is there a sympatic guy how can help me pls?
Hmaada
in one or two lines
Hmaada
my code work but i wrote more than 7 lines
Pavel
in one or two lines
for(int i=0; i<len; ++i) if(char c=input[i]; c!= ' ') output[i] = c;
Hmaada
mmm are you sure about it
Pavel
mmm are you sure about it
It's a just an idea
Pavel
Extreamly primitive one
Hmaada
ah oke oke i think that what i wrote but yeah now i see how to compress it
Pavel
You can use erase-remove idiom to do it in c++ way
Hmaada
thank you
Pavel
ah oke oke i think that what i wrote but yeah now i see how to compress it
And what's the purpose of that "optimization"? Hard drive is not large enough to store couple of bytes?
Hmaada
it juste for the beauty of the code hhh 😎
Pavel
/report crypto ad
Sasuke
Hi. I have few questions about functions. Kindly have a look at the below paste. https://pastebin.com/Mcykq0q4
Asad
Hi. I have few questions about functions. Kindly have a look at the below paste. https://pastebin.com/Mcykq0q4
In function_2, you are returning the address of local variable that will be popped out of stack after the function is finished.
Roxifλsz 🇱🇹
/ban
Asad
Ya... Can u read the questions below the code?
A1: I am not 100% sure, but 0 returned by function_2() must be NULL (NULL equals 0). A2: You can return value but not the pointer. Because, values are copied. Even if, the object will be destroyed at the end of the function, its copy will still exist. A3: A2 is the answer for Q3, too
Asad
Two copies does not share the same memory. Just like in real life, your second duplicate car does not get crashed if you crash the first one.
Amox
Hi all
Pavel
Ya... Can u read the questions below the code?
funtion_2 returns address of local, it's UB. In fact most likely will not be NULL
Asad
Ok I understand so far. But where does it copy resides I mean which memory segment? Since stack frame got deleted for that function.
It first returns by value, and then take the memory of local variables. Returned copy value will have it is own stack mem inside the scope of caller of the function_2()
Hmaada
hi a small question pls, how can i return -1 if a char* string is empty
Pavel
So getting 0 (NULL) I must be lucky 😂
It's just compiler behaviour with given flags. Try -O3
Sasuke
It's just compiler behaviour with given flags. Try -O3
It removes optimization I guess.. anyways thks
Asad
So during this period returning from callee to caller does it stores in a register or something?
Idk. You don't have to think this low-level in high-level language like C. It just copies the value to new location, then destroys the old value
Aakash
malloc doesn't call a constructor
What makes you say new calls constructor?
Aakash
What makes you say new calls constructor?
Here how new operator is implemented. New uses malloc or aligned malloc!! https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc;h=dadcef2c3011679ed4e40724f6940d8cd4618a46;hb=HEAD
Sasuke
hi a small question pls, how can i return -1 if a char* string is empty
Use if Statement like if(strcmp(string, '\0') == 0){ return -1} I don't remember correctly I think strcmp returns 0 when matches. Google to confirm. Btw your function must be of a int return type.
Sasuke
Vlad
What makes you say new calls constructor?
new and operator new are different things
Hmaada
so it doesn't work i wrote smthg like this and it doesnt work too if(str==NULL){ printf("test : %s\n",str ); return -1;
Hmaada
it print nothing
Sasuke
strcmp compare two string if they are equal it return 0
I suppose that's what he is trying to do
Vlad
Just kidding
Anonymous
Just kidding
Nerd 👍
Hmaada
Try if(!str || !*str)
it doesnt work but what does mean ?
Anonymous
node* creat(char *ques)
Anonymous
what is this
Vlad
Nerd 👍
What did ya say, punk?
Anonymous
does node is pointer
Aakash
new and operator new are different things
Okay!! Let take keyword new! Does it calls constructor?