5
/** @file alloc.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>


#define NEW_BLOCK_SIZE 1024 
#define ARRAY_SIZE 31


typedef struct _metadata_mem
{
size_t size;
void * addr ;
struct _metadata_mem * next_free ;
struct _metadata_mem * pre_free;
char * unit ; 


} metadata_mem;

#define SIZE_OF_MATEDATA sizeof(metadata_mem) 



metadata_mem * array_of_block[31] ; 

int i;
for(i=0; i<31; i++){
    array_of_block[i]=NULL; 
}


int index(size_t size){
int count=0;
while((int)size>=2){
    size/=2;
    count++;
}
return count ; 

}

我收到以下错误,它在 for 循环中开始:

gcc alloc.c -O3 -Wextra -Wall -Werror -Wno-unused-result -Wno-unused-parameter -o    alloc.so -shared -fPIC
alloc.c:30:2: error: expected identifier or ‘(’ before ‘for’
alloc.c:30:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘&lt;’ token
alloc.c:30:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
alloc.c:35:5: error: conflicting types for ‘index’
make: *** [alloc.so] Error 1

我不知道 for 循环有什么问题。好像没问题。我不应该在全局上下文中初始化 array_of_block 吗?

非常感谢。

4

2 回答 2

11

代码需要在函数内部。如果此代码是程序中唯一的代码,则该函数称为“main”。以其(几乎)最简单的形式:

int main() {
    ... your code
}
于 2013-09-18T00:48:44.063 回答
3

已经有一段时间了,但是那个“for”语句需要包装在一个函数中,不是吗?不要认为您可以在文件块级别执行程序语句。

于 2013-09-18T00:46:02.813 回答