Velocity Reviews > free() problem

free() problem

James Leddy
Guest
Posts: n/a

 06-26-2003
Hello,

I'd like to take a string passed as an argument and realloc() the string to
have more spaces. Then I thought that I would probably have to free() the
string. But now I am confused

If I free() the string in the called function what happens to the data that
that was passed by the calling function.

Ok, so what if I free()d it by the calling function. But then again I
didn't pass the string as a double pointer, so I don't know really how that
would work. And what if I passed a string literal in, how would the
calling function be responsible for that?

Here is the specific instance. As many of you may know, blowfish algorythim
bepends on a key, which I have chosen to represent as a string. I'm
supposed to wrap the string if I run out of chars, but since I need it in 4
char blocks, I decided to add three chars to the end

int InitializeBlowfish(char *key, size_t len)
{...

key = realloc(key, len + 4); //give 3 more spaces,
memcpy(key + len, key, sizeof(char) * 3);
....}

How do I go about free()ing the memory I realloc()ed?

A. Sinan Unur
Guest
Posts: n/a

 06-26-2003
James Leddy <(E-Mail Removed)> wrote in
news:LIGKa.1870\$(E-Mail Removed) :

> Hello,
>
> I'd like to take a string passed as an argument and realloc() the
> string to have more spaces. Then I thought that I would probably have
> to free() the string. But now I am confused
>
> If I free() the string in the called function what happens to the data
> that that was passed by the calling function.

If you want to preserve the contents of the memory pointed to by the
pointer passed to your function, you can allocate the needed space, copy
the contents of the original memory to that space, and free it at the end

> int InitializeBlowfish(char *key, size_t len)
> {...
>
> key = realloc(key, len + 4); //give 3 more spaces,
> memcpy(key + len, key, sizeof(char) * 3);
> ...}
>
> How do I go about free()ing the memory I realloc()ed?

Are you trying to get free the extra 3 characters worth of space you
allocated? Just to a realloc with the original size.

Also, your use of realloc above is buggy. You do not check if realloc
succeeded, and you do not preserve the value of key to deal with the case
realloc fails. Finally, sizeof(char) is 1 by definition, no need to
clutter code.

You might find:

http://www.dinkumware.com/manuals/re...b.html#realloc

useful.

Sinan.
--
A. Sinan Unur
http://www.velocityreviews.com/forums/(E-Mail Removed)
Spam bait: (E-Mail Removed)

bd
Guest
Posts: n/a

 06-26-2003
On Thu, 26 Jun 2003 18:12:59 +0000, James Leddy wrote:

> Hello,
>
> I'd like to take a string passed as an argument and realloc() the string to
> have more spaces. Then I thought that I would probably have to free() the
> string. But now I am confused

If you want to resize it, there's no need to free - realloc takes care of
everything except updating all pointers to it.

> If I free() the string in the called function what happens to the data that
> that was passed by the calling function.

It's gone, then.

> Ok, so what if I free()d it by the calling function. But then again I
> didn't pass the string as a double pointer, so I don't know really how that
> would work. And what if I passed a string literal in, how would the
> calling function be responsible for that?

You must make provisions to pass back the updated pointers if you realloc
in a called function.

> Here is the specific instance. As many of you may know, blowfish algorythim
> bepends on a key, which I have chosen to represent as a string. I'm
> supposed to wrap the string if I run out of chars, but since I need it in 4
> char blocks, I decided to add three chars to the end
>
> int InitializeBlowfish(char *key, size_t len)
> {...
>
> key = realloc(key, len + 4); //give 3 more spaces,
> memcpy(key + len, key, sizeof(char) * 3);
> ...}
>
> How do I go about free()ing the memory I realloc()ed?

You don't - by doing that you invalidated the pointer passed in. Try:
int InitializeBlowfish(const char *key, size_t len){
char *newkey;
size_t newlen = len;
if(newlen % 4)
newlen += 4 - (newlen % 4);
newkey = malloc(newlen);
memcpy(newkey, key, len);
memcpy(newkey + len, key, newlen - len);
/* ... */
free(newkey);
}

Also, sizeof char is 1 by definition.

--
Freenet distribution not available
Life is wasted on the living.
-- The Restaurant at the Edge of the Universe.

bd
Guest
Posts: n/a

 06-26-2003
On Thu, 26 Jun 2003 18:29:34 +0000, A. Sinan Unur wrote:

> James Leddy <(E-Mail Removed)> wrote in
> news:LIGKa.1870\$(E-Mail Removed) :

[snip]
>> int InitializeBlowfish(char *key, size_t len)
>> {...
>>
>> key = realloc(key, len + 4); //give 3 more spaces,
>> memcpy(key + len, key, sizeof(char) * 3);
>> ...}
>>
>> How do I go about free()ing the memory I realloc()ed?

>
> Are you trying to get free the extra 3 characters worth of space you
> allocated? Just to a realloc with the original size.

But the old pointer may be invalid now. realloc can move the data to a new
location if necessary.
--
Freenet distribution not available
A real diplomat is one who can cut his neighbor's throat without having
his neighbour notice it.
-- Trygve Lie

A. Sinan Unur
Guest
Posts: n/a

 06-26-2003
"bd" <(E-Mail Removed)-ip.org> wrote in
news(E-Mail Removed)-ip.org:

> On Thu, 26 Jun 2003 18:29:34 +0000, A. Sinan Unur wrote:
>
>> James Leddy <(E-Mail Removed)> wrote in
>> news:LIGKa.1870\$(E-Mail Removed) :

> [snip]
>>> int InitializeBlowfish(char *key, size_t len)
>>> {...
>>>
>>> key = realloc(key, len + 4); //give 3 more spaces,
>>> memcpy(key + len, key, sizeof(char) * 3);
>>> ...}
>>>
>>> How do I go about free()ing the memory I realloc()ed?

>>
>> Are you trying to get free the extra 3 characters worth of space you
>> allocated? Just to a realloc with the original size.

>
> But the old pointer may be invalid now. realloc can move the data to a
> new location if necessary.

You are right, of course. I should have pointed that out as well as
commenting on the OP's usage of realloc.

Sinan.

--
A. Sinan Unur
(E-Mail Removed)
Spam bait: (E-Mail Removed)