LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2011, 06:24 PM   #16
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723

Quote:
Originally Posted by SigTerm View Post
Now, what do you think will happen if that pointer or its capacity are initialized with garbage?
But doesn't copying a new string into it overwrite the grabage?
 
Old 09-27-2011, 06:32 PM   #17
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
Maybe, but isn't it possible to somehow tell the compiler that I don't want to initialize anything?
How will the compiler know if or when to call the destructor then?
Quote:
Originally Posted by MTK358 View Post
I still don't understand what's wrong with creating an empty space of sizeof(std::string) and then assigning a value to it like this:

Code:
var = std::string("example");
where "var" contains an uninitialized value the size of an std::string.
Have a union with std::string* and int then, and delete the std::string* when necessary. And if you're that worried about space, just use a char* and manage the allocation yourself.
Quote:
Originally Posted by MTK358 View Post
I thought that complex data structures are just a series of bytes no different from an integer, and that copying them involves just copying every byte.
One of the most basic principles that everyone should know about C++ is that this isn't at all true.
Kevin Barry

PS About that last comment, it's something that should be included in all C++ tutorials. The tutorial I learned from left out a lot of important things that I learned the hard way, even after learning much more advanced concepts. The lesson is that not everyone who thinks he/she is knowledgeable enough to write a C++ tutorial actually is.

Last edited by ta0kira; 09-27-2011 at 07:18 PM.
 
1 members found this post helpful.
Old 09-27-2011, 06:34 PM   #18
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by MTK358 View Post
But doesn't copying a new string into it overwrite the grabage?
Sigh...

No. Because there is no way to distinguish between garbage pointer and initialized pointer. Assignment operator is written with expectation that data within string is not garbage. So it will either dereference or try to free "initialzied" pointer at one point.

As I already said, try to implement std::string (or a generic string class) yourself as an exercise.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C (++) Unions trouble rajitsingh Programming 8 05-10-2010 02:52 PM
C Unions MTK358 Programming 4 03-18-2010 08:57 AM
C++ Unions to understand - question vargadanis Programming 14 04-19-2007 11:49 PM
unions spx2 Programming 3 08-21-2006 09:35 AM
array of unions in c alaios Programming 3 09-19-2005 09:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration