Thread: memcmp ?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    memcmp ?

    Can someone tell me why the following is not working?

    Code:
    char temp[HOW_MANY_SENSORS] = { [0 ... (HOW_MANY_SENSORS - 1)] = 0xFF};
    Code:
        test = memcmp((const void *)temp, (const void *)0xFF, 1);
    test is an int.
    If I change temp to 0xFF I get the expected test = 0 but for the life of me I cannot get temp to work...I've tried &temp, &temp[0] ....in all instances test comes back as a non-zero integer

    I've also tried strncmp and get the same type of results??

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Why would you expect some random byte at memory location 0xFF would equal 0xFF?
    I would expect a segfault, actually.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    There is nothing random here...I have an actual use case where a segment in memory will initialize to 0xFF then over time the space can change....I need a conditional to check if the space is void (ie 0xFF)....how can I do that over HOW_MANY_SENSORS????
    my example above is kept simple so someone can show me what I am doing wrong

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Casting (in this case, with (const void *)) is a way to tell the compiler that you know what you're doing. You don't know what you're doing (otherwise you wouldn't be asking us for help), so remove the casts and report what errors you get from the compiler.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why don't you start with
    Code:
    for ( i = 0 ; i < HOW_MANY_SENSORS ; i++ ) {
      if ( temp[i] == 0xff ) {
        // something
      }
    }
    Instead of digging yourself a hole in your attempt at micro-optimisation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Code:
    char test[3] = { [0 ... 2] = 0xFF};
    
    int flag = 0;
    
    flag = strncmp(test, 0xFF, 3);
    Yes I could do the for loop but was hoping to do it like above bcz I am placing this in a conditional &&

    I get the following warnings from the compiler:

    Description Resource Path Location Type
    #154-D conversion of nonzero integer to pointer j.c /ThermalSensorNoLib/Source line 18 C/C++ Problem
    #169-D argument of type "int" is incompatible with parameter of type "const char *" j.c /ThermalSensorNoLib/Source line 18 C/C++ Problem

    but if I place (const char *) in front of 0xFF the warning disappears. 0xFF is NOT a memory address it is just a char that I fill the array with...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write the loop and place it in a function that returns a value that you can then check in a conditional. Problem solved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. !memcmp in set
    By thames in forum C Programming
    Replies: 2
    Last Post: 01-13-2013, 12:55 PM
  2. Where is the defination of memcmp?
    By shwetha_siddu in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 06:47 AM
  3. Access Violation using memcmp?
    By someprogr in forum C Programming
    Replies: 3
    Last Post: 12-31-2007, 01:45 AM
  4. Question about using memcmp ()
    By Vortex in forum C++ Programming
    Replies: 5
    Last Post: 06-03-2006, 03:44 PM

Tags for this Thread