M
Mark Hobley
I have two structures:
struct buffertablestru {
struct bufstru *addr;
int numbufs;
};
struct bufstru {
char *addr;
int sz;
int ptr;
int tail;
};
The variable buffertable is of type buffertablestru and holds the address of a
set of buffer tables of type bufstru allocated in dynamic memory:
/* allocate the buffertable */
buffertable.addr = malloc(BUFTABINIT * sizeof(struct bufstru));
buffertable.numbufs = BUFTABINIT
(In this case BUFTABINIT is the number of tables to be allocated).
I have a function that I want to use to wipe an entry from the dyamic table
on request:
int wipebuffer(int bufnum) {
struct bufstru *bufstruptr = buffertable.addr +
(bufnum * sizeof(struct bufstru)); <---
bufstruptr -> addr = NULL; |
bufstruptr -> sz = 0; |
bufstruptr -> ptr = 0; |
bufstruptr -> tail = 0; |
} |
|
My question is, should I be calculating an offset here, or does pointer
arithmetic apply, requiring me to only provide the buffer number as below?
struct bufstru *bufstruptr = buffertable.addr + bufnum;
I have the start of the buffer tables at buffertable.addr and a buffer number.
I guess because I have a physical address, I am adding the offset, not the
buffer number. Does that sound right?
Now, suppose that i did this instead:
struct bufstru *bufstruptr = buffertable.addr;
I now want to set *bufstruptr to the address of the third buffer within the
dynamic table:
bufnum = 2; /* I want the third entry in the dynamic table */
bufstruptr += bufnum;
Am I right in thinking that pointer arithmetic applies here, so I don't need
to calculate the offset, because bufstruptr will always go up and down by the
size of bufstru?
Mark.
struct buffertablestru {
struct bufstru *addr;
int numbufs;
};
struct bufstru {
char *addr;
int sz;
int ptr;
int tail;
};
The variable buffertable is of type buffertablestru and holds the address of a
set of buffer tables of type bufstru allocated in dynamic memory:
/* allocate the buffertable */
buffertable.addr = malloc(BUFTABINIT * sizeof(struct bufstru));
buffertable.numbufs = BUFTABINIT
(In this case BUFTABINIT is the number of tables to be allocated).
I have a function that I want to use to wipe an entry from the dyamic table
on request:
int wipebuffer(int bufnum) {
struct bufstru *bufstruptr = buffertable.addr +
(bufnum * sizeof(struct bufstru)); <---
bufstruptr -> addr = NULL; |
bufstruptr -> sz = 0; |
bufstruptr -> ptr = 0; |
bufstruptr -> tail = 0; |
} |
|
My question is, should I be calculating an offset here, or does pointer
arithmetic apply, requiring me to only provide the buffer number as below?
struct bufstru *bufstruptr = buffertable.addr + bufnum;
I have the start of the buffer tables at buffertable.addr and a buffer number.
I guess because I have a physical address, I am adding the offset, not the
buffer number. Does that sound right?
Now, suppose that i did this instead:
struct bufstru *bufstruptr = buffertable.addr;
I now want to set *bufstruptr to the address of the third buffer within the
dynamic table:
bufnum = 2; /* I want the third entry in the dynamic table */
bufstruptr += bufnum;
Am I right in thinking that pointer arithmetic applies here, so I don't need
to calculate the offset, because bufstruptr will always go up and down by the
size of bufstru?
Mark.