473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Will this code leaks memory

Can someone please tell if following code leaks memory

void someFunction()
{

char MyArray[512];

memset(MyArray,0,512);

}

May 10 '06 #1
5 1378
No - it can't leak. However it is better to write
memset(MyArray,0,sizeof(MyArray));
to avoid duplicating of magic number (512).

May 10 '06 #2
* Dervish:
[non-quoting]
Please read the FAQ and the monthly welcome message.

Please quote what you're replying to.

This isn't a web forum or instant messaging.

No - it can't leak. However it is better to write
memset(MyArray,0,sizeof(MyArray));
to avoid duplicating of magic number (512).


memset is not type safe and thus should be avoided where practically
possible.

In this case, if a raw array is really needed (it seldom is), write

void someFunction()
{
char MyArray[512] = {};
}
--
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?
May 10 '06 #3
even if I don't use memset() and just declare the array then will it be
deallocated when function exits..

May 10 '06 #4
LinuxGuy wrote:
even if I don't use memset() and just declare the array then will it be
deallocated when function exits..


First, to reiterate: please QUOTE what you are replying to. Not
everyone is using Google Groups, and it helps all to follow the
conversation. (To quote automatically from GG, click "show options" and
then "reply" in the revealed header.)

Second, you seem to have either asked a question but omitted to proper
punctuation or inappropriately phrased an indicative sentence as an
interrogative. In either case, the answer is that, yes, automatic
variables and arrays are freed when they leave scope.

Cheers! --M

May 10 '06 #5
LinuxGuy <ra**********@gmail.com> wrote:
even if I don't use memset() and just declare the array then will it be
deallocated when function exits..


Your failure to post properly has already been duly noted.

In answer to your actual question, yes, your array will be deallocated
when the function exits, no matter what you do to it. If your
implementation uses a stack (it probably does), it may help to know
that space allocated on the stack is *always* freed when the function
returns. Such is the beauty of "automatic" (whatever C++ calls it)
storage.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 12 '06 #6

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

Similar topics

4
16663
by: Maurice | last post by:
Hi there, I'm experiencing big memory problems on my webserver. First on an old RedHat 7.2 system, now on an other fresh installed Suse 8.2 system: Linux version 2.4.20-4GB...
2
6243
by: Mike Krasnik | last post by:
Hello all, I've been messing with checking memory leaks last time, and it looks like some of the memory was not freed due to the fact "library's default allocators keep free memory in a pool for...
1
1696
by: strk | last post by:
Hello, I'm trying to detect memory leaks in an external postgres extensions. Since SIZE of postgres process keeps growing between function calls, does this mean there are memory leaks in the...
9
1201
by: Frank1213 | last post by:
In my ASP.NET application, we are noticing appreciable memory leaks and the main culprit appears to be System.String We use ResourceReader to read in a resource file and we close and dispose the...
10
418
by: darkStar_e2 | last post by:
Hi guys. I have an applications which unfortunately eating up all the computer resources. the this is that it is not releasing the resources (memory) that it has eaten after it was closed... how...
2
5861
by: asterixgallier | last post by:
Hello i'm having problems with log4cxx when using inside an MFC application build with visual studio 2005. The Application starts and works normaly, but while exiting, inside the DEBUG window of...
7
15671
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
20
1777
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
9
1603
by: Ryan Liu | last post by:
Hi, I use C# wrote an Client/Server application. In production environment, will be 130 clients (Windows XP) connect to a Server (Windows 2000/2003 Server) thought TCP/IP socket in a local 100M...
6
5553
by: nmehring | last post by:
I have an MFC app with 2000 users. I have one user that experiences a crash in our software anywhere from 1 to 5 times a week when opening a particular module. No other users have reported this...
0
7203
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
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
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,...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.