我写了一个自定义内存分配器.它有2个限制,我想删除所以它的工作方式类似malloc/free.
1.)mem_free调用需要为其输入参数强制转换为unsigned char*.我希望它采用任何类型的指针.如何才能做到这一点?
2.)我写的内存分配器在缓冲区的前面分配了一块内存,并且还写入了它的大小.free函数删除缓冲区中最后一块已分配的内存.所以malloc/free调用的顺序很重要或者它不起作用.如何删除此限制?
希望能够做到这一点:
char* ptr1 = mem_alloc(10);
char* ptr2 = mem_alloc(4);
mem_free(ptr1);
mem_free(ptr2);
Run Code Online (Sandbox Code Playgroud)
现在必须这样做:
char* ptr1 = mem_alloc(10);
char* ptr2 = mem_alloc(4);
mem_free(ptr2);
mem_free(ptr1);
Run Code Online (Sandbox Code Playgroud)
码:
unsigned char* mem_alloc(unsigned int size) {
unsigned int s;
if( (size + MEM_HEADER_SIZE) > (MEM_MAX_SIZE - mem_current_size_bytes) ) {
return NULL;
}
if(is_big_endian() == 0) {
s = (mem_buff[3] << 24) + (mem_buff[2] << 16) + (mem_buff[1] << 8) + mem_buff[0];
} else {
s = (mem_buff[0] << 24) + (mem_buff[1] << 16) …Run Code Online (Sandbox Code Playgroud) 我没想到能够在结构的内存块中增加指向结构的指针.但似乎有效.有什么情况不起作用吗?如果我创建一个结构的"列表",那么我应该总是能够增加指向它们的指针,C将计算出要移动多少字节?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct User {
int id;
char name[32];
float net_worth;
};
typedef struct User User;
int main(int argc, char** argv) {
User* u1 = (User*)malloc(sizeof(User));
u1->id = 1;
strcpy(u1->name, "Mike");
u1->net_worth = 43.45;
User* u2 = (User*)malloc(sizeof(User));
u2->id = 2;
strcpy(u2->name, "Pablo");
u2->net_worth = -2.00;
User* u3 = (User*)malloc(sizeof(User));
u3->id = 3;
strcpy(u3->name, "Frederick");
u3->net_worth = 7329213.45;
User** users = (User**)malloc(sizeof(User)*10);
*users = u1;
printf("%s\n", ((User*)(*users))->name);
*users++;
*users = u2;
printf("%s\n", ((User*)(*users))->name);
*users++;
*users …Run Code Online (Sandbox Code Playgroud) 我想映射结构成员,这样我就可以消除循环中的分支。在 C 中实现它的最佳方法或约定是什么?我想它可能是一个二维数组......然后我可以将整数映射到字符键?
char chunk[32];
int n;
int i;
char *ptr = config;
while (*ptr != '\0') {
int items_read = sscanf(ptr, "%31[^;]%n", chunk, &n);
if(chunk[0] == 'S' && chunk[1] == 'P') {
for(i=0;i<GLOBAL_MEAS_CUTOFF; i++) {
theMeas[i].signal_path = atoi(&chunk[2]);
}
}
if(chunk[0] == 'T' && chunk[1] == 'L') {
for(i=0;i<GLOBAL_MEAS_CUTOFF; i++) {
theMeas[i].trace_length = atoi(&chunk[2]);
}
}
if(chunk[0] == 'S' && chunk[1] == 'R') {
for(i=0;i<GLOBAL_MEAS_CUTOFF; i++) {
theMeas[i].sample_rate = atoi(&chunk[2]);
}
}
chunk[0]='\0';
if (items_read == 1)
ptr += …Run Code Online (Sandbox Code Playgroud) 如果我定义一个重用主线程也使用的另一个函数的线程函数....是否可能存在竞争条件?是否在线程之间共享同一函数中的局部变量?在这种情况下,函数do_work在thread_one线程和主线程中都使用.函数do_work中的局部变量x是否可以被两个线程修改,因此会产生意外结果?
void *thread_one() {
int x = 0;
int result;
while(1) {
for(x=0; x<10; x++) {
result = do_work(x);
}
printf("THREAD: result: %i\n", result);
}
}
int do_work(int x) {
x = x + 5;
return x;
}
int main(int argc, char**argv) {
pthread_t the_thread;
if( (rc1 = pthread_create( &the_thread, NULL, thread_one, NULL)) ) {
printf("failed to create thread %i\n", rc1);
exit(1);
}
int i = 0;
int result = 0;
while(1) {
for(i=0; i<12; i+=2) {
result = do_work(i); …Run Code Online (Sandbox Code Playgroud) 我找到了一个解决方案,当你按住一个键时,可以移动精灵.问题在于它强制编写丑陋的重复代码.我找到的当前解决方案是:
for event in pygame.event.get():
if event.type == KEYDOWN:
keystate = pygame.key.get_pressed()
while keystate[K_RIGHT]:
screen.fill((255,255,255))
pygame.event.get()
for sprite in sprites:
rimage = sprite[1].getimage()
if sprite[2] is None:
x+=3
sprite[1].update(time)
screen.blit(rimage, (x,y))
if sprite[1].isfinished() == True:
sprite[1].reset()
last_dir = "right"
if x >= screen_width - rimage.get_width():
x = screen_width - rimage.get_width()
#update player sprite movement
#update player sprite animation
#update rest of game map
keystate = pygame.key.get_pressed()
time = pygame.time.get_ticks()
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
问题是while keystate块.必须针对每个方向重复,并且需要在每个块中更新游戏世界.这是需要复制相同代码的五个地方....如果没有按下一个键,则加上4个方向.我可以将它包装在一个函数中,但我想知道是否有更好的方法来处理按下pygame中的按钮.
I am trying to move the float array ptr 256 "units" from the start so (256 * 4 bytes) for floats. I am receiving a compile time error.
long new_capture_length = 4096;
long step_size = 256;
float data[new_capture_length];
data+=step_size;
Run Code Online (Sandbox Code Playgroud)
error: invalid operands to binary + (have ‘float[(long unsigned int)(new_capture_length)]’ and ‘float *’)
How can I achieve this?
我期望下面的数组初始化的大小为32. 1字节字符,列表中的每个项目为2个字节,16个项目.... = 32.但是它是128个字节.为什么?
char* cmds[] = {"AQ", "BD", "LS", "TW", "AS", "CP", "TR", "CO", "BF", "MS", "SR", "TL", "WT", "PM", "TE", "TC"};
printf("%li\n", sizeof(cmds));
//result is 128
//size of list is 16
//8 bytes per item in the list
//why?
Run Code Online (Sandbox Code Playgroud) 所述sizeof(test)返回24.是3点数组char*的指针,它们分别我的机器上的8个字节.我的问题是C是否也为角色正确分配了空间?我想这样做是因为这有效,但我想确保我正确地做到了.
char* test[] = {"QW", "BT", "GH"};
int size = sizeof(test) / sizeof(char*);
Run Code Online (Sandbox Code Playgroud) 我有两个程序通过同一台计算机上的套接字进行通信.目前使用TCP/IP传输160万字节大约需要7秒.我需要快速做到.
如果我使用原始套接字而忽略TCP/IP标头,那么这应该提高速度吗?我还能做些什么来提高速度吗?SOCKET_RAW选项是直接复制还是其他任何操作?