Link to home
Create AccountLog in
C++

C++

--

Questions

--

Followers

Top Experts

Avatar of dhananjay_n
dhananjay_n

can static_cast lead to memory leak??
can static_cast lead to memory leak.
i have used static_cast at many places in my code can it lead to memory leak if used in a improper way

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of ZoppoZoppo🇩🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of evilrixevilrix🇬🇧

You don't need static_cast (or any cast) to convert any pointer type to void.
#include <string>
int main(void)
{
	std::string *pString = new std::string;
	*pString = "Hello world!";
	void* pVoid = pString;
	delete pVoid;
}

Open in new window


Avatar of dhananjay_ndhananjay_n

ASKER

thanks a lot for ur help n support
i have got my answer

Avatar of evilrixevilrix🇬🇧

>> i have used static_cast at many places in my code can it lead to memory leak if used in a improper way
Apart from casting void pointers to non-void pointers (and vice versa) static cast will not allow you to cast unrelated pointer types. For this you should be using reinterpret_cast. For down-casting (from a polymorphic super to a sub class) you would normally use dynamic_cast to confirm the super IS_A sub class relationship.

I'm sure you could contrive a way (if you really tried) to leak memory, but I can't think of a normal and reasonable use for static_cast that would do this.

What I'd be more worried about is that you need to use "static_cast at many places". The use of cast is rarely required if code is structured right. It's usage coerces the compiler into ignoring potential problems in your code and can shadow issues. Are you really sure all the times you've used cast you've really needed to? Are you sure you couldn't find a better way?

-Rx.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of evilrixevilrix🇬🇧

additional...

The one to take real care with is const_cast. If you use this to cast away constness on a type that was originally defined as const and then you try to modify it you'll almost certainly cause a  program crash. This is because data types declared const are frequently stored in read-only memory. It is; however, safe to cast away constness on a type that has acquired const (say, through the call stack) but wasn't originally defined as const. This is because the original type will live in read/write memory. Again; however, the need to use const_cast (except when interfacing to 3rd party APIs) is usually indicative of poor design -- your code isn't const correct.
C++

C++

--

Questions

--

Followers

Top Experts

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.