free() problem

J

James Leddy

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

A. Sinan Unur

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
of your function.
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/reader.aspx?b=c/&h=stdlib.html#realloc

useful.

Sinan.
 
B

bd

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.
 
B

bd

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.
 
A

A. Sinan Unur

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top