Comparing memory with a constant

Discussion in 'C Programming' started by galapogos, Nov 22, 2006.

  1. galapogos

    galapogos Guest

    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 =
    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!
    galapogos, Nov 22, 2006
    #1
    1. Advertising

  2. 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."
    james of tucson, Nov 22, 2006
    #2
    1. Advertising

  3. galapogos

    Ian Collins Guest

    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 =
    > 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.
    Ian Collins, Nov 22, 2006
    #3
  4. galapogos

    galapogos Guest

    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 =
    > > 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?
    galapogos, Nov 22, 2006
    #4
  5. galapogos

    Ian Collins Guest

    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 =
    >>>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.
    Ian Collins, Nov 22, 2006
    #5
  6. galapogos

    galapogos Guest

    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 =
    > >>>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?
    galapogos, Nov 22, 2006
    #6
  7. galapogos

    Ian Collins Guest

    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 =
    >>>>>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.
    Ian Collins, Nov 22, 2006
    #7
  8. galapogos

    galapogos Guest

    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 =
    > >>>>>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?
    galapogos, Nov 22, 2006
    #8
  9. galapogos

    Ian Collins Guest

    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,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?
    >

    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.
    Ian Collins, Nov 22, 2006
    #9
  10. galapogos

    galapogos Guest

    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,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?
    > >

    > 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.
    galapogos, Nov 22, 2006
    #10
  11. galapogos

    Chris Torek Guest

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


    In article <>
    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?


    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.
    Chris Torek, Nov 22, 2006
    #11
  12. 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?)
    james of tucson, Nov 22, 2006
    #12
  13. galapogos

    Ian Collins Guest

    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.
    Ian Collins, Nov 22, 2006
    #13
  14. galapogos

    galapogos Guest

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

    >
    > In article <>
    > 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?

    >
    > 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 :)
    galapogos, Nov 23, 2006
    #14
  15. 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().
    J. J. Farrell, Nov 23, 2006
    #15
  16. galapogos

    CBFalconer Guest

    "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--;
    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>
    CBFalconer, Nov 23, 2006
    #16
  17. galapogos

    Ian Collins Guest

    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.
    Ian Collins, Nov 23, 2006
    #17
  18. galapogos

    CBFalconer Guest

    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>
    CBFalconer, Nov 23, 2006
    #18
  19. galapogos

    galapogos Guest

    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?
    galapogos, Nov 23, 2006
    #19
  20. 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.
    Richard Heathfield, Nov 23, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,073
  2. Martin Magnusson
    Replies:
    2
    Views:
    492
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    544
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    332
    Keith Thompson
    Dec 14, 2006
  5. mojumbo
    Replies:
    2
    Views:
    1,321
    Jeff Flinn
    Oct 16, 2009
Loading...

Share This Page