will I get Memory leak..

G

gNash

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
 
R

Richard Bos

gNash said:
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
 
S

Spiros Bousbouras

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.
 
G

gNash

gNash said:
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,
 
J

James Kuyper

gNash said:
gNash said:
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.
 
G

gNash

gNash said:
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?
 
B

Ben Bacarisse

gNash said:
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.
 
G

gNash

gNash said:
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.


Thank you all.. i am clear..

Thank you again..
Ganesh.
 
J

James Kuyper

I strongly recommend re-reading Richard Bos' response, but from your
comments about it I thought you might benefit from a response that more
directly addresses your questions.
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.. ??

Writing a null character into dynamically allocated memory will not, in
itself, cause a memory leak.

The problem with your code, on the other hand, is that your call to
strcpy() writes a null character to fp[26], which is one position past
the end of the allocated memory. As a result of that error, the behavior
of your entire program is undefined, which means that anything could go
wrong A memory leak is very definitely a possibility from making that
kind of mistake, but most of the other possible consequences of that
mistake are much worse than memory leaks.
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??

Your code wrote one null value at a position one past the end of the
allocated memory, and a second null character at the beginning of the
allocation. It didn't write any null characters into the middle of the
allocation. Calling free() does not delete memory.

Writing null characters into the allocated memory has no affect on the
behavior of free(), regardless of where you write them, so long as it is
inside the allocation. Because of your mistake, the call to free() could
do anything, including sending insulting e-mail to your mother. However,
if you hadn't written a null character one past the end of the allocated
memory, the behavior of free() would have been to deallocate the memory,
making it available for further allocation.
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 occur at run time, not at compile time. Compilers can't
catch memory leaks, though some of the better ones can catch some of the
mistakes that result in memory leaks.

There are compilers that provide options that provide run-time help with
leak detection. There are debugging versions of the malloc() family of
functions. There are tools which run your program inside an environment
that allows you to monitor memory allocations and detect links. However,
in order to recommend an appropriate tool, we need to know what compiler
and operating system you're using.
 
G

gNash

gNash said:
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.


Could you any one please tell me how is free() is working?
 
J

James Kuyper

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.
 
B

Ben Bacarisse

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.
 
R

Richard Bos

gNash said:
gNash said:
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.
the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.

I did. Learn to read. And learn not to be so bleedin' demanding. You
don't get to order me around unless you pay me a damn sight more than
you can afford.

Richard
 
G

gNash

gNash wrote:

...


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 ?
 
M

Mark Bluemel

gNash said:
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.
 
M

Mark Bluemel

gNash said:
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.
 
G

gNash

gNash said:
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.
}
the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.

I did. Learn to read. And learn not to be so bleedin' demanding. You
don't get to order me around unless you pay me a damn sight more than
you can afford.

Richard
 
G

gNash

gNash said:
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.
}
the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.

I did. Learn to read. And learn not to be so bleedin' demanding. You
don't get to order me around unless you pay me a damn sight more than
you can afford.

Richard

It was request... i was not order.. sorry if do you feel so..

Gnash.
 
P

Philip Potter

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top