473,394 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Comparing memory with a constant

Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.

Thanks!

Nov 22 '06 #1
25 5638
galapogos wrote:
How do I do that. Obviously I can't use memcmp()
That's obvious but I think not for the reason you are thinking of.
Consider byte-order and alignment for instance.

Depending on how your long number was generated, you might not be able
to do what you're suggesting at all, with any portability.

You are making assumptions about the bitwise order and alignment of an
array, and also about the bitwise representation of a long value. Both
of these fall under the category of "you might get lucky."

Nov 22 '06 #2
galapogos wrote:
Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.
Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?

--
Ian Collins.
Nov 22 '06 #3

Ian Collins wrote:
galapogos wrote:
Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.
Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?

--
Ian Collins.
That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?

Nov 22 '06 #4
galapogos wrote:
Ian Collins wrote:
>>galapogos wrote:
>>>Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.

Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?
That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?
How is the constant represented?

--
Ian Collins.
Nov 22 '06 #5

Ian Collins wrote:
galapogos wrote:
Ian Collins wrote:
>galapogos wrote:

Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.
Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?
That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?
How is the constant represented?

--
Ian Collins.
That's a problem isn't it? A 10 byte constant can't be represented in
any data format, so I guess I have to split it up?

Nov 22 '06 #6
galapogos wrote:
Ian Collins wrote:
>>galapogos wrote:
>>>Ian Collins wrote:
galapogos wrote:
>Hi,
>I'm trying to compare an array of unsigned chars(basically just data
>without any context) with a constant, and I'm not sure how to do that.
>Say my array is array[10] and I want to compare it with the constant
>0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
>How do I do that. Obviously I can't use memcmp() with the
>0x00010203040506070809 since the compiler will use that constant as an
>address and give me a segfault. Other than doing an if (array[i] =
>0x0i) for all 10 values of i, how can I do such a compare? I thought of
>converting the constant to a string and doing a memcmp/strcmp using
>that string but not all the bytes are printable ASCII characters.
>

Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?
That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?

How is the constant represented?

That's a problem isn't it? A 10 byte constant can't be represented in
any data format, so I guess I have to split it up?
Please trim signatures in replies.

const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 };
--
Ian Collins.
Nov 22 '06 #7

Ian Collins wrote:
galapogos wrote:
Ian Collins wrote:
>galapogos wrote:

Ian Collins wrote:
galapogos wrote:
Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.
Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?
That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?
How is the constant represented?
That's a problem isn't it? A 10 byte constant can't be represented in
any data format, so I guess I have to split it up?
Please trim signatures in replies.

const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 };
Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

Nov 22 '06 #8
galapogos wrote:
Ian Collins wrote:
>>galapogos wrote:
>>>Ian Collins wrote:
galapogos wrote:

>That was what I was hoping to avoid. If I have to convert the constant
>to an array, I might as well save some space and compare each byte of
>the array with each byte of the constant. I guess there's no easier way?
>

How is the constant represented?
That's a problem isn't it? A 10 byte constant can't be represented in
any data format, so I guess I have to split it up?

Please trim signatures in replies.

const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x 09};


Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?
But you still have to represent the constants, don't you?

You'll have to fall back on a set of if/else tests with memcmp and const
unsigned char arrays.

--
Ian Collins.
Nov 22 '06 #9

Ian Collins wrote:
galapogos wrote:
Ian Collins wrote:
>galapogos wrote:

Ian Collins wrote:
galapogos wrote:

That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?
How is the constant represented?
That's a problem isn't it? A 10 byte constant can't be represented in
any data format, so I guess I have to split it up?
Please trim signatures in replies.

const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x0 9};

Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?
But you still have to represent the constants, don't you?

You'll have to fall back on a set of if/else tests with memcmp and const
unsigned char arrays.
Yes I know that's the worst case scenario that would work. I was just
hoping there was an easier way that I can implement the switch/case
statements rather than using a bunch of if-else statements that I'm
currently doing.

Nov 22 '06 #10
>Ian Collins wrote:
> const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x0 9};
In article <11**********************@h48g2000cwc.googlegroups .com>
galapogos <go*****@gmail.comwrote:
>Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?
Indeed, you cannot do this at all.

If you dislike both:

if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...

and:

if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...

you can always write a program "P" to produce a C program (or program
fragment) to do the comparisons. You can then decide how fancy Program
P should be; it can even do things like:

