Link to home
Start Free TrialLog in
Avatar of Wendyu66
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.
Avatar of ozo
ozo
Flag of United States of America image

LENGTH_BYTE = 1 doesn't make sense in a typedef,
unless you meant to say
typedef enum length {
    LENGTH_BYTE = 1,
    LENGTH_HALF_WORD = 2,
    LENGTH_WORD = 4,
} Length_alignment;
Avatar of Wendyu66
Wendyu66

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.
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 )
For alignment you need the address of the data. Then its just a modulus operation.

int checkAlignment (int alignment, char *ptr)
{
    return ptr % alignment;
}

Open in new window

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
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial