Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

freeing allocated memory

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

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


It's full of bugs.
The simplest way to fix the printf calls,
is to declare index and size as type int:

int **pointer, **temp_pointer, size_t index, size;

> printf("\nmalloc(sizeof *pointer[index]) "
> "failed allocation attempt %d\n\n", index + 1);


> if (pointer[index]) {
> printf("*pointer[%d] is %d\n", index, *pointer[index]);
> } else {
> printf(" pointer[%d] is NULL\n", index);
> }
> }


> if (pointer[index]) {
> printf("*pointer[%d] is %d\n", index, *pointer[index]);
> } else {
> printf(" pointer[%d] is NULL\n", index);
> }


> if (pointer) {
> printf("\npointer was reallocated from %d"
> " elements, to " xstr(FIRST) " elements.\n", size);
> index = FIRST;
> while(index--) {
> free(pointer[index]);
> }


> /* END allocation.c */


--
pete
 
Reply With Quote
 
 
 
 
James Hu
Guest
Posts: n/a
 
      10-19-2003
On 2003-10-19, Arthur J. O'Dwyer <(E-Mail Removed)> wrote:
> On Sun, 19 Oct 2003, James Hu wrote:
>>
>> 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.


I initially thought about mentioning something about this, but I thought
that any such sentence can be misinterpreted, and figured the intention
of realloc() would be clear to the OP once he/she read the appropriate
documentation about realloc(). Just looking at the sentence above, it
is not clear what the sentence means.

The information in your post is good, but presented a little strangely.
I think it is much better to just cut to the chase:

The the first argument to realloc must be a value returned from a
previous call to malloc(), calloc(), or realloc(), and must not yet
have been freed by a call to free() or realloc(). If the first
argument is NULL, it behaves like malloc(). realloc() may return NULL,
which means the reallocation did not succeed, but the contents of the
passed in pointer are unchanged.

But all the above should have been found in the OP's C language
reference or compiler documentation.

If I had more time when I initially responded to the poster, I would
have provided a wrapper function. I have the time now, so:

static void * shrink_x(void *p, size_t sz, size_t offset, size_t len);

/*
* Deallocate the specified range from an allocated object.
*
* p - the allocated object
* sz - size of the allocated object
* offset - the starting point of the deallocation
* len - the number of bytes to deallocate
*
* Returns 0 if the deallocation fails, otherwise returns a pointer
* to the shrunken object.
*/
void *
shrink(void *p, size_t sz, size_t offset, size_t len)
{
if (p == 0) {
return realloc(p, (offset+len > sz ? offset+len : sz));
}
if (offset+len > sz) {
return 0;
}
if (offset+len == sz) {
return realloc(p, offset);
}
return shrink_x(p, sz, offset, len);
}

static void *
shrink_x(void *p, size_t sz, size_t offset, size_t len)
{
char buf[len];
char *q = p;
void *t;
memcpy(buf, q+offset, len);
memmove(q+offset, q+offset+len, sz-offset-len);
t = realloc(p, sz-len);
if (t == 0) {
memmove(q+offset+len, q+offset, sz-offset-len);
memcpy(q+offset, buf, len);
}
return t;
}

I'll leave it as an exercise to the reader to find a way to do the
memory copies without using an extra array.

-- James
 
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