Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6628043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:05:06+00:00 2026-05-25T22:05:06+00:00

please look through the following program.. #include <stdio.h> #include <malloc.h> #include <iostream> #include <string.h>

  • 0

please look through the following program..

#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    char *key1 = (char*)malloc(sizeof(char)*26);
    char *key2 = (char*)malloc(sizeof(char)*26);


    int intkey1 = 10;
    int intkey2 = 10;

    float floatkey1 = 13.5f;
    float floatkey2 = 13.5f;

    double doublekey1 = 18.5;
    double doublekey2 = 18.5;

    char charkey1[10] = "nikhil";
    char charkey2[10] = "nikhil";

    //cout<<sizeof(int)<<" "<<sizeof(float)<<" "<<sizeof(double)<<endl;

    memcpy(key1,&intkey1,sizeof(int));
    memcpy(key2,&intkey2,sizeof(int));

    int offset = sizeof(int);

    memcpy(&key1[offset],&floatkey1,sizeof(float));
    memcpy(&key2[offset],&floatkey2,sizeof(float));

    offset = offset + sizeof(float);

    memcpy(&key1[offset],&doublekey1,sizeof(double));
    memcpy(&key2[offset],&doublekey2,sizeof(double));

    offset = offset + sizeof(double);

    memcpy(&key1[offset],charkey1,sizeof(charkey1));
    memcpy(&key2[offset],charkey2,sizeof(charkey2));


    int diff = memcmp(key1,key2,26);
    if( diff > 0)
        cout<<"Key1 is greater than key2"<<endl;
    else if(diff < 0)
        cout<<"Key1 is smaller than key2"<<endl;
    else
        cout<<"Both keys are equal"<<endl;
}

I am developing a B+ tree for a database engine for which i need a generic keyCompare method…Is this way of comparing byte streams by memcpy always the safe and foolproof or should i need to compare based on the types of the fields present in char key that i get from scanning column information present in the database engine? IS this fastest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:05:06+00:00Added an answer on May 25, 2026 at 10:05 pm

    Is this way of comparing byte streams by memcpy always the safe and foolproof

    It doesn’t produce the correct result with integers and floats on little-endian architectures. And it can’t handle special floating point values like inf and nan.

    or should i need to compare based on the types of the fields present in char key that i get from scanning column information present in the database engine?

    It should compare types using type-specific methods. For strings that involves using locales.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please look at the following code listing: #include <iostream> using namespace std; class Base
Hi I am drawing a circle using wxpython for a project.Please look through the
Please look that alias name. I hope to set the value into a string
am new here. i have a slight problem; PLease look at the following code
Getting the above error in following code. How to rectify it. Thanks. Please look
Please have a look at http://live.heritageartpapers.co.uk/catalogue.aspx and advise me on the following: Currently customers
Please look through code below: // A.class File file = new File(blah.txt); FileWriter fileWriter
please look the following code: <?php if ($_GET['picture'] == 1) { echo <img src=\1.jpg\
Note:- It just might be a iterating XML nodes using VBA question. Please look
Would someone please take a look at the following code fragments (all from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.