zoukankan      html  css  js  c++  java
  • malloc的堆内存挂死原因;负数的表示

    malloc的堆内存,内存越界导致挂死。
    还有内存被覆盖挂死。(本内存被别人写了)
    访问没有申请的内存,挂死。

    ================================================================================================

    0xffffffff -1
    0xfffffffE -2
    0xfffffffD -3
    0xfffffffC -4
    0xfffffffB -5
    0xfffffffA -6
    0xfffffff9 -7
    0xfffffff8 -8
    0xfffffff7 -9
    0xfffffff6 -10
    0xfffffff5 -11
    0xfffffff4 -12

    The C standard doesn't mandate any particular way of representing negative signed numbers.

    In most implementations that you are likely to encounter, negative signed integers are stored in what is called two's complement. The other major way of storing negative signed numbers is called one's complement.

    The two's complement of an N-bit number x is defined as 2^N - x. For example, the two's complement of 8-bit 1 is 2^8 - 1, or 1111 1111. The two's complement of 8-bit 8 is 2^8 - 8, which in binary is 1111 1000. This can also be calculated by flipping the bits of x and adding one. For example:

     1      = 0000 0001
    ~1      = 1111 1110
    ~1 + 1  = 1111 1111
    -1      = 1111 1111

     21     = 0001 0101
    ~21     = 1110 1010
    ~21 + 1 = 1110 1011
    -21     = 1110 1011

    The one's complement of an N-bit number x is defined as x with all its bits flipped, basically.

     1      = 0000 0001
    -1      = 1111 1110

     21     = 0001 0101
    -21     = 1110 1010

    Two's complement has several advantages over one's complement. For example, it doesn't have the concept of 'negative zero', which for good reason is confusing to many people. Addition, multiplication and subtraction work the same with signed integers implemented with two's complemented as they do with unsigned integers as well.
    shareimprove this answer
        
    answered May 14 '14 at 9:44
    Miles Rout
    35418
        
    add a comment
    up vote
    7
    down vote
        

    There are three well known methods for representing negative values in binary:

        Signed magnitude. This is the easiest to understand, because it works the same as we are used to when dealing with negative decimal values: The first position (bit) represents the sign (0 for positive, 1 for negative), and the other bits represent the number. Although it is easy for us to understand, it is hard for computers to work with, especially when doing arithmetic with negative numbers.
        In 8-bit signed magnitude, the value 8 is represented as 0 0001000 and -8 as 1 0001000.

        One's complement. In this representation, negative numbers are created from the corresponding positive number by flipping all the bits and not just the sign bit. This makes it easier to work with negative numbers for a computer, but has the complication that there are two distinct representations for +0 and -0. The flipping of all the bits makes this harder to understand for humans.
        In 8-bit one's complement, the value 8 is represented as 00001000 and -8 as 11110111.

        Two's complement. This is the most common representation used nowadays for negative integers because it is the easiest to work with for computers, but it is also the hardest to understand for humans. When comparing the bit patterns used for negative values between one's complement and two's complement, it can be observed that the same bit pattern in two's complement encodes for the next lower number. For example 11111111 stands for -0 in one's complement and for -1 in two's complement, and similarly for 10000000 (-127 vs -128).
        In 8-bit two's complement, the value 8 is represented as 00001000 and -8 as 11111000.

  • 相关阅读:
    JS基础(数组的基础知识)
    JS基础(JavaScript三大特点、基本数据类型检测、逻辑运算符的短路运算、几大循环结构的特点)
    JS基础(注释方法、基本数据类型、代码位置、运算符、字符转换、Math常用对象属性及方法、Number常用对象属性及方法)
    JS基础(JavaScript的三大主要组成部分、JavaScript浏览器内核、JavaScript变量)
    CSS入门(定位的简单总结)
    CSS入门(定位之浮动定位、伪类之鼠标悬停、光标修改和透明度修改和列表样式)
    CSS入门(背景各种属性的详解、垂直居中和过渡效果的详解、渐变效果的简单讲解、雪碧图和精灵图)
    CSS入门(边框、轮廓、元素的分类、盒子模型的三个构成部分)
    CSS入门(css简介与样式汇总、CSS的使用方式和特征、CSS基础选择器和复杂选择器、边框阴影)
    简单学习HTML
  • 原文地址:https://www.cnblogs.com/elseliving/p/7895245.html
Copyright © 2011-2022 走看看