473,387 Members | 1,624 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.

Will this leak memory?

MyClass::foo ( const std::string str) {
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...

...
//Do I need a free(s) here to free memory allocated ?
}

Tks

Jul 23 '05 #1
5 3187
> MyClass::foo ( const std::string str) {

Passing by constant value is unnecessary and misleading at best.
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...
Perhaps, but that is not your business. This should be

const char *s = std.c_str();

What std::string::c_str() returns is owned by the string and you may
not modify it in any way.
//Do I need a free(s) here to free memory allocated ?


No. That's illegal/unnecessary/bad for your health.
Jonathan

Jul 23 '05 #2
"Jonathan Mcdougall" <jo***************@gmail.com> schrieb:
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...


const char *s = std.c_str();


Because the pointer is defined as const it will *never* cause any
leak. You can call c_str() as often as you want.

T.M.
Jul 23 '05 #3
> > > char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...
const char *s = std.c_str();


Because the pointer is defined as const it will *never* cause any
leak.


It will never cause any leak, but that's not because it is const (you
can delete const pointers), it's because the standard says it.
You can call c_str() as often as you want.


Yes.
Jonathan

Jul 23 '05 #4


Alfonzo Morra wrote:
MyClass::foo ( const std::string str) {
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...

...
//Do I need a free(s) here to free memory allocated ?
}

Tks


Thanks guys !

Jul 23 '05 #5
* Alfonzo Morra:
MyClass::foo ( const std::string str) {
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...

...
//Do I need a free(s) here to free memory allocated ?
}


First, to clear up some remarks made by another poster:

* Adding 'const' to pass-by-value can be helpful to detect the
common '=' instead of '==' typo. But be aware that the 'const'
does not, in this case, affect the function signature. Also,
it may be more efficient to pass by reference, i.e.

std::string const& str

and that is how it's usually done.

* The declaration of 's' should be

char const* const s = str.c_str();

The first 'const' refers to the contents of the string; you can
get away without it only because of an old C compatibility feature
stemming from the time before 'const' was introduced. The second
'const' refers to the pointer itself, ensuring that this pointer
is not reassigned to point to something else.

* The 'const's have nothing to do with memory allocation/deallocation.

The pointer you obtain is valid until you change the contents of 'str'; it
is the 'str' object that is responsible for deallocation, if any is needed.

Since 'str' is 'const' and by value a change of 'str' won't happen, so the
pointer is guaranteed to be valid till the end of the function body.

When you pass by reference you run the risk of aliasing, that in some way
the code may change the contents of 'str' via some non-'const' reference,
e.g. a global reference, and in that case the pointer can be become invalid.
However, ordinarily that's not a problem. Strings are simply not used in
ways that can ordinarily cause aliasing, and anyway, globals are Evil... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
9
by: Joakim Hove | last post by:
Hello, I have the following code: foo_ptr * alloc_foo(int size, ...) { /* This function allocates an instance of the foo_ptr type, and returns a pointer to the newly allocated storage. */ }
20
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.