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 8513341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:32:22+00:00 2026-06-11T04:32:22+00:00

The following piece of code worked well in one program and caused a bus

  • 0

The following piece of code worked well in one program and
caused a bus error in the other

    char *temp1;
    temp1=(char*)malloc(2);
    for(b=3;b>=0;b--)
       {
       sprintf(temp1,"%02x",s_ip[b]);
       string temp2(temp1); 
       temp.append(temp2);
       } 

s_ip[b] is of type byte and temp is a string. What caused this bus error and how can I solve this?
Moreover, what is the reason for this strange behaviour?

  • 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-06-11T04:32:23+00:00Added an answer on June 11, 2026 at 4:32 am

    The temp buffer must be 3 chars in length as sprintf() will append a null terminator after the two hex characters:

    char temp1[3];
    

    There appears to be no reason to be using dynamically allocated memory. Note you can avoid the creation of the temporary string named temp2 by using std::string::append():

    temp.append(temp1, 2);
    

    An alternative is to avoid using sprintf() and use a std::ostringstream with the IO manipulators:

    #include <sstream>
    #include <iomanip>
    
    std::ostringstream s;
    
    s << std::hex << std::setfill('0');
    
    for (b = 3; b >= 0; b--)
    {
        s << std::setw(2) << static_cast<int>(s_ip[b]);
    }
    

    Then use s.str() to obtain the std::string instance.

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

Sidebar

Related Questions

The following piece of code worked fine if garbage collection was not turned on
the following piece of code worked fine all day yesterday public Image getImage() {
The following piece of code in my JSP caused a cross site scripting vulnerability
I have a program that contains the following piece of code for constructing an
The following piece of code keeps giving me an error message of: Object reference
The following piece of code namespace ConsoleApplication1 { class Program { static void Main(string[]
The following piece of code doesn't make an application to run. Can some one
the following piece of code works fine when i use it among some code
The following piece of code is used to print the time in the logs:
The following piece of code should read each line of the file and operate

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.