473,657 Members | 2,805 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 1386
No - it can't leak. However it is better to write
memset(MyArray, 0,sizeof(MyArra y));
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(MyArra y));
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**********@g mail.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)cybers pace.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
16682
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 (root@Pentium.suse.de) (gcc version 3.3 20030226 (prerelease) (SuSE Linux)) #1 Wed Aug 6 18:26:21 UTC 2003 Apache 1.3.27-41 PHP 4.3.1-52 MySQL 3.23.55-20
2
6257
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 later reuse, rather than returning it to the OS." In C it can be easily avoided using function extern "C" void __libc_freeres(void);
1
1702
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 extenal packages or it might be the postgres itself keeping allocated memory as a buffer for later use ? To note also that the same identical calls keeps making SIZE grow ... PS: I'm working with postgres-7.3.2
9
1207
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 resourcereader object also. We profiled the application using .NET Memory profiler and it appears that garbage collection does not appear to be happening. Even after closing all sessions memory for aspnet process remains pegged at the level it...
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 do i handle this...
2
5867
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 Visual Studio, there are printed a lot of Memory Leaks errors: Detected memory leaks! Dumping objects -> {1323} normal block at 0x00BB6F20, 4 bytes long.
7
15693
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 lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
20
1800
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
9
1612
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 LAN. "Server" is running as a Windows service. The is one thread running for one clinet in the server. Sometime the user tells me the Sever will be slow after it runs for 1-2 days.
6
5565
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 particular crash so I don't think anyone else is experiencing it but I know other users are doing exactly what she is doing because it is our most popular module. I have analyzed the dmp files from several of this user's crashes using windbg...
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.