How to check data in memory

Hello experts,

Assume that I have

// 1, 2 and 4 are bytes
typedef length {
    LENGTH_BYTE = 1,
    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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LENGTH_BYTE = 1 doesn't make sense in a typedef,
unless you meant to say
typedef enum length {
    LENGTH_BYTE = 1,
    LENGTH_WORD = 4,
} Length_alignment;
Wendyu66Author Commented:
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_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 )
 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.
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
As per the question, I feel that you have taken the type of enum similar to struct or union.
It is not so.
It is allowing user defined DATA TYPE.
Since you have asked 1 MB, I feel you are using TurboC/C++
The size of that user defined type is equal to sizeof(int)
Length_alignment alignment;

Open in new window

Compiled_at                      sizeof(int)   sizeof(alignment)
Turbo C                              2        2
Cygwin 32 bit compilation               4      4
Linux 64 bit compilation                  4      4

Open in new window

In Linux platform you using gdb you can know the type of enum:

The following code begins with line number.
#include <stdio.h>
typedef enum length
        one = 1,
        two = 2,
        three = 3,
        four = 4,
        five = 5,
        six = 6,
        seven = 7,
        eight = 8,
        nine = 9,
        ten = 10
int check (Length_alignment alignment)
        if ( six == alignment)
                printf( "alignment value is six\n");
        printf( "sizeof(alignment) :%lu:\n", sizeof(alignment));
        return three;
Length_alignment alignment = six;
int main()
        check (alignment);
        return 0;

Open in new window

$ /usr/bin/gcc -g -DBIT_64 -Wall length.c
$ /usr/bin/file ./a.out | /bin/sed 's/,.*//'
./a.out: ELF 64-bit LSB executable
$ ./a.out
alignment value is six
sizeof(alignment) :4:

Open in new window

In the following "print" is invalid"
$ gdb ./a.out
(gdb) break main
(gdb) break main
Breakpoint 1 at 0x400512: file length.c, line 27.
(gdb) run
Starting program: /home/murugesan/cpp/a.out

Breakpoint 1, main () at length.c:27
27              check (alignment);
(gdb) print alignment
$1 = six
(gdb) ptype alignment
type = enum length {one = 1, two, three, four, five, six, seven, eight, nine, ten}
(gdb) print
Attempt to extract a component of a value that is not a structure.

Open in new window

Look at the last output obtained from gdb
"Attempt to extract a component of a value that is not a structure."

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.