Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > freeing allocated memory

Reply
Thread Tools

freeing allocated memory

 
 
binaya
Guest
Posts: n/a
 
      10-19-2003
Dear all,

Say I allocate a block of memory using the command malloc.I
have a question. Can I deallocate certain portion of it only during
runtime ?

For eg:

Say I allocate 5 pointers to int as below.

link=(int*)malloc(5*sizeof(int))



But during runtime, I find that I only need the front 3 of them.ie
I only need link,link+1 and link+2.

If I don't need link+3 and link+4, can I free these last two
pointers to int using the command free(link+3) ?



If not, then what is the best way to do so ?
Thanking you in advance.
 
Reply With Quote
 
 
 
 
James Hu
Guest
Posts: n/a
 
      10-19-2003
On 2003-10-19, binaya <(E-Mail Removed)> wrote:
> Say I allocate a block of memory using the command malloc.I
> have a question. Can I deallocate certain portion of it only during
> runtime ?


If you want to shrink the amount of memory you initially allocated,
you can use realloc().

-- James
 
Reply With Quote
 
 
 
 
Robert Stankowic
Guest
Posts: n/a
 
      10-19-2003

"binaya" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
> Dear all,
>
> Say I allocate a block of memory using the command malloc.I
> have a question. Can I deallocate certain portion of it only during
> runtime ?
>
> For eg:
>
> Say I allocate 5 pointers to int as below.
>
> link=(int*)malloc(5*sizeof(int))
>
>
>
> But during runtime, I find that I only need the front 3 of them.ie
> I only need link,link+1 and link+2.
>
> If I don't need link+3 and link+4, can I free these last two
> pointers to int using the command free(link+3) ?
>
>
>
> If not, then what is the best way to do so ?
> Thanking you in advance.


#include <stdlib.h>

int main(void)
{
int *tmp;
int *my_array = malloc(5 * sizeof *my_array);

if(my_array)
{
my_array[0] = 100;
my_array[1] = 200;
my_array[2] = 300;
/*now you find out, you need just 3 ints*/
tmp = realloc(my_array, 3 * sizeof *my_array);
if(tmp)
{
my_array = tmp;
/*use the array here, it's now just 3 ints long, however, the
content of the first three ints of the original array are preserved*/
}
else
{
/*realloc failed, but your allocated 5 ints are still available*/
}
}
else
{
/*your request failed, take some sensible action*/
return EXIT_FAILURE;
}
free(my_array);
return EXIT_SUCCESS;
}

HTH
Robert


 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      10-19-2003

On Sun, 19 Oct 2003, James Hu wrote:
>
> On 2003-10-19, binaya <(E-Mail Removed)> wrote:
> > Say I allocate a block of memory using the command malloc.I
> > have a question. Can I deallocate certain portion of it only during
> > runtime ?

>
> If you want to shrink the amount of memory you initially allocated,
> you can use realloc().


But you can only shrink it from the front. That is, you can do

int *p = malloc(5 * sizeof *p); /* allocate 5 ints */

p = realloc(p, 3 * sizeof *p); /* shrink to only first 3 ints */

but you absolutely *cannot* do

int *p = malloc(5 * sizeof *p); /* allocate 5 ints */

p = realloc(p+2, 3 * sizeof *p); /* shrink to last 3 ints */

because the pointer 'p+2' didn't come from a call to malloc() --
only the pointer 'p' itself did. If you want to copy from the
end of the array, you have to do it explicitly via memcpy() or
the like:

int *p, *t;
p = malloc(5 * sizeof *p);

t = malloc(3 * sizeof *t);
memcpy(t, p, 3 * sizeof *t); /* copy last 3 ints into t */
free(p); /* avoid a memory leak */
p = t;


Note that in all the examples above, I left out the proper
error checking. After each call to malloc or calloc, remember
to check the returned pointer for NULL:

int *p;

p = malloc(n * sizeof *p);
if (p == NULL)
printf("Whoops! Out of memory!\n");
else
(...)
free(p);

And remember that if realloc() returns NULL, the original block
of memory is *still* *allocated* -- so use a temporary variable!

