will I get Memory leak..

M

Mark Bluemel

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

Mark Bluemel

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

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

Martin Ambuhl

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

Martin Ambuhl

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.

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

Keith Thompson

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

Richard

Ben Bacarisse said:
Pedantry is catching. There are two "2nd editions". One that
pre-dates the ANSI standard (1988) and one that post-dates it. That
later one is what most people mean by "2nd edition". It has "ANSI C"
in big red letters stamped over the title.

It is probably quite hard to get any other edition these days.

Well, I never knew! Thanks.
 
K

Keith Thompson

Ben said:
Pedantry is catching. There are two "2nd editions". One that
pre-dates the ANSI standard (1988) and one that post-dates it. That
later one is what most people mean by "2nd edition". It has "ANSI C"
in big red letters stamped over the title.

It is probably quite hard to get any other edition these days.

Do the two "2nd editions" differ significantly? The only difference I'm aware
of is the big red stamp; a few minor errors may also have been corrected. I
*think* they're basically just two different printings.
 
B

Ben Bacarisse

Keith Thompson said:
Do the two "2nd editions" differ significantly? The only difference
I'm aware of is the big red stamp; a few minor errors may also have
been corrected. I *think* they're basically just two different
printings.

Yes, they are all relatively minor changes. The main change is indeed
to the cover!
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top