some strange for me (addition)

P

profjwang

the problem is in some strange for me,
the program's goal will realize my own malloc and free function,
the project's h_file below:
#ifndef _MALLOCFREE_H_
#define _MALLOCFREE_H_

#define PAGE_SIZE 4096
#define BUCKET_SIZE (PAGE_SIZE*1)
#define BUCKET_MASK (BUCKET_SIZE-sizeof(bucket_t))

struct object_t{
object_t *next;
char data[0];
};

struct bucket_t{
bucket_t *next, *prev;
int object_size;
int object_num;
object_t *free_object;
int free_object_num;
object_t objects[0];

void *alloc();
void free(void *ptr);
};

struct pool_t{
bucket_t dummy;
int object_size;

bucket_t *new_bucket();
void free_bucket(bucket_t *b);
void *alloc();
void free(void *ptr);
};

#endif
 
J

Jack Klein

the problem is in some strange for me,

The problem is even stranger for us, because I read your entire post
and you didn't tell us what the problem is. All I see is some invalid
code. No description of compile or run time errors.
the program's goal will realize my own malloc and free function,
the project's h_file below:
#ifndef _MALLOCFREE_H_

You are not allowed to define identifiers beginning with an underscore
followed by an upper case letter, or beginning with two underscores.
Such symbols are reserved for the implementation in all contexts.
#define _MALLOCFREE_H_

#define PAGE_SIZE 4096
#define BUCKET_SIZE (PAGE_SIZE*1)
#define BUCKET_MASK (BUCKET_SIZE-sizeof(bucket_t))

struct object_t{
object_t *next;

The line above cannot be accepted by a C compiler. The identifier
"object_t" is not defined.
char data[0];

The line above cannot be accepted by a C compiler. The value inside [
and ] in the definition of an array, or an array member of a
structure, must be greater than 0.
};

struct bucket_t{
bucket_t *next, *prev;

Identifier "bucket_t" is undefined.
int object_size;
int object_num;
object_t *free_object;
int free_object_num;
object_t objects[0];

It is a constraint violation to have 0 or a negative number inside the
[ and ].
void *alloc();
void free(void *ptr);
};

struct pool_t{
bucket_t dummy;
int object_size;

bucket_t *new_bucket();
void free_bucket(bucket_t *b);
void *alloc();
void free(void *ptr);
};

#endif

If your problem is that this will not compile with a C compiler, then
the compiler is correct.

If you are using some language other than C, you are posting in the
wrong group, and you need to post in a group for the language you are
actually using. If you are using the language that I think you are,
the identifier "_MALLOCFREE_H_" and the arrays with 0 length are
invalid in that language as well.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
T

Thad Smith

the problem is in some strange for me,
the program's goal will realize my own malloc and free function,
the project's h_file below:
#ifndef _MALLOCFREE_H_
#define _MALLOCFREE_H_

#define PAGE_SIZE 4096
#define BUCKET_SIZE (PAGE_SIZE*1)
#define BUCKET_MASK (BUCKET_SIZE-sizeof(bucket_t))

struct object_t{
object_t *next;
char data[0];
};

struct bucket_t{
bucket_t *next, *prev;
int object_size;
int object_num;
object_t *free_object;
int free_object_num;
object_t objects[0];

void *alloc();
void free(void *ptr);
};

struct pool_t{
bucket_t dummy;
int object_size;

bucket_t *new_bucket();
void free_bucket(bucket_t *b);
void *alloc();
void free(void *ptr);
};

#endif

I assume this is C++. Ask in comp.lang.c++.
 
K

Keith Thompson

the problem is in some strange for me,
the program's goal will realize my own malloc and free function,
the project's h_file below:
#ifndef _MALLOCFREE_H_
#define _MALLOCFREE_H_

#define PAGE_SIZE 4096
#define BUCKET_SIZE (PAGE_SIZE*1)
#define BUCKET_MASK (BUCKET_SIZE-sizeof(bucket_t))

struct object_t{
object_t *next;
char data[0];
};

struct bucket_t{
bucket_t *next, *prev;
int object_size;
int object_num;
object_t *free_object;
int free_object_num;
object_t objects[0];

void *alloc();
void free(void *ptr);
};

struct pool_t{
bucket_t dummy;
int object_size;

bucket_t *new_bucket();
void free_bucket(bucket_t *b);
void *alloc();
void free(void *ptr);
};

#endif

You've started a new thread, unconnected to what was being discussed
before. (I *think* you were discussing something similar earlier, but
I'm not going to go back and check.)

Looking at the code you just posted, you attempt to declare arrays of
length 0. This is illegal. You also appear to be writing C++, not C.

What was your question?
 
M

Mark McIntyre

the problem is in some strange for me,

You need to describe the problem.
struct object_t{
object_t *next;
char data[0];

This is not allowed in C. The size of an array must be at east 1.
Note that some implementations allow zero-size arrays as an extension but
its nonportable and not standard.
struct bucket_t{
bucket_t *next, *prev;
int object_size;
int object_num;
object_t *free_object;

No type "object_t" has been defined.
Have you missed some code out?
Note that C is not C++, and a struct definition does not introduce a type
alias.
void *alloc();
void free(void *ptr);

Better not to declare your own prototypes for standard functions - use
the appropriate header.
bucket_t *new_bucket();

Function pointer inside a struct. You're thinking of C++
void free(void *ptr);

Definitely not C....
 
D

David Thompson

Function pointer inside a struct. You're thinking of C++
s/pointer //
If it was a pointer it would be valid C.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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

Similar Threads

some strange for me 6
Linux: using "clone3" and "waitid" 0
could you help me about this problem? 21
Queue in C 0
A doubly linked-list in C 216
Help me with callback funcion. 2
zero up memory 44
Remving an element from Queue 37

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top