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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:59:19+00:00 2026-05-12T00:59:19+00:00

Can anyone explain the following behaviour to a relative newbie… const char cInputFilenameAndPath[] =

  • 0

Can anyone explain the following behaviour to a relative newbie…

const char cInputFilenameAndPath[] = "W:\\testerfile.bin";
int filesize = 4584;

char * fileinrampointer;
fileinrampointer = (char*) malloc(filesize);

ifstream fsInputFileStream;
fsInputFileStream.open(cInputFilenameAndPath, fstream::in | fstream::binary);

fsInputFileStream.read((char *)(fileinrampointer), filesize);

for(int f=0; f<4; f++)
{
    printf("%x\n", *fileinrampointer);
    fileinrampointer++;
}

I was expecting the above code to rread the first 4 bytes of the file I just read into memory. In the loop I am just displaying the current byte pointed to by the pointer then incrementing the pointer ready to display the next byte.
When I run the code I get:

37

ffffff94

42

ffffffd2

The values are correct but every other value seems to be padded up to a 64 bit number.
Because I’m asking it to display the value indicated by a ‘char sized’ pointer, I was expecting char size results but every other result comes out as a long long.
If I asign *fileinrampointer to an unsigned __int8 it leaves me with the value I want (without the leading 1s) which solves the problem, but I’m just wondering if anyone can explain what is happening above?

  • 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-12T00:59:20+00:00Added an answer on May 12, 2026 at 12:59 am

    The expression *fileinrampointer is of type signed char, and it is being promoted to a signed int while being passed to printf. Thus, the sign bit propagates. Later on, you print it out with %x which means unsigned int in hex, which causes you to print all the 1’s (as opposed to correctly interpret them as a part of a 2’s complement signed integer). Also, ffffffd2 is 8 hex digits which means it’s a 32bit signed integer.

    If you declare fileinrampointer as unsigned char or unsigned __int8 the sign bit doesn’t propagate during promotion. You may as well leave it signed and cast it

    printf("%x\n", static_cast<unsigned char>(*fileinrampointer) );
    

    ISO/IEC 9899:1999 6.5.2.2:

    6 . If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. […]
    […]
    7. If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type. The ellipsis notation in a function prototype declarator causes argument type conversion to stop after last declared parameter. The default argument promotions are performed on trailing arguments.

    This clearly backs up my statement that this is integer promotion, and not printf interpretation.

    Also see
    ISO/IEC 9899:1999 7.15.1.1
    glibc manual A.2.2.4
    glibc manual 12.12.4
    securecoding.cert.org

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

Sidebar

Related Questions

Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
can anyone explain the following to me. I have two JavaScript functions defined in
Can anyone explain why the following test is not passing. The regex is getting
Can anyone explain why the following script behaves differently on two different platforms? Script:
Can anyone explain why the following occurs: String.Format(null, foo) // Returns foo String.Format((string)null, foo)
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction

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.