User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,515 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1043 | Replies: 2 | Solved
Reply
Join Date: Sep 2004
Posts: 416
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Strings and the new operator?

  #1  
Sep 25th, 2007
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?


  1. // Testing code.
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. int main()
  7. {
  8. std::string mName = "John Malkovich";
  9.  
  10. std::string *pName = new std::string;
  11. *pName = mName;
  12.  
  13. std::cout << "Name is: " << *pName << " !\n";
  14.  
  15. delete pName;
  16. pName = 0;
  17.  
  18. std::string secName = "";
  19.  
  20. getline(std::cin, secName);
  21. pName = new std::string;
  22. *pName = secName;
  23.  
  24. std::cout << "Second name is: " << *pName << " !\n";
  25.  
  26. delete pName;
  27. pName = 0;
  28.  
  29. return 0;
  30. }

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.
Last edited by JoBe : Sep 25th, 2007 at 11:19 am. Reason: Added additional question.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Strings and the new operator?

  #2  
Sep 25th, 2007
>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.
Last edited by Narue : Sep 25th, 2007 at 11:34 am.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Sep 2004
Posts: 416
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Strings and the new operator?

  #3  
Sep 25th, 2007
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:57 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC