473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const std::string SOME_STRING =

If this question is already ask please forgive me and let me know where it is.

1) which way is better? SOME_STRING is never modified.
2) Does C++ instantiate string object if I use the first method (const std::string SOME_STRING = "This is a test.")
3) If I use the second method, do I need to destroy the object after I use it.
4) The first method, SOME_STRING will be placed in a heap or a stack?
5) I always use the first method, but some engineers told me that the second method is much better.

I really appreciate your help.
Jan 30 '15 #1

✓ answered by weaknessforcats

This code:
Expand|Select|Wrap|Line Numbers
  1. const std::string SOME_STRING = "This is a test.";
  2.  
  3.  
does create a string object. The object is created on the stack because it was not created using the new operator.

The code is really a constructor call:
Expand|Select|Wrap|Line Numbers
  1. const std::string SOME_STRING("This is a test.");
If you create your string using the new operator, then you must delete the object when you are finished with it.

Objects on the stack are managed by the compiler and they will be created and deleted by the compiler without telling you.

Objects on the heap are not deleted until you call the delete operator. These objects will exist until you are finished with them.

The engineers who favor the heap method are correct. You just need to remember to delete these objects to avoid memory leaks. Personally, I use stack variables only for local variables inside the function that I wanted deleted when the function completes.

You can even have your heap objects automatically deleted by using a smart or managed pointer. Read this: http://bytes.com/topic/c/insights/65...-smart-pointer

2 1314
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. const std::string SOME_STRING = "This is a test.";
  2.  
  3.  
does create a string object. The object is created on the stack because it was not created using the new operator.

The code is really a constructor call:
Expand|Select|Wrap|Line Numbers
  1. const std::string SOME_STRING("This is a test.");
If you create your string using the new operator, then you must delete the object when you are finished with it.

Objects on the stack are managed by the compiler and they will be created and deleted by the compiler without telling you.

Objects on the heap are not deleted until you call the delete operator. These objects will exist until you are finished with them.

The engineers who favor the heap method are correct. You just need to remember to delete these objects to avoid memory leaks. Personally, I use stack variables only for local variables inside the function that I wanted deleted when the function completes.

You can even have your heap objects automatically deleted by using a smart or managed pointer. Read this: http://bytes.com/topic/c/insights/65...-smart-pointer
Jan 30 '15 #2
thanks so much for your answer.
Jan 31 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
15
by: **Developer** | last post by:
I understand that If somestring.length = 0 Then is faster then If somestring = "" Then and is equivalent to
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
9
by: Christopher | last post by:
Code compiles, but the string evaluates to NULL in debugger. Using gcc 3.1.1. Did I not initialize the string properly or is this a compiler bug? // some.h #include <string> namespace ns {
13
by: S S | last post by:
Hi I have a very basic question, but it's a good one. Below is the code fragment. struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0;
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.