好的,首先我是 C 新手,并且对指针(Java 程序员)非常陌生,所以请保持温和。对于这个类,我只允许使用 Syscalls,所以没有 stdio。
该函数接收一个 C 字符串作为 void* 的输入,另一个 void* 指向我需要将其添加到的内存区域。无论出于何种原因,它都没有正确地放入新位置。
*(char*)ptr = (char*)data;
ptr 是一个 void* 并且是我想要将字符串写入到的内存中的位置,而 data 是一个 void* 是我想要在那里写入的字符串。
如果我打印出这两个值,我会在打印 (char*) 数据时得到正确的字符串,但在打印 (char*)ptr 时我只会得到一个垃圾字符。如果我将上面的行更改为:
*(char*)ptr = *(char*)data;
然后只有字符串的第一个字符进入内存。
我究竟做错了什么?
谢谢。
完整来源:
#include <stdlib.h>
#include <unistd.h>
#define F_first 1 //First call to function, allocate amount of memory specified by data
#define F_last 2 //last call to function, free memory area
#define F_data_int 3 //void* arg points to integer data
#define F_data_char 4 //void* arg points to character string
#define F_data_float 5 //void* arg points to float data
#define F_print 6 //print the accumulated values in memory
#define Ptr_offset 32 //the amount of extra data I'm setting aside to store info about how much total data is allocated and how much of that area is being used
void* f( int code, void* mem, void* data )
{
size_t totalSize = 500;
size_t dataSize = 0;
size_t availableSize = 0;
size_t neededSize = 0;
void* ptr;
if( code == F_first )
{
if( mem != NULL )
{
exit(1);
}
if( data != NULL )
{
totalSize = (size_t)data + Ptr_offset;
mem = malloc( totalSize );
}else{
totalSize += Ptr_offset;
mem = malloc( totalSize );
}
dataSize = 0;
}
else if( code == F_last )
{
if( mem == NULL )
{
exit(1);
}
else
{
free( mem );
}
}
else if( code == F_data_int )
{
dataSize = *(int*)(mem + (Ptr_offset/2));
totalSize = *(int*)mem;
availableSize = totalSize - dataSize;
neededSize = sizeof(*(int*)data);
if( neededSize >= availableSize )
{
totalSize = neededSize * 2;
mem = realloc(mem, totalSize);
}
ptr = mem + Ptr_offset + dataSize;
*(int*)ptr = *(int*)data;
ptr = mem + Ptr_offset/2;
*(int*)ptr = dataSize+sizeof(*(int*)data);
}
else if( code == F_data_char )
{
dataSize = *(int*)(mem + (Ptr_offset/2));
totalSize = *(int*)mem;
availableSize = totalSize - dataSize;
neededSize = sizeof(*(char*)data);
if( neededSize >= availableSize )
{
totalSize = neededSize * 2;
mem = realloc(mem, totalSize);
}
ptr = mem + Ptr_offset + dataSize;
strcpy(ptr, data);
printf("\n chardata: %s \n charptr: %s \n", (char*)data, (char*)ptr);
ptr = mem + Ptr_offset/2;
*(int*)ptr = dataSize+sizeof(*(char*)data);
}else if( code == F_data_float )
{
dataSize = *(int*)(mem + (Ptr_offset/2));
totalSize = *(int*)mem;
availableSize = totalSize - dataSize;
neededSize = sizeof(*(float*)data);
if( neededSize >= availableSize )
{
totalSize = neededSize * 2;
mem = realloc(mem, totalSize);
}
ptr = mem + Ptr_offset + dataSize;
*(float*)ptr = *(float*)data;
ptr = mem + Ptr_offset/2;
*(int*)ptr = dataSize+sizeof(*(float*)data);
}else if( code == F_print )
{
ptr = mem + Ptr_offset/2;
write(1, ptr, *(int*)mem);
}
if(mem != NULL)
{
*(int*)mem = totalSize;
}
return mem;
}
int main( void )
{
int i_a;
float f_a;
void* m;
int* i;
float* fl;
fl = &f_a;
i = &i_a;
m = f( F_first, 0, (void*)300 );
m = f( F_data_char, m, (void*)"Systems programming class has " );
f_a = 69.7;
m = f( F_data_float, m, (void*)fl );
m = f( F_data_char, m, (void*)"registered " );
m = f( F_data_char, m, (void*)"students in a " );
m = f( F_data_char, m, (void*)"classroom of " );
i_a = 70;
m = f( F_data_int, m, (void*)i );
m = f( F_data_char, m, (void*)"\n" );
m = f( F_print, m, 0 );
m = f(F_last, m, 0);
return 0;
}