simple question about malloc & free 
Author Message
 simple question about malloc & free

char *ptr;
ptr = malloc(sizeof(char) * 256);
ptr += 100;
free(ptr);

Since I've changed the address of ptr, is this going to cause problems
freeing the memory?



Sun, 28 May 2000 03:00:00 GMT  
 simple question about malloc & free


in comp.lang.c:

=> char *ptr;
=> ptr = malloc(sizeof(char) * 256);
=> ptr += 100;
=> free(ptr);
=>
=> Since I've changed the address of ptr, is this going to cause problems
=> freeing the memory?

Yes.

Instead do:

char *ptr;
char *ptr2;
ptr = malloc(sizeof(char) * 256);
ptr2 = ptr + 100;
free(ptr);

But remember that ptr2 is also invalid after the free().

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Be Different -
        THINK!

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

http://mroot.home.mindspring.com



Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free

On Wed, 10 Dec 1997 04:46:40 -0600, "Joe Shmoe"

Quote:

>char *ptr;
>ptr = malloc(sizeof(char) * 256);

sizeof(char) is 1, by definition.

Quote:
>ptr += 100;
>free(ptr);

>Since I've changed the address of ptr, is this going to cause problems
>freeing the memory?

You bet!

==
Miguel Carrasquer Vidal                     ~ ~
Amsterdam                   _____________  ~ ~

========================== Ce .sig n'est pas une .cig



Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free


: On Wed, 10 Dec 1997 04:46:40 -0600, "Joe Shmoe"

: >char *ptr;
: >ptr = malloc(sizeof(char) * 256);

: sizeof(char) is 1, by definition.

: >ptr += 100;
: >free(ptr);
: >
: >Since I've changed the address of ptr, is this going to cause problems
: >freeing the memory?

: You bet!

: ==
: Miguel Carrasquer Vidal                     ~ ~
: Amsterdam                   _____________  ~ ~

: ========================== Ce .sig n'est pas une .cig

        evident intent is achieved
        by use of
                realloc()
        ...

--

Ralph Silverman



Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free

Quote:


>: On Wed, 10 Dec 1997 04:46:40 -0600, "Joe Shmoe"

>: >char *ptr;
>: >ptr = malloc(sizeof(char) * 256);
>: sizeof(char) is 1, by definition.
>: >ptr += 100;
>: >free(ptr);
>: >
>: >Since I've changed the address of ptr, is this going to cause problems
>: >freeing the memory?
>    evident intent is achieved
>    by use of
>            realloc()
>    ...

realloc() can be used to shorten an allocated chunk of memory, but I
am not aware of a use of realloc that makes it possible to use just the
"rest" of an allocated chunk. I doubt very strongly that there is a
reasonable connection between realloc() and what happens in the
above piece of code.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2178-2781



Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free


Quote:


>>        evident intent is achieved
>>        by use of
>>                realloc()
>>        ...

>realloc() can be used to shorten an allocated chunk of memory, but I
>am not aware of a use of realloc that makes it possible to use just the
>"rest" of an allocated chunk. I doubt very strongly that there is a
>reasonable connection between realloc() and what happens in the
>above piece of code.

I think Silverman is right this time, at least if the intent is to
keep the first 100 chars and free the rest. They may not end up
in their original address, but that doesn't seem to matter here.

Ulric
--
"You say to-mah-to, I say to-mah-to, and he says to-mah-to,
 but ISO disagrees with all three of us and says to-mah-to."



Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free

Quote:

> char *ptr;
> ptr = malloc(sizeof(char) * 256);
> ptr += 100;
> free(ptr);

> Since I've changed the address of ptr, is this going to cause problems
> freeing the memory?

Yes, serious problems. When you call free you must pass a pointer that
was returned to you by malloc, calloc, or realloc, or a NULL pointer.
        -- Pete


Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free


Quote:

> : On Wed, 10 Dec 1997 04:46:40 -0600, "Joe Shmoe"

> : >char *ptr;
> : >ptr = malloc(sizeof(char) * 256);

> : sizeof(char) is 1, by definition.

> : >ptr += 100;
> : >free(ptr);
> : >
> : >Since I've changed the address of ptr, is this going to cause problems
> : >freeing the memory?

