By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

will I get Memory leak..

P: n/a
Hi all,

void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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
Share this Question
Share on Google+
20 Replies


P: n/a
gNash <ga**********@gmail.comwrote:
void main()
This is wrong. It should be

int main()

or by preference,

int main(void)
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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

P: n/a
On Nov 14, 12:31 pm, gNash <ganeshamu...@gmail.comwrote:
Hi all,

void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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

P: n/a
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
gNash <ganeshamu...@gmail.comwrote:
void main()

This is wrong. It should be

int main()

or by preference,

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

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

P: n/a
gNash wrote:
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>gNash <ganeshamu...@gmail.comwrote:
>> void main()
This is wrong. It should be

int main()

or by preference,

int main(void)
>> {
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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

P: n/a
On Nov 14, 5:50 pm, James Kuyper <jameskuy...@verizon.netwrote:
gNash wrote:
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
gNash <ganeshamu...@gmail.comwrote:
void main()
This is wrong. It should be
int main()
or by preference,
int main(void)
> {
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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

P: n/a
gNash <ga**********@gmail.comwrites:
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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

P: n/a
On Nov 14, 5:59 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
gNash <ganeshamu...@gmail.comwrites:
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

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

P: n/a
On Nov 14, 5:59 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
gNash <ganeshamu...@gmail.comwrites:
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

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

P: n/a
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

P: n/a
gNash <ga**********@gmail.comwrites:
On Nov 14, 5:59 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>gNash <ganeshamu...@gmail.comwrites:
<snip>
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.
<snip>
Could you any one please tell me how is free() is working?
Only by looking at the code for every implementation! It just works
(if used correctly). If you get a copy of K&R ("The C Programming
Language" by Kernighan and Ritchie) you will find an explanation of
one way to implement malloc and free as well as having one of the best
book on C there is.

--
Ben.
Nov 14 '07 #11

P: n/a
On Nov 14, 6:22 pm, James Kuyper <jameskuy...@verizon.netwrote:
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.

What will happen if would i say free(str+2)?? will i get leak ?

Nov 14 '07 #12

P: n/a
gNash wrote:
Could you any one please tell me how is free() is working?
If passed a pointer to a piece of memory which was allocated with
malloc(), calloc() or realloc(), which has not already been freed by a
call to free() or realloc, free() returns that memory to the pool which
malloc() etc allocate from.

if passed a NULL pointer, free() does nothing.

If passed any other pointer, the behaviour is undefined.

If you want to know how free() accomplishes this, you are asking about
the implementation details, which differ - you'd have to refer to the
source code for a specific malloc()/calloc()/realloc()/free()
implementation.
Nov 14 '07 #13

P: n/a
gNash wrote:
>
What will happen if would i say free(str+2)?? will i get leak ?
Read the specification for free() - if you pass it a pointer which isn't
the starting address of a malloc()/calloc()/realloc() allocated area,
the behaviour is undefined.

free() is an "all or nothing" operation - you can't free() all but 2
bytes of a malloc()ed space.
Nov 14 '07 #14

P: n/a
Mark Bluemel wrote:
gNash wrote:
>>
What will happen if would i say free(str+2)?? will i get leak ?
Read the specification for free() - if you pass it a pointer which isn't
the starting address of a malloc()/calloc()/realloc() allocated area,
the behaviour is undefined.

free() is an "all or nothing" operation - you can't free() all but 2
bytes of a malloc()ed space.
-- but if this is what you want to do, look up the realloc() function.

--
Philip Potter pgp <atdoc.ic.ac.uk
Nov 14 '07 #15

P: n/a
gNash wrote:
Hi all,

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

Please refer the program for my questions..
Your program has bugs which have already been pointed out.
1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??
Yes, of course someone can tell you that. No, you won't get a memory
leak.
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
free() doesn't care what's in the memory pointed to by its argument.
It has other ways, which vary according to the implementation of the
library, of telling how large a region of memory to free.
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?
Memory leaks are dynamic. Compiler options and splint can only perform
static analysis. Tools such as valgrind can perform certain dynamic
analyses and detect (at least some) memory leaks.
Nov 14 '07 #16

P: n/a
gNash wrote:
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>gNash <ganeshamu...@gmail.comwrote:
>> void main()
This is wrong. It should be

int main()

or by preference,

int main(void)
>> {
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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
the int main(void) and malloc(26+1) it is not my doubt
But these, particularly the inadequate allocation, are issues which
should be brought to your attention.
my doubts
are posted clearly.. Please reply for it.
Richard answered your original question - the assignment of a null
character to fp[10] should cause no problems.
Nov 14 '07 #17

P: n/a
gNash wrote:
Hi all,

void main()
^^^^
main returns an int. The failure to get the most fundamental part of
a C program right rather marks one as incompetent.
{
char *fp;
fp=malloc(26);
^^^^^^
no declaration for malloc in scope.
The function malloc returns a (void *) and the inclusion
of <stdlib.hwould provide the correct declaration. Without it, malloc
is assumed to return an int, which is an error.
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
^^^^^^^^^^^^^^^^^^^^^^^^^^
This string occupies 27 chars. You have allocated
only 26 for the target. This is an error.
fp[10]='\0';
free(fp);
It is always a good idea to return a value from a function
which returns one. main returns an int. Unless you have a C99 compiler
you must return one in a program expected to be portable.
}

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, but your program was already dead before that.
free() doesn't care what values you stored in the allocated space.
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
No, but your program was already dead before that.
free() doesn't care what values you stored in the allocated space.
Avoid using NULL in inappropriate places. NULL is a pointer
constant. A char with the value 0 might be called a null char,
but not a NULL; and, for the special case of programs using ASCII
encoding, the ASCII character with the value 0 is named NUL, not
NULL.
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?
By being sure to free all dynamically allocated memory.
There are tools to use, but they are to be used in conjunction
with, not instead of, careful programming.
Nov 14 '07 #18

P: n/a
gNash wrote:
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>gNash <ganeshamu...@gmail.comwrote:
>> void main()
This is wrong. It should be

int main()

or by preference,

int main(void)
>> {
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
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.
When you post code which has serious errors with regard to very basic
aspects of C, those _should_ be the "doubts" that you concern yourself
with. Your imperious demand for specific answers smacks of an attempt
to impose a master-slave relationship on people who are trying to help
you. It is not a way to ever get help. It certainly will win you no
friends.
Nov 14 '07 #19

P: n/a
Martin Ambuhl wrote:
[...]
It is always a good idea to return a value from a function
which returns one. main returns an int. Unless you have a C99 compiler
you must return one in a program expected to be portable.
[...]

To be clear: "one" is a pronoun here, not a number. In other words, you should
return an integer from main. Unless you want to indicate that an error
occurred, you should add a "return 0;" before the closing "}". (To indicate an
error, use "return EXIT_FAILURE;" or "exit(EXIT_FAILURE);"; exit() and
EXIT_FAILURE are declared in <stdlib.h>.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 14 '07 #20

P: n/a
Ben Bacarisse <be********@bsb.me.ukwrites:
gNash <ga**********@gmail.comwrites:
>On Nov 14, 5:59 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>>gNash <ganeshamu...@gmail.comwrites:
<snip>
> 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.
<snip>
>Could you any one please tell me how is free() is working?

Only by looking at the code for every implementation! It just works
(if used correctly). If you get a copy of K&R ("The C Programming
Language" by Kernighan and Ritchie) you will find an explanation of
one way to implement malloc and free as well as having one of the best
book on C there is.
slight pedant point : the second edition!

http://www.amazon.com/C-Programming-.../dp/0131103628
Nov 15 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.