/* program P has observed that these three bytes
combine to a value in [0..9180] that is unique for
all the "interesting" values, so that we only have to
verify that all ten values are correct */
switch (array[2] + (3 * array[3]) + (array[7] << 5)) {
static const unsigned char patterns[N][10] = {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 },
... others ...
};
case DIGESTED_CONST_0:
if (memcmp(array, patterns[0], 10) != 0) goto Default;
... matched pattern #0 ...
break;
case DIGESTED_CONST_1:
if (memcmp(array, patterns[1], 10) != 0) goto Default;
... matched pattern #1 ...
break;
... other cases here ...
default:
Default:
... did not match any pattern ...
}

Look up "perfect hash" to find strategies for producing a formula
for computing a number to switch on, and case-labels.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 22 '06 #11
Chris Torek wrote:
>where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

Indeed, you cannot do this at all.
I still don't understand how the basic idea can work, given byteorder
and data alignment considerations.

There is no guarantee that the data in an array are aligned anything
like the arbitrary structure of bits that it's being compared to, (is
there?)
Nov 22 '06 #12
james of tucson wrote:
Chris Torek wrote:

>>>where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

Indeed, you cannot do this at all.


I still don't understand how the basic idea can work, given byteorder
and data alignment considerations.

There is no guarantee that the data in an array are aligned anything
like the arbitrary structure of bits that it's being compared to, (is
there?)
The data and constants are represented as arrays of unsigned char.

--
Ian Collins.
Nov 22 '06 #13

Chris Torek wrote:
Ian Collins wrote:
const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 };

In article <11**********************@h48g2000cwc.googlegroups .com>
galapogos <go*****@gmail.comwrote:
Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

Indeed, you cannot do this at all.

If you dislike both:

if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...

and:

if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...

you can always write a program "P" to produce a C program (or program
fragment) to do the comparisons. You can then decide how fancy Program
P should be; it can even do things like:

/* program P has observed that these three bytes
combine to a value in [0..9180] that is unique for
all the "interesting" values, so that we only have to
verify that all ten values are correct */
switch (array[2] + (3 * array[3]) + (array[7] << 5)) {
static const unsigned char patterns[N][10] = {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 },
... others ...
};
case DIGESTED_CONST_0:
if (memcmp(array, patterns[0], 10) != 0) goto Default;
... matched pattern #0 ...
break;
case DIGESTED_CONST_1:
if (memcmp(array, patterns[1], 10) != 0) goto Default;
... matched pattern #1 ...
break;
... other cases here ...
default:
Default:
... did not match any pattern ...
}

Look up "perfect hash" to find strategies for producing a formula
for computing a number to switch on, and case-labels.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Thanks! I don't think I'll do the perfect hash way though it is pretty
interested if a tad convoluted. The 2nd idea is great though. I've been
looking for a way to represent the pattern with a constant, even a
string, but for some reason I didn't think of that method. I just tried
it and it works. That'll save me the memory for declaring the patterns
as constant arrays. I can just put a couple of #defines with this :)

Nov 23 '06 #14

galapogos wrote:
Chris Torek wrote:

If you dislike both:

if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...

and:

if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
...

Thanks! I don't think I'll do the perfect hash way though it is pretty
interested if a tad convoluted. The 2nd idea is great though. I've been
looking for a way to represent the pattern with a constant, even a
string, but for some reason I didn't think of that method. I just tried
it and it works. That'll save me the memory for declaring the patterns
as constant arrays. I can just put a couple of #defines with this :)
How will it save you memory? memcmp() compares two chunks of memory.
Making one of those chunks anonymous doesn't take it out of memory yet
leave it accessible to memcmp().

Nov 23 '06 #15
"J. J. Farrell" wrote:
galapogos wrote:
>Chris Torek wrote:
>>>
If you dislike both:

if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
and:
if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
...

Thanks! I don't think I'll do the perfect hash way though it is
pretty interested if a tad convoluted. The 2nd idea is great
though. I've been looking for a way to represent the pattern with
a constant, even a string, but for some reason I didn't think of
that method. I just tried it and it works. That'll save me the
memory for declaring the patterns as constant arrays. I can just
put a couple of #defines with this :)

How will it save you memory? memcmp() compares two chunks of memory.
Making one of those chunks anonymous doesn't take it out of memory
yet leave it accessible to memcmp().
How about the following (using strings for convenience), untested:

