By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

Comparing memory with a constant

P: n/a
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
Share this Question
Share on Google+
25 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
>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 (4039.22'N, 11150.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

P: n/a
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

P: n/a
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

P: n/a

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 (4039.22'N, 11150.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

P: n/a

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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.