void* to char*

J

James Purser

I have a piece of c code I am trying to include in a c++/wxWidgets
project.

len = strlen(sha1_dir);
path = malloc(len + 40);
memcpy(path, sha1_dir, len);

When I try to compile the above using g++ I get "error: invalid conversion
from void* to char*"

I have tried casting the malloc as a char * but that results in a seg
fault at the memcpy line.

Any ideas would be much appreciated.

James Purser
 
A

Axter

James said:
I have a piece of c code I am trying to include in a c++/wxWidgets
project.

len = strlen(sha1_dir);
path = malloc(len + 40);
memcpy(path, sha1_dir, len);

When I try to compile the above using g++ I get "error: invalid conversion
from void* to char*"

I have tried casting the malloc as a char * but that results in a seg
fault at the memcpy line.

Any ideas would be much appreciated.

James Purser
Please post declaration for path, and code you use with the cast.

FYI: This code doesn't need cast in C, but would need it in C++

If you're putting this in a C++ file, I recommend you use new instead
of malloc.
 
R

Rolf Magnus

James said:
I have a piece of c code I am trying to include in a c++/wxWidgets
project.

len = strlen(sha1_dir);
path = malloc(len + 40);
memcpy(path, sha1_dir, len);

When I try to compile the above using g++ I get "error: invalid conversion
from void* to char*"

In C++, it's better to use new/delete instead of malloc/free.
I have tried casting the malloc as a char * but that results in a seg
fault at the memcpy line.

I can't see anything wrong with it. memcpy shouldn't crash AFAICS - unless
malloc failed to allocate the memory or sha1_dir doesn't point to the start
of a valid C style string.
You didn't copy the terminating '\0' character, so if you don't add anything
else and don't correctly terminate the string, the next function that
expects a C style string will probably crash.
 
J

James Purser

Please post declaration for path, and code you use with the cast.

FYI: This code doesn't need cast in C, but would need it in C++

If you're putting this in a C++ file, I recommend you use new instead
of malloc.
Full function declaration:

int init_db(char * dpath)
{
safe_create_dir(dpath);
char *sha1_dir, *path;
int len, i, err;
sha1_dir = getenv(DB_ENVIRONMENT);
if (!sha1_dir) {
sha1_dir = DEFAULT_DB_ENVIRONMENT;
err = 1;
}
len = strlen(sha1_dir);
path = malloc(len + 40);
memcpy(path, sha1_dir, len);

safe_create_dir(sha1_dir);
for (i = 0; i < 256; i++) {
sprintf(path+len, "/%02x", i);
safe_create_dir(path);
}
return err;
}
 
R

Rolf Magnus

James said:
Full function declaration:

int init_db(char * dpath)

Why not const char*?
{
safe_create_dir(dpath);
char *sha1_dir, *path;
int len, i, err;
sha1_dir = getenv(DB_ENVIRONMENT);
if (!sha1_dir) {
sha1_dir = DEFAULT_DB_ENVIRONMENT;
err = 1;

This is the only place where you write something to err. So if the getenv()
call succeeds, your function returns the value of an uninitialized
variable.
}
len = strlen(sha1_dir);
path = malloc(len + 40);

That would be ok in C, but in C++, you need to cast the value returned by
malloc, like:

path = static_cast<char*>(malloc(len + 40));

Or using new:

path = new char*[len + 40];
memcpy(path, sha1_dir, len);

safe_create_dir(sha1_dir);
for (i = 0; i < 256; i++) {
sprintf(path+len, "/%02x", i);
safe_create_dir(path);
}
return err;

You're having a memory leak here. The memory that 'path' points to is lost,
since the only pointer to it goes out of scope here, and you didn't free
the memory.

This all looks pretty much like C. Why are you running this through a C++
compiler?
 
O

Owen Jacobson

James said:
int init_db(char * dpath)
{
safe_create_dir(dpath);
char *sha1_dir, *path;
....

That would be ok in C, but in C++, you need to cast the value returned by
malloc, like:

path = static_cast<char*>(malloc(len + 40));

Or using new:

path = new char*[len + 40];

path = new char[len + 40]; // long day, rolf?
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top