> : You bet!

>    evident intent is achieved
>    by use of
>            realloc()
>    ...

How's that, Ralph? The intent seems to be, to allocate some memory, play
around with the resulting pointer a bit, then free the original block of
memory. How realloc() accomplishes this, I don't really know. With the
exception that his free() was incorrect, the code the original poster had
was right. He just needed to make a copy and modify -that- instead of the
original, then go back and free the original.

[-                               firewind                                   -]

[-          "You're just jealous because the voices talk to -me-."          -]
[-                   Have a good day, and enjoy your C.                     -]
[-          (on a crusade of grumpiness where grumpiness is due)            -]



Tue, 30 May 2000 03:00:00 GMT  
 simple question about malloc & free

Quote:

> char *ptr;
> ptr = malloc(sizeof(char) * 256);
> ptr += 100;
> free(ptr);

> Since I've changed the address of ptr, is this going to cause problems
> freeing the memory?

Absolutely.  do this instead:

char *ptr, *bptr;
bptr = malloc(256); /* sizeof( char ) is 1 by definition. */
ptr = bptr+100;
/* some code. */
free( bptr );  /* Free the memory, never change bptr. */
ptr = NULL;    /* Nullify any pointers based on a free()ed pointer. */

Make sure that any pointers pointing into memory that you have freed are
assigned either a NULL pointer value or some other valid value.  Any
effort to dereference or access a pointer into free()ed memory is
undefined behavior, its value is indeterminant and may cause you system
to crash.

e.g.
char *ptr, *bptr;
bptr = malloc(256);
ptr = bptr+100;
free( bptr );  

if( ptr == NULL ) { /* this produces undefined behavior. */

The value of ptr is indeterminant.  You can not use it to access memory,
you can't even compare its value with another (Really, how can you
compare a value whan that value is indeterminant - unable to be
determined)!

--
*****************************************************************
If you {*filter*}a smurf, what color does it turn?
What happens if you get scared half to death twice?
Energizer Bunny arrested, charged with battery.
I spilled Spot remover on my dog. Now he's gone.
I used to have an open mind but my brains kept falling out.
=========================================

=========================================
READ THE FAQ for more information:
   C-FAQ ftp sites: ftp://ftp.eskimo.com or ftp://rtfm.mit.edu
   Hypertext C-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html



Thu, 01 Jun 2000 03:00:00 GMT  
 simple question about malloc & free



Quote:

>> char *ptr;
>> ptr = malloc(sizeof(char) * 256);
>> ptr += 100;
>> free(ptr);

>> Since I've changed the address of ptr, is this going to cause problems
>> freeing the memory?

>Absolutely.  do this instead:

>char *ptr, *bptr;
>bptr = malloc(256); /* sizeof( char ) is 1 by definition. */
>ptr = bptr+100;
>/* some code. */
>free( bptr );  /* Free the memory, never change bptr. */
>ptr = NULL;    /* Nullify any pointers based on a free()ed pointer. */

>Make sure that any pointers pointing into memory that you have freed are
>assigned either a NULL pointer value or some other valid value.  Any
>effort to dereference or access a pointer into free()ed memory is
>undefined behavior, its value is indeterminant and may cause you system
>to crash.

The same is true for trying to dereference a null pointer. If the code tries
to dereference a pointer to freed memory then the program has a logic fault.
The program logic needs to be fixed rather then propped up like this. Also
it may not be possible to write to all pointers to a freed block since the
section of code in question may not know where they all are or have access
to them.

Quote:
>e.g.
>char *ptr, *bptr;
>bptr = malloc(256);
>ptr = bptr+100;
>free( bptr );  

While I'm not advocating this as good style it is worth noting that

 free(ptr-100);

is valid too.

--
-----------------------------------------


-----------------------------------------



Thu, 01 Jun 2000 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. SIMPLE malloc & pointer question

2. malloc & free in c#

3. Malloc & free acting strange

4. malloc&free

5. malloc/calloc & free

6. malloc, realloc, free questions

7. malloc/free questions

8. malloc - free question

9. malloc/free question

10. Dumb question concerning malloc/free

11. a question about malloc and free

12. basic malloc/free question

 

 
Powered by phpBB® Forum Software