473,804 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
0x0001020304050 6070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x0001020304050 6070809 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
25 5721
>Ian Collins wrote:
> const unsigned char ref[] =
{0x00,0x01,0x0 2,0x03,0x04,0x0 5,0x06,0x07,0x0 8,0x09};
In article <11************ **********@h48g 2000cwc.googleg roups.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 0x0001020304050 6070809 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\x 03\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 "interestin g" 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 0x0001020304050 6070809 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 0x0001020304050 6070809 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************ **********@h48g 2000cwc.googleg roups.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 0x0001020304050 6070809 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\x 03\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 "interestin g" 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\x 03\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\x 03\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\x 03\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\x 03\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...\xn n"), 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...\xn n"), 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 0x0001020304050 6070809 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

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

Similar topics

6
2017
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 memory in terms of the length of the list i? If so why not and how could I change it to be so? I am sure the solution is O(n) since the list must only iterated once and the dictionary is O(1), correct? Thanks for the help!! #You are given a...
11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
9
5933
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 letter "d" this is what i have so far if (argv) { write_read_input_file(filename); }
8
2934
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 stored in the memory?? what happens internal so that its a read only. 3. when declared volatile int *p where exactly in the memory it is stored.
2
1679
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 48% of a xeon 3.0 processor!!! <IfModule prefork.c> StartServers 8 MinSpareServers 5 MaxSpareServers 20
10
11331
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
13064
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.