Hello ladies and gents,

It's been ages since I posted here, have been doing some coding on and of and I was wondering about the following, beneath is a test I made in trying to use the new operator in combination with a string, first a string literal, second time a string input.

It's working, but, the thing that I'm seeing when debugging this is when I get to the point std::string mName = "John Malkovich"; the value of mName comes up as <bad Ptr>.

Questions:
A) is the way I used the string and new operator correct for this little program?
B) is this <bad Ptr> related to a bad Pointer and what does that mean? Probably not something good?
C) It seems that when I use std::string *pName = new std::string; , I don't have to determine the sizeof this string? Is this correct? Or is something going wrong which I'm not aware of?

// Testing code.

#include <iostream>
#include <string>

int main()
{
	std::string mName = "John Malkovich";

	std::string *pName = new std::string;
	*pName = mName;

	std::cout << "Name is: " << *pName << " !\n";

	delete pName;
	pName = 0;

	std::string secName = "";

	getline(std::cin, secName);
	pName = new std::string;
	*pName = secName;

	std::cout << "Second name is: " << *pName << " !\n";

	delete pName;
	pName = 0;

    return 0;
}

Thanks for any help you guys/girls can help.

Edit: oh yes, has the search function been altered on this forum? I seem to remember that you could do a more in depth search for certain subjects you wanted to find out about.

>A) is the way I used the string and new operator correct for this little program?
It looks fine, why do you ask?

>B) is this <bad Ptr> related to a bad Pointer and what does that mean?
It's just your debugger's way of saying that mName isn't ready to be used yet. If you get that after stepping over the line (where mName should be created and initialized), then you probably have an issue.

>C) I don't have to determine the sizeof this string?
Why should you? std::string is an object that manages the actual string for you. The memory grows to fit the size of the string automagically. If you had to manage the size, why use std::string instead of an array of char?

>oh yes, has the search function been altered on this forum?
Yes, it's gimped now. Completely useless if you want to do any kind of advanced searching. But it seems Dani isn't done upgrading the search feature, so perhaps it will be fixed in the near future.

Hi Narue,

Great to see you're stilll around here.

>It looks fine, why do you ask?
Because I wasn't sure Narue.

>It's just your debugger's way of saying that mName isn't ready to be used yet. If you get that after stepping over the line (where mName should be created and initialized), then you probably have an issue.
Ah, ok, theres no problem after stepping over the line.

>Why should you? std::string is an object that manages the actual string for you. The memory grows to fit the size of the string automagically.
Well, believe it or not, I thought it wasn't necessary, but, because the message of the <bad Ptr> I started to doubt this. I was thinking because I didn't add a sizeof, the <bad Ptr> was appearing.

>Yes, it's gimped now. Completely useless if you want to do any kind of advanced searching. But it seems Dani isn't done upgrading the search feature, so perhaps it will be fixed in the near future.
Well, I hope she fixes it, it was alot better and easier to find related posts/threads to what you where looking for !!!

Anyway, thanks again Narue, good to see you're still around :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.