#include <strings.h>
void switchonpattern(const char *pattern)
{
static const char *patterns[] = {NULL
,"pattern1"
,"pattern2"
....
,"patternN");

#define N = sizeof(patterns) / sizeof(pattern[0];

int i;

patterns[0] = pattern; /* sentinel */
i = N;
while (strcmp(pattern, patterns[i]) i--;
switch (i) {
case N: dopatternN(); break;
case N-1: dopatternNm1(); break;
....
case 1: dopattern1(); break;
case 0:
default: donotfound(); break;
}
#undef N;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 23 '06 #16
CBFalconer wrote:
"J. J. Farrell" wrote:
>>galapogos wrote:
>>>Chris Torek wrote:

If you dislike both:

if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
and:
if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
...

Thanks! I don't think I'll do the perfect hash way though it is
pretty interested if a tad convoluted. The 2nd idea is great
though. I've been looking for a way to represent the pattern with
a constant, even a string, but for some reason I didn't think of
that method. I just tried it and it works. That'll save me the
memory for declaring the patterns as constant arrays. I can just
put a couple of #defines with this :)

How will it save you memory? memcmp() compares two chunks of memory.
Making one of those chunks anonymous doesn't take it out of memory
yet leave it accessible to memcmp().


How about the following (using strings for convenience), untested:

#include <strings.h>
void switchonpattern(const char *pattern)
{
static const char *patterns[] = {NULL
,"pattern1"
,"pattern2"
....
,"patternN");
What if a pattern contains a 0 byte?

--
Ian Collins.
Nov 23 '06 #17

J. J. Farrell wrote:
galapogos wrote:
Chris Torek wrote:
>
If you dislike both:
>
if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
>
and:
>
if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
...
Thanks! I don't think I'll do the perfect hash way though it is pretty
interested if a tad convoluted. The 2nd idea is great though. I've been
looking for a way to represent the pattern with a constant, even a
string, but for some reason I didn't think of that method. I just tried
it and it works. That'll save me the memory for declaring the patterns
as constant arrays. I can just put a couple of #defines with this :)

How will it save you memory? memcmp() compares two chunks of memory.
Making one of those chunks anonymous doesn't take it out of memory yet
leave it accessible to memcmp().
Hmm, so if I do a memcmp(array, "\x00\x01...\xnn"), the compiler still
allocates memory for the 2nd term even though I don't declare it as a
constant/variable?

Nov 23 '06 #18
galapogos said:

<snip>
Hmm, so if I do a memcmp(array, "\x00\x01...\xnn"), the compiler still
allocates memory for the 2nd term even though I don't declare it as a
constant/variable?
Who knows? It doesn't have to, as the code stands. All it *has* to do is
complain that you haven't provided enough argument expressions to memcmp.

Once you've fixed that, then yes, of course the compiler will store the
information you will need for your runtime comparison, and yes, of course
that will occupy some memory, so therefore the compiler will have to
allocate some memory for it to occupy.

Everything Has To Be Somewhere.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 23 '06 #19
galapogos wrote:
Ian Collins wrote:
>galapogos wrote:
>>Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?
But you still have to represent the constants, don't you?

You'll have to fall back on a set of if/else tests with memcmp and const
unsigned char arrays.
Yes I know that's the worst case scenario that would work. I was just
hoping there was an easier way that I can implement the switch/case
statements rather than using a bunch of if-else statements that I'm
currently doing.
Hashtable?

--
imalone
Nov 23 '06 #20
Ian Collins wrote:
CBFalconer wrote:
.... snip ...
>>
How about the following (using strings for convenience), untested:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>
#include <strings.h>
void switchonpattern(const char *pattern)
{
static const char *patterns[] = {NULL
,"pattern1"
,"pattern2"
....
,"patternN");
What if a pattern contains a 0 byte?
Note the underlined above. If you want arrays, use arrays and
memcmp. You may then need a length parameter in addition, or a
record with a length field.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Nov 23 '06 #21

Ian Malone wrote:
galapogos wrote:
Ian Collins wrote:
galapogos wrote:
>Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

But you still have to represent the constants, don't you?

You'll have to fall back on a set of if/else tests with memcmp and const
unsigned char arrays.
Yes I know that's the worst case scenario that would work. I was just
hoping there was an easier way that I can implement the switch/case
statements rather than using a bunch of if-else statements that I'm
currently doing.

Hashtable?

--
imalone
Could you elaborate? I know the concept of a hashtable but I don't have
much experience with it and I don't see how it relates to the problem.
Are you saying each pattern will hash into the table and hopefully no
collisions will take place? Some kinda example would be great...thanks!

Nov 23 '06 #22

Richard Heathfield wrote:
galapogos said:

<snip>
Hmm, so if I do a memcmp(array, "\x00\x01...\xnn"), the compiler still
allocates memory for the 2nd term even though I don't declare it as a
constant/variable?

Who knows? It doesn't have to, as the code stands. All it *has* to do is
complain that you haven't provided enough argument expressions to memcmp.

Once you've fixed that, then yes, of course the compiler will store the
information you will need for your runtime comparison, and yes, of course
that will occupy some memory, so therefore the compiler will have to
allocate some memory for it to occupy.

Everything Has To Be Somewhere.
Thanks, I guess I should've known, since such large constants won't fit
into the literal/immediate field of any instruction set architecture
anyway. I was asking because I'm programming for an embedded system so
memory is kinda valuable.

Nov 23 '06 #23
galapogos wrote:
Ian Malone wrote:
>galapogos wrote:
>>Ian Collins wrote:
galapogos wrote:
Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that
>
switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}
>
where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?
>
But you still have to represent the constants, don't you?

You'll have to fall back on a set of if/else tests with memcmp and const
unsigned char arrays.

Yes I know that's the worst case scenario that would work. I was just
hoping there was an easier way that I can implement the switch/case
statements rather than using a bunch of if-else statements that I'm
currently doing.
Hashtable?

Could you elaborate? I know the concept of a hashtable but I don't have
much experience with it and I don't see how it relates to the problem.
Are you saying each pattern will hash into the table and hopefully no
collisions will take place? Some kinda example would be great...thanks!
If you want to be able to do switch on these numbers then I guess
you have a finite manageable number of them and they are known
at compile time. Come up with some type of preprocessor which
converts your bit patterns to valid case labels, and make the
argument to the switch statement a call to a C function which
does the same hash.

This is basically Galapogos's proposal, although his suggested
'program P' would also try to analyze the set of bit patterns
you use to produce the most efficient outcome, and could be
used when the number of bit patterns exceeds the range
representable by an operand to switch. However if you have
that many of them a switch statement would be a bit absurd.

--
imalone
Nov 23 '06 #24
Ian Malone wrote:
If you
<etc.>
>
This is basically Galapogos's proposal, although his suggested
Sorry, Chris Torek's proposal (mental cut 'n paste broken today)

--
imalone
Nov 23 '06 #25
"galapogos" <go*****@gmail.comwrites:
Richard Heathfield wrote:
>galapogos said:

<snip>
Hmm, so if I do a memcmp(array, "\x00\x01...\xnn"), the compiler still
allocates memory for the 2nd term even though I don't declare it as a
constant/variable?

Who knows? It doesn't have to, as the code stands. All it *has* to do is
complain that you haven't provided enough argument expressions to memcmp.

Once you've fixed that, then yes, of course the compiler will store the
information you will need for your runtime comparison, and yes, of course
that will occupy some memory, so therefore the compiler will have to
allocate some memory for it to occupy.

Everything Has To Be Somewhere.

Thanks, I guess I should've known, since such large constants won't fit
into the literal/immediate field of any instruction set architecture
anyway. I was asking because I'm programming for an embedded system so
memory is kinda valuable.
And even if they do, literal/immediate instruction fields take up
space too.

But if you're using an embedded system where RAM is scarce and ROM is
cheap, it *is* entirely possible that the compiler will put your
string literal in ROM. The standard doesn't say anything about this,
so if you want more information you'll have to try it yourself or ask
in a system-specific forum.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '06 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant...
11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
8
by: vikram | last post by:
i have series of questions 1.How a c program is loaded in memory i mean the whats is the structure that the code segment?? data segment?? 2.When you say const int *p; where is p...
2
by: gavino | last post by:
REHDAT LINUX 4S PHP 4.3.9 LEGACY APP I MOVED NOW EATS MEMORY CAN ANYONE TAE A LOOK ? I FIXED ONE PARTIALLY BY CHANGING TO here are my apache settings: 1 page laoding for a few seconds eat like...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.