#strings basic

1 messages · Page 1 of 1 (latest)

fervent girder
#

this converts string to character array in java ??

in cpp whats the similar command ?

weary havenBOT
#

<@&987246683568103514> please have a look, thanks.

weary havenBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

tough kelp
#

what strings are u talking about in c++? std::string or char*?

#

unlike java, in c++, strings are char arrays already. std::string is merely a convenience wrapper around the raw char array

#

std::string has a method called c_str() which gives u the underlying raw char array

#

note that, unlike java, strings are mutable in c++. so be careful when u edit it

#

and note that the array is one larger than the actual string length, since strings in c++ (usually) end with the nul-terminator

fervent girder
#

2.) another qn --- are heap and binary heap same thing ?
or they are different

tough kelp
#

for ease of use

#

capability-wise, the biggest difference is probably that std::string is still aware of the length

#

while raw arrays are not

tough kelp
#

id suggest u share the source where u got this from. im sure it makes sense then

fervent girder
tough kelp
#

there is no such method

fervent girder
tough kelp
fervent girder
royal burrowBOT
ripe schooner
#

You can use this if u want toconvert to char array

#

Oh nvm ur question itself uses that method

fervent girder
#

@tough kelp i made an class with a pointer in it
so some uses -->

ObjectOfThatClass->pointer;
to access it
and sometime i saw ObjectOfThatClass.pointer;
to access it, which is correct ?

tough kelp
#

so its a combination of "resolve this pointer and then access that property"

#
Person p1("John");
std::cout << p1.name;

Person* p2 = &p1;
std::cout << p2->name;
// same:
std::cout << (*p2).name;
fervent girder
#

oh got you
i got another doubt --->

some make the object of the class like ---

ClassName *ObjectName = new ClassName();
and some make it with
ClassName ObjectName;

what are the differences and are both correct in cpp ? where to prefer which one ? @tough kelp

tough kelp
#

Person p; is created on the stack. Person* p = new Person(); is created on the heap

#

sounds harmless, but the differences are significant

#

first of all, the stack is small in comparison (~500 MB maybe, depends on the system)

#

the heap is the rest of ur RAM, so typically multiple gigabytes

#

stack is also slightly faster than heap

#

code-wise, objects on the stack are auto-deleted when they go out of scope

#

so

#
...
{
  Person p;
  ...
} // p is killed NOW
... // p is dead, cant be used anymore
#

objects on the heap are never auto-deleted

#

they keep hogging ur memory until u explicitly free them again:

...
Person* p = new Person();
...
delete p; // p is killed NOW
... // p is dead, must not be used anymore
#

if u forget to delete it, it will stay there and consume ur memory. thats called a memory leak

#

in general: prefer the stack

#

unless:

#
  • u create a lot of data or dont know how much (e.g. u want the user to enter a list of names and u dont know how many they will enter)
  • u want shared ownership (object should only die once nobody uses it anymore)
  • u want to transfer ownership to someone else
#

in practice, cause its so easy to do mistakes with pointers, people dont write Person* p = new Person(), but instead use smart pointers std::unique_ptr, std::shared_ptr, std::weak_ptr. with them, u can essentially benefit from the stack-like syntax, but have the actual objects on the heap

fervent girder
#

got it, thanks for answering @tough kelp