How does float store in C language?

Source: Internet
Author: User

float how memory is stored

Type

Number of storage bits

Total number of digits

Offset value
(offset)

Digit (S)

Order Code (E)

Mantissa (M)

Short real number (float)

1

8

23

32

127

Long real number (double)

1

11

52

64

1023

N (10) = 123.456,

Translates into binary representations:

N (2) = 1111011.01110100101111001

= 1. 11101101110100101111001 (...) *2^6

then e–127 = 6; E =127 + 6 = 133 (Ten) = 10000101 (2)

M =111 0110 1110 1001 0111 1001 ( max digits omitted 1, total 23bit)

together it is:

S E M

0 10000101 1110110 1110 1001 0111 1001

4bit One interval:

0100 0010 1111 0110 1110 1001 0111 1001

4 2 F 6 E 9 7 9

The first two days of interviewing a company, did not answer a question, summarized as follows, for reference.

Q: How much memory is consumed by instance variables of this struct type:
struct STRUCT1
{
int i;
SHORTJ;
Charc;
};
I ask: What language is the machine what the compiler environment?
He answered, VC6.0.
Me: The memory is aligned and then takes up 8byte.
He went on to ask: how to make it only 7byte?
My first reaction is to use a bit segment, is preparing to answer, and feel wrong, the bit segment can not let it not aligned AH. Thought for a few seconds, or not, had to return to his said not to play ...


This afternoon went to the library to turn over "code optimization: Efficient Use of memory", found that there are two methods mentioned:
Method1:
#pragma pack (push)
#pragma pack (1)
struct STRUCT1
{
int i;
SHORTJ;
Charc;
};
#pragma pack (POP)
The original #pragma pack had several forms, and I was exposed to the #pragma pack (n), that is, the variable is aligned in N-byte.

Method2:
Modify the parameters of the compilation directive to prevent memory alignment:
VC + +:/zn1 (VS2005, right-click Project-Properties-Configuration Properties-c/c++-code generation-Structure Member alignment-select "1 bytes (/ZN1):" (that is, prevent memory alignment), default is to use the default value, that is, the structure of the largest space-occupying members to align. To align the size of the.
Borland C + +:/-A1


Method2is to disable memory alignment for the entire project,and the Law1memory alignment can be disabled for a specific structure,it provides greater flexibility.



In addition, the book also mentions:
char is not aligned;
Short along the even address alignment;
The int/float is aligned along the address of a multiple of the value 4.
Double the address aligned along a multiple of the value 8.


VS2005, the default is to use the default value, that is, according to the structure of the largest space-occupying members to align, we can test the following structure:
struct STRUCT2
{
Chari
SHORTD;
Doublec;
SHORTJ;
};

Leave the default value or modify the compilation parameter/zn?, guess how much the result of sizeof? :)
printf ("struct2:%d\n", sizeof (STRUCT2));
Struct2 St;
printf ("%p\n", &st.i);
printf ("%p\n", &ST.D);
printf ("%p\n", &st.c);
printf ("%p\n", &ST.J);

Supplement: manual alignment of the data:

Char *p;
int temp = align_power-1;
p= (char*) malloc (need_size + temp;
p= (char*) malloc (((int) p+temp) &temp);//modified P, so remember to return to the position before releasing P
Note: Align_power is required for Zishing, char* (which can also be int*) is a pointer type. Also, remember to point to the first address of the requested memory before releasing p.

How does float store in C language?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.