• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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.
1 Solution
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 )
satsumoSoftware DeveloperCommented:
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.
Murugesan NagarajanSubject-matter expert at C++ C delivery, implementation, and automation at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
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 alignment.one" 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 alignment.one
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."
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now