473,657 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

will I get Memory leak..

Hi all,

void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");
fp[10]='\0';
free(fp);
}

Please refer the program for my questions..

1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?

Thanks,
Ganesh

Nov 14 '07 #1
20 1798
gNash <ga**********@g mail.comwrote:
void main()
This is wrong. It should be

int main()

or by preference,

int main(void)
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");
This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.
fp[10]='\0';
free(fp);
These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.
}
Richard
Nov 14 '07 #2
On Nov 14, 12:31 pm, gNash <ganeshamu...@g mail.comwrote:
Hi all,

void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");
fp[10]='\0';
free(fp);
}

Please refer the program for my questions..

1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?
Richard Bos has answered your main questions. I will simply
add that the character with value 0 is referred to as NUL not
NULL. For detecting memory leaks valgrind has a good reputation.

Nov 14 '07 #3
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
gNash <ganeshamu...@g mail.comwrote:
void main()

This is wrong. It should be

int main()

or by preference,

int main(void)
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");

This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.
fp[10]='\0';
free(fp);

These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.
}

Richard

Hi Richard..

the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.

Thankingyou,

Nov 14 '07 #4
gNash wrote:
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>gNash <ganeshamu...@g mail.comwrote:
>> void main()
This is wrong. It should be

int main()

or by preference,

int main(void)
>> {
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");
This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.
>> fp[10]='\0';
free(fp);
These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.
>> }
Richard


Hi Richard..

the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.
He did. He justed added extra advice which you clearly need, since you
weren't following it.
Nov 14 '07 #5
On Nov 14, 5:50 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
gNash wrote:
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
gNash <ganeshamu...@g mail.comwrote:
void main()
This is wrong. It should be
int main()
or by preference,
int main(void)
> {
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");
This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.
> fp[10]='\0';
free(fp);
These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.
> }
Richard
Hi Richard..
the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.

He did. He justed added extra advice which you clearly need, since you
weren't following it.

Thanks for advice.. but i have little confusion when i add '\0' and
free() will clear whole memory since '\0' representing end of array.
am i right?


Nov 14 '07 #6
gNash <ga**********@g mail.comwrites:
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");
You should get into the habit now or always checking the return from
malloc. Also, 26 is not enough for this string. You need space for
27 bytes.
fp[10]='\0';
free(fp);
}

Please refer the program for my questions..

1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??
No.
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
Yes. BTW, NULL is not the same as '\0'. That is a null byte.
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?
There is an excellent tool called "valgrind" that can do this (and
more) to help you find memory allocation errors. splint (and friends)
can tell you about possible portability issues and about errors that
can be detected without running your program.

--
Ben.
Nov 14 '07 #7
On Nov 14, 5:59 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
gNash <ganeshamu...@g mail.comwrites:
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");

You should get into the habit now or always checking the return from
malloc. Also, 26 is not enough for this string. You need space for
27 bytes.
fp[10]='\0';
free(fp);
}
Please refer the program for my questions..
1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??

No.
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??

Yes. BTW, NULL is not the same as '\0'. That is a null byte.
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?

There is an excellent tool called "valgrind" that can do this (and
more) to help you find memory allocation errors. splint (and friends)
can tell you about possible portability issues and about errors that
can be detected without running your program.

--
Ben.

Thank you all.. i am clear..

Thank you again..
Ganesh.

Nov 14 '07 #8
On Nov 14, 5:59 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
gNash <ganeshamu...@g mail.comwrites:
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCD EFGHIJKLMNOPQRS TUVWXYZ");

You should get into the habit now or always checking the return from
malloc. Also, 26 is not enough for this string. You need space for
27 bytes.
fp[10]='\0';
free(fp);
}
Please refer the program for my questions..
1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??

No.
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??

Yes. BTW, NULL is not the same as '\0'. That is a null byte.
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?

There is an excellent tool called "valgrind" that can do this (and
more) to help you find memory allocation errors. splint (and friends)
can tell you about possible portability issues and about errors that
can be detected without running your program.

--
Ben.

Could you any one please tell me how is free() is working?
Nov 14 '07 #9
gNash wrote:
....
Thanks for advice.. but i have little confusion when i add '\0' and
free() will clear whole memory since '\0' representing end of array.
am i right?
No. You are confusing three different things: strings, arrays, and
dynamic memory allocation.
Many C library functions read or write null-terminated strings. The
free() function is NOT one of them.

Arrays have a length that can be determined at the point of declaration.
Writing a null character to an array does not change its size, it merely
affects how much of that array will be read by routines that take
strings as input.

Dynamic memory allocations have a size that is determined solely by the
call to malloc(), calloc(), or realloc() that allocated the memory. The
size is not changed by what you write into that memory, not even if what
you write is a null character. The free() function deallocates the
entire amount of memory allocated.
Nov 14 '07 #10

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

Similar topics

10
8702
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to look for uncollectable objects. I've used the next: # at the beginning of code gc.enable()
8
3406
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 ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
4
6078
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"; scope.Path.Path=@"\\pc\root\cimv2";
20
8091
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
13
1536
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete the reference to my object from one of its shared lists.And since the operation repeats many times the leak is huge. Is there a way to kill my object anyway? Thanks a lot, Boni
3
5306
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
0
1665
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. If you are interested in how builtin/pure-python inheritance interacts
9
311
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. */ }
22
9331
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 memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
8395
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
8310
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
8826
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
8732
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
8503
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,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
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.