Wendyu66
asked on
How to check data in memory
Hello experts,
Assume that I have
// 1, 2 and 4 are bytes
typedef length {
LENGTH_BYTE = 1,
LENGTH_HALF_WORD = 2,
LENGTH_WORD = 4,
} Length_alignment;
and a function
int check (Length_alignment alignment) {
}
My question is - in above check(...) function, how to check if data is in alignment with the data lengths in bytes in typedef? Assume the memory space is 1024 x 1024 bytes (1M bytes).
Thanks so much.
Assume that I have
// 1, 2 and 4 are bytes
typedef length {
LENGTH_BYTE = 1,
LENGTH_HALF_WORD = 2,
LENGTH_WORD = 4,
} Length_alignment;
and a function
int check (Length_alignment alignment) {
}
My question is - in above check(...) function, how to check if data is in alignment with the data lengths in bytes in typedef? Assume the memory space is 1024 x 1024 bytes (1M bytes).
Thanks so much.
ASKER
Thanks ozo for your reply! You are right. Sorry I missed "enum" when I sent my question. Let me simplify the enum in following way.
typedef enum {
LENGTH_BYTE = 1,
LENGTH_HALF_WORD = 2,
LENGTH_WORD = 4,
} Length_alignment;
btw, do you have any ideas about my question? It's quite ok to use MIPS (assembly language).
thanks so much.
typedef enum {
LENGTH_BYTE = 1,
LENGTH_HALF_WORD = 2,
LENGTH_WORD = 4,
} Length_alignment;
btw, do you have any ideas about my question? It's quite ok to use MIPS (assembly language).
thanks so much.
I'm still not sure what you are asking.
There's a sizeof operator, which can be invoked by
sizeof( Length_alignment )
or
sizeof( alignment )
There's a sizeof operator, which can be invoked by
sizeof( Length_alignment )
or
sizeof( alignment )
For alignment you need the address of the data. Then its just a modulus operation.
int checkAlignment (int alignment, char *ptr)
{
return ptr % alignment;
}
The alignment can be any number. There really isn't much need for a function to do this because its just a wrapper for the '%' operator.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
unless you meant to say
typedef enum length {
LENGTH_BYTE = 1,
LENGTH_HALF_WORD = 2,
LENGTH_WORD = 4,
} Length_alignment;