int *p, *t;

p = malloc(n * sizeof *p);
if (p != NULL) {
t = realloc(p, k * sizeof *p);
if (t != NULL)
p = t;
else
printf("No memory for realloc, but p is still valid!\n");
}
free(p);


HTH,
-Arthur

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (binaya) wrote:

Others have already responded; however, one note:

> Say I allocate 5 pointers to int as below.


You don't.

> link=(int*)malloc(5*sizeof(int))


You allocate memory to hold five ints.

Consider:

/* allocate memory for five ints: */
int *foo;
foo = malloc( 5 * sizeof *foo );

/* allocate memory for five pointers to int: */
int **bar;
bar = malloc( 5 * sizeof *bar );

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-19-2003
binaya wrote:
> Dear all,
>
> Say I allocate a block of memory using the command malloc.I
> have a question. Can I deallocate certain portion of it only during
> runtime ?


If you allocate a block of memory using the function [not 'command']
malloc, you can resize the allocation with the function realloc.
Deallocation (for which the function free is used) of part of it is not
possible.

> For eg:
>
> Say I allocate 5 pointers to int as below.
>
> link=(int*)malloc(5*sizeof(int))


It is better to use a form like
link = malloc(5 * sizeof *link);
The cast is useless and can mask errors and the sizeof(int) can bite you
should the type of link change. In fact, it has already bitten you, since
you claim that you are allocating 5 pointers to int, while your form
allocates a space for 5 ints and stores 1 pointer to it in link.

It would be better, of course, to find a way to turn the literal 5 into a
symbolic value.


> But during runtime, I find that I only need the front 3 of them.ie
> I only need link,link+1 and link+2.


recapitulation: Then reallocate with realloc.

> If I don't need link+3 and link+4, can I free these last two
> pointers to int using the command free(link+3) ?


recapitulation: No. Reallocate with realloc.

> If not, then what is the best way to do so ?


recapitulation: Reallocate with realloc.

--
Martin Ambuhl

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-19-2003
binaya wrote:
>
> Dear all,
>
> Say I allocate a block of memory using the command malloc.I
> have a question. Can I deallocate certain portion of it only during
> runtime ?
>
> For eg:
>
> Say I allocate 5 pointers to int


> But during runtime, I find that I only need the front 3 of them.


/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

/**
#define OPTIONAL
/*/
#undef OPTIONAL
/**/

#define TOTAL 5
#define FIRST 3
#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
int **pointer, **temp_pointer;
size_t index, size;

size = TOTAL;
do {
pointer = malloc(size * sizeof *pointer);
} while (!pointer && --size != 0);
if (!pointer) {
fputs("This isn't going to work\n", stderr);
exit(EXIT_FAILURE);
}
putchar('\n');
for (index = 0; size > index; ++index) {
pointer[index] = malloc(sizeof *pointer[index]);
if (!pointer[index]) {
printf("\nmalloc(sizeof *pointer[index]) "
"failed allocation attempt %d\n\n", index + 1);
while (size > ++index) {
pointer[index] = 0;
}
break;
} else {
*pointer[index] = index;
}
}
for (index = 0; size > index; ++index) {
if (pointer[index]) {
printf("*pointer[%d] is %d\n", index, *pointer[index]);
} else {
printf(" pointer[%d] is NULL\n", index);
}
}

#ifdef OPTIONAL
putchar('\n');
index = size;
while(index-- > FIRST) {
free(pointer[index]);
pointer[index] = 0;
}
for (index = 0; TOTAL > index; ++index) {
if (pointer[index]) {
printf("*pointer[%d] is %d\n", index, *pointer[index]);
} else {
printf(" pointer[%d] is NULL\n", index);
}
}
#endif

temp_pointer = pointer;
pointer = realloc(pointer, FIRST * sizeof *pointer);
if (pointer) {
printf("\npointer was reallocated from %d"
" elements, to " xstr(FIRST) " elements.\n", size);
index = FIRST;
while(index--) {
free(pointer[index]);
}
} else {
pointer = temp_pointer;
index = TOTAL;
while(index--) {
free(pointer[index]);
}
}
free(pointer);
return 0;
}

/* END new.c */

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-19-2003
pete wrote:
>
> binaya wrote:
> >
> > Dear all,
> >
> > Say I allocate a block of memory using the command malloc.I
> > have a question. Can I deallocate certain portion of it only during
> > runtime ?
> >
> > For eg:
> >
> > Say I allocate 5 pointers to int

>
> > But during runtime, I find that I only need the front 3 of them.


> /* BEGIN new.c */


> size = TOTAL;


Subsequent appearences of the TOTAL macro should be replaced by "size"

> for (index = 0; TOTAL > index; ++index) {


for (index = 0; size > index; ++index) {

> index = TOTAL;


index = size;

> /* END new.c */


--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-19-2003
pete wrote:
>
> binaya wrote:
> >
> > Dear all,
> >
> > Say I allocate a block of memory using the command malloc.I
> > have a question. Can I deallocate certain portion of it only during
> > runtime ?
> >
> > For eg:
> >
> > Say I allocate 5 pointers to int

>
> > But during runtime, I find that I only need the front 3 of them.

>
> /* BEGIN new.c */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> /**


/**/

> #define OPTIONAL
> /*/
> #undef OPTIONAL
> /**/


> #ifdef OPTIONAL
> putchar('\n');
> index = size;
> while(index-- > FIRST) {
> free(pointer[index]);
> pointer[index] = 0;
> }
> for (index = 0; TOTAL > index; ++index) {
> if (pointer[index]) {
> printf("*pointer[%d] is %d\n", index, *pointer[index]);
> } else {
> printf(" pointer[%d] is NULL\n", index);
> }
> }
> #endif


Upon further consideration,
the OPTIONAL part, is really mandatory.
Whatever you allocate, you should free.

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-19-2003
pete wrote:

> Upon further consideration,
> the OPTIONAL part, is really mandatory.
> Whatever you allocate, you should free.


/* BEGIN allocation.c */

#include <stdio.h>
#include <stdlib.h>

#define TOTAL 5
#define FIRST 3
#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
int **pointer, **temp_pointer;
size_t index, size;

size = TOTAL;
do {
pointer = malloc(size * sizeof *pointer);
} while (!pointer && --size != 0);
if (!pointer) {
fputs("This isn't going to work\n", stderr);
exit(EXIT_FAILURE);
}
putchar('\n');
for (index = 0; size > index; ++index) {
pointer[index] = malloc(sizeof *pointer[index]);
if (!pointer[index]) {
printf("\nmalloc(sizeof *pointer[index]) "
"failed allocation attempt %d\n\n", index + 1);
while (size > ++index) {
pointer[index] = 0;
}
break;
} else {
*pointer[index] = index;
}
}
for (index = 0; size > index; ++index) {
if (pointer[index]) {
printf("*pointer[%d] is %d\n", index, *pointer[index]);
} else {
printf(" pointer[%d] is NULL\n", index);
}
}
putchar('\n');
index = size;
while(index-- > FIRST) {
free(pointer[index]);
pointer[index] = 0;
}
for (index = 0; size > index; ++index) {
if (pointer[index]) {
printf("*pointer[%d] is %d\n", index, *pointer[index]);
} else {
printf(" pointer[%d] is NULL\n", index);
}
}
temp_pointer = pointer;
pointer = realloc(pointer, FIRST * sizeof *pointer);
if (pointer) {
printf("\npointer was reallocated from %d"
" elements, to " xstr(FIRST) " elements.\n", size);
index = FIRST;
while(index--) {
free(pointer[index]);
}
} else {
pointer = temp_pointer;
index = size;
while(index--) {
free(pointer[index]);
}
}
free(pointer);
return 0;
}

/* END allocation.c */


--
pete
 
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
Freeing memory allocated by another function Praetorian C Programming 6 12-05-2007 09:15 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
freeing allocated memory Curley Q. C Programming 7 04-30-2004 04:09 PM
some queries on freeing memory allocated using malloc Hassan Iqbal C Programming 3 09-25-2003 02:53 PM



Advertisments