Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > will I get Memory leak..

Reply
Thread Tools

will I get Memory leak..

 
 
James Kuyper
Guest
Posts: n/a
 
      11-14-2007
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.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-14-2007
gNash <(E-Mail Removed)> writes:

> On Nov 14, 5:59 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>> gNash <(E-Mail Removed)> writes:

<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.
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      11-14-2007
gNash <(E-Mail Removed)> wrote:

> On Nov 14, 5:34 pm, (E-Mail Removed) (Richard Bos) wrote:
> > gNash <(E-Mail Removed)> wrote:
> > > 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
 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 6:22 pm, James Kuyper <(E-Mail Removed)> wrote:
> 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 ?

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      11-14-2007
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.
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      11-14-2007
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.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      11-14-2007
Ben Bacarisse <(E-Mail Removed)> writes:

> gNash <(E-Mail Removed)> writes:
>
>> On Nov 14, 5:59 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>>> gNash <(E-Mail Removed)> writes:

> <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
 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 6:27 pm, (E-Mail Removed) (Richard Bos) wrote:
> gNash <(E-Mail Removed)> wrote:
> > On Nov 14, 5:34 pm, (E-Mail Removed) (Richard Bos) wrote:
> > > gNash <(E-Mail Removed)> wrote:
> > > > 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



 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 6:27 pm, (E-Mail Removed) (Richard Bos) wrote:
> gNash <(E-Mail Removed)> wrote:
> > On Nov 14, 5:34 pm, (E-Mail Removed) (Richard Bos) wrote:
> > > gNash <(E-Mail Removed)> wrote:
> > > > 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.

 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-14-2007
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 <at> doc.ic.ac.uk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why does this memory intensive C++ program get poor memory accessspeed? Ian Collins C++ 1 03-28-2010 10:51 PM
How do I get an out-of-memory error memory usage dump? Todd Java 4 09-05-2007 03:08 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Memory Control - Can you get the memory usage of the interpreter? Ian Python 3 07-10-2004 09:16 PM



Advertisments