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

  • Home
  • SEARCH
  • 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 8539643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:24:53+00:00 2026-06-11T11:24:53+00:00

I was actually hacking a part of code to make it useful for my

  • 0

I was actually hacking a part of code to make it useful for my purpose but I am unable to get what exactly is this statement doing :

    asprintf(&buffr, "%.*s", packet_length, in + 8);

What is meant by “%.*s” specifically ??
Is it safe to use asprintf() function ??
Or using snprintf() function for the same purpose would be better and reliable ??

Thanks in advance.

  • 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-11T11:24:54+00:00Added an answer on June 11, 2026 at 11:24 am

    The format specifier "%.*s" is used to print a specific number of characters from a char* argument (avoids requirement for null termination). In this case, packet_length characters will be printed from in + 8.

    From man asprintf():

    The functions asprintf() and vasprintf() are analogs of sprintf(3) and vsprintf(3), except that they allocate a string large enough to hold the output including the terminating null byte, and return a pointer to it via the first argument. This pointer should be passed to free(3) to release the allocated storage when it is no longer needed.

    It is safe in the sense of buffer overrun but is a non-standard function (from the linked page, it is a GNU extension). In the context of the posted code in which the size of the buffer is known prior to the asprintf() call, the asprintf() provides a convenience for the programmer in that it avoids a malloc() (which is necessary to avoid guessing the maximum size of the buffer to be populated).

    If portability is not a requirement, then snprintf() offers nothing extra than asprintf() and asprintf() is more convenient to use.

    As this is tagged C++ you could use a std::string instead:

    #include <string>
    
    std::string s(in + 8, packet_length);
    

    and use std::string::c_str() if really required. This avoids having to manually free() the buffer produced by asprintf().

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

Sidebar

Related Questions

Actually I am working on this for the last 3 or 4 weeks but
Actually I am stuck in middle of some mysql code. Can anyone suggest a
Actually ,i am working on a livechat application its works fine.This application is tabbed
After surprisingly little hacking about, I managed to get libc++ installed on my Linux
I've spent the past hour hacking away at this: I want to write a
I have an OpenGL ES game that I am hacking together. One part of
I was looking through the sample gwt code provided with gwt, and in hacking
actually i have to display ppt file in browser but it gives download option
Actually I need to reduce the font size of spinner via code, below is
I come from a computer science. background, but I am now doing genomics. My

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.