473,804 Members | 2,070 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:
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 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
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...\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.
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 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?

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...\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.

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_Keit h) 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
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
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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?

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.