Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C - free()

Name: Anonymous 2012-12-31 14:02

If I allocate a block of memory, malloc() returns a pointer saved in variable x. I can set it free with free(x).
Now what if I don't know the pointer x anymore, but a pointer to an element within the allocated memory block - like &x[10] for example. Does free(&x[10]) do the same thing as free(x)? Or is possible to let it work that way?

Name: Anonymous 2012-12-31 14:11

No and no.

Read the source of any malloc implementation (for example: ftp://g.oswego.edu/pub/misc/malloc.c), it's not magical.

Name: Anonymous 2012-12-31 14:31

Some segmented architectures such as 16-bit Windows, OS/2, and AS/400 have ways to allocate a block of memory in its own segment. Freeing the memory only needs the segment part and not the offset part, so it would work how you describe, but most allocations require the exact address even on a segmented system because a 64KB or 16MB segment is split up into smaller allocations.

Name: Anonymous 2012-12-31 16:15

>>1
free(&x[10])
I'm afraid you've got the wrong rascal.

Name: Anonymous 2012-12-31 16:17

RASKAL THE DEAD PUPPY

Name: Anonymous 2012-12-31 16:27

rof ur god's god is ded

Name: Anonymous 2012-12-31 18:20

<b>yo</b>

Name: Anonymous 2012-12-31 19:26

>>1
free(x-10) would work in that case.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List