求助C++下面这几句malloc到底应该怎么写才能通过VC 6的编译?

「已注销」 2011-08-21 11:42:13
9 #define xmalloc malloc //这句的应该是说xmalloc和malloc是一样的吧
44 ret = xmalloc(sizeof(struct path));
117 file = xmalloc(sizeof(struct file));
154 handle = xmalloc(sizeof(struct dir));
169 handle = xmalloc(sizeof(struct dir));
186 entry = xmalloc(sizeof(struct direntry));

出错代码如下
--------------------Configuration: fs - Win32 Debug--------------------
Compiling...
filesystem.cpp
c:\复件 fs_dumper\filesystem.cpp(44) : error C2440: '=' : cannot convert from 'void *' to 'struct path *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
c:\复件 fs_dumper\filesystem.cpp(117) : error C2440: '=' : cannot convert from 'void *' to 'struct file *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
c:\复件 fs_dumper\filesystem.cpp(134) : warning C4018: '>' : signed/unsigned mismatch
c:\复件 fs_dumper\filesystem.cpp(154) : error C2440: '=' : cannot convert from 'void *' to 'struct dir *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
c:\复件 fs_dumper\filesystem.cpp(169) : error C2440: '=' : cannot convert from 'void *' to 'struct dir *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
c:\复件 fs_dumper\filesystem.cpp(186) : error C2440: '=' : cannot convert from 'void *' to 'struct direntry *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
执行 cl.exe 时出错.

fs.exe - 1 error(s), 0 warning(s)
源文件在这下载http://www.mediafire.com/?h1nhf53b165te6o
其中的filesystem.cpp在下面


#include <stdio.h>
#include <cstdlib>
#include <string.h>
#include <winsock2.h>
#include "nand.h"
#include "filesystem.h"

#define xmalloc malloc

#define MIN(a,b) ((a)>(b)?(b):(a))

static FILE * rom_fh;
static unsigned long start_blk;

unsigned long find_fs(FILE * fh)
{
struct nand_block blk = {0,};
unsigned long blkno = 0;
while(1) {
if (!read_nand(fh, blkno, &blk)) {
blkno = 0;
break;
}
if (memcmp(blk.data + 496, LABEL, 16) == 0) {
blkno += 2;
break;
}
++blkno;
}
return blkno;
}

struct path * parse_path(const char *path)
{
char *end;
int len;
struct path *ret;
char *p;
char *p1;
int slash_count = 0;
if (path == NULL)
return NULL;
ret = malloc(sizeof(struct path));
p = strdup(path);
p1 = p;
memset(ret, 0, sizeof(struct path));
if (p[0] != '/')
return NULL;
while(p1[0] != '\0') {
if(p1 != p) {
while(p1[-1] == '/' && p1[0] == '/') {
char *t = p1;
while(t[0] != '\0') {
t++;
t[-1] = t[0];
}
}
}
if (p1[0] == '/')
++slash_count;
p1++;
}
if(slash_count > 2) {
free(p);
free(ret);
return NULL;
}
len = strlen(p);
end = strrchr(p, '/');
if (len == 1) {
strcpy(ret->directory, "/");
} else if (slash_count == 1) {
memcpy(ret->directory, p + 1, 15);
} else {
len = end - (p + 1);
memcpy(ret->directory, p + 1, (len > 15 ? 15 : len));
len = strlen(end);
memcpy(ret->filename, end + 1, (len > 15 ? 15 : len));
}
free(p);
return ret;
}

int init_fs(const char * image)
{
rom_fh = fopen(image, "r");
if (rom_fh == NULL) {
return 0;
}
start_blk = find_fs(rom_fh);
if (start_blk == 0) {
fclose(rom_fh);
rom_fh = NULL;
return 0;
}
return 1;
}

struct file* fs_open(const char *filename)
{
struct dir *d;
struct direntry *entry;
struct file *file = NULL;
struct path *path = parse_path(filename);
char dir[17] = {'/'};
if (path && strcmp(path->filename, "") != 0) {
strncpy(dir + 1, path->directory, 16);
d = fs_opendir(dir);
while((entry = fs_readdir(d)) != NULL) {
if (strcmp(entry->filename, path->filename) == 0)
break;
free(entry);
}
free(d);
if (entry != NULL) {
file = malloc(sizeof(struct file));
file->fp = 0;
file->blkno = entry->location - LOCOFF;
file->size = entry->size;
free(entry);
}
}
free(path);
return file;
}

unsigned long fs_read(struct file* fp, char *buffer, int length)
{
struct nand_block blk;
unsigned long block;
unsigned long offset;
unsigned long read = 0;
unsigned long left = MIN(length, fp->size - fp->fp);
while (left > 0) {
block = fp->fp / 512;
offset = fp->fp % 512;
read_nand(rom_fh, start_blk + fp->blkno + block, &blk);
memcpy(buffer + read, blk.data + offset, MIN(512 - offset, left));
read += MIN(512 - offset, left);
fp->fp += MIN(512 - offset, left);
left -= MIN(512 - offset, left);
}
return read;
}

struct dir* fs_opendir(const char* dir)
{
struct path *path = parse_path(dir);
struct dir *handle, *subdir;
struct direntry *entry;
if (path && strcmp(path->filename, "") == 0) {
if (strcmp(path->directory, "/") == 0) {
handle = malloc(sizeof(struct dir));
handle->cidx = 0;
handle->dir_id = -1;
return handle;
} else {
subdir = fs_opendir("/");
if (subdir == NULL)
return NULL;
while((entry = fs_readdir(subdir)) != NULL) {
if (strcmp(entry->filename, path->directory) == 0)
break;
free(entry);
}
free(subdir);
if (entry != NULL) {
handle = malloc(sizeof(struct dir));
handle->cidx = entry->location;
handle->dir_id = entry->dir_id;
free(entry);
return handle;
}
}
}
free(path);
return NULL;
}

struct direntry * fs_readdir(struct dir *directory)
{
struct nand_block blk;
struct direntry *entry;
unsigned long block, offset;
entry = malloc(sizeof(struct direntry));
block = (directory->cidx * 0x20) / 512;
offset = (directory->cidx * 0x20) % 512;
read_nand(rom_fh, start_blk + block, &blk);
memcpy(entry, blk.data + offset, 32);
entry->dir_id = ntohs(entry->dir_id);
entry->dir_ptr = ntohs(entry->dir_ptr);
entry->location = ntohl(entry->location);
entry->size = ntohl(entry->size);
if (directory->dir_id == -1) {
if (entry->dir_id == 0xffff) {
free(entry);
entry = NULL;
}
} else {
if (entry->dir_ptr != directory->dir_id) {
free(entry);
entry = NULL;
}
}
if (entry != NULL)
directory->cidx++;
return entry;
}

void deinit_fs()
{
fclose(rom_fh);
}

...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
super_admi 2011-08-22
  • 打赏
  • 举报
回复
强制转换即可,不要换什么.c,.cpp。

比如char * the_string = NULL;
the_string = (char*)malloc(sizeof(char)*string_len);
xin_wu_hen 2011-08-22
  • 打赏
  • 举报
回复
在xmalloc之前,进行强制类型转换(为=号之前的变量类型)。一般来说,我们可以将任何类型的指针赋给void*,但是相反过程必须使用强制类型转换才可以。这是c++的一个标准。
「已注销」 2011-08-21
  • 打赏
  • 举报
回复
--------------------Configuration: fs - Win32 Debug--------------------
Compiling...
filesystem.c
c:\复件 fs_dumper\filesystem.c(134) : warning C4018: '>' : signed/unsigned mismatch
Linking...
fs_dumper.obj : error LNK2001: unresolved external symbol "unsigned long __cdecl fs_read(struct file *,char *,int)" (?fs_read@@YAKPAUfile@@PADH@Z)
fs_dumper.obj : error LNK2001: unresolved external symbol _g_free
fs_dumper.obj : error LNK2001: unresolved external symbol "struct file * __cdecl fs_open(char const *)" (?fs_open@@YAPAUfile@@PBD@Z)
fs_dumper.obj : error LNK2001: unresolved external symbol _g_build_path
fs_dumper.obj : error LNK2001: unresolved external symbol "struct direntry * __cdecl fs_readdir(struct dir *)" (?fs_readdir@@YAPAUdirentry@@PAUdir@@@Z)
fs_dumper.obj : error LNK2001: unresolved external symbol "struct dir * __cdecl fs_opendir(char const *)" (?fs_opendir@@YAPAUdir@@PBD@Z)
fs_dumper.obj : error LNK2001: unresolved external symbol "void __cdecl deinit_fs(void)" (?deinit_fs@@YAXXZ)
fs_dumper.obj : error LNK2001: unresolved external symbol "struct path * __cdecl parse_path(char const *)" (?parse_path@@YAPAUpath@@PBD@Z)
fs_dumper.obj : error LNK2001: unresolved external symbol "int __cdecl init_fs(char const *)" (?init_fs@@YAHPBD@Z)
fs_dumper.obj : error LNK2001: unresolved external symbol _g_print
fs_dumper.obj : error LNK2001: unresolved external symbol _g_option_context_parse
fs_dumper.obj : error LNK2001: unresolved external symbol _g_option_context_add_main_entries
fs_dumper.obj : error LNK2001: unresolved external symbol _g_option_context_new
filesystem.obj : error LNK2001: unresolved external symbol _read_nand
filesystem.obj : error LNK2001: unresolved external symbol __imp__ntohl@4
filesystem.obj : error LNK2001: unresolved external symbol __imp__ntohs@4
Debug/fs.exe : fatal error LNK1120: 16 unresolved externals
执行 link.exe 时出错.

fs.exe - 1 error(s), 0 warning(s)
jackyjkchen 2011-08-21
  • 打赏
  • 举报
回复
把扩展名从cpp改成c

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