473,320 Members | 1,955 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,320 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 3180
> 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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.