user-0 提问: malloc申请内存机制，为什么申请超出我的内存空间了还能成功，很奇怪

回复:
user: user-1
content: 申请失败。。。。。。。。。

user: user-2
content: C Integer Constants An “integer constant” is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed.  Syntax  integer-constant :  decimal-constant integer-suffix opt octal-constant integer-suffix opt hexadecimal-constant integer-suffix opt  decimal-constant :  nonzero-digit decimal-constant digit  octal-constant :  0 octal-constant octal-digit  hexadecimal-constant :  0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit  nonzero-digit : one of  1 2 3 4 5 6 7 8 9  octal-digit : one of  0 1 2 3 4 5 6 7  hexadecimal-digit : one of  0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F  integer-suffix :  unsigned-suffix long-suffix opt long-suffix unsigned-suffix opt   unsigned-suffix : one of  u U  long-suffix : one of  l L  64-bit integer-suffix :  i64  Integer constants are positive unless they are preceded by a minus sign (–). The minus sign is interpreted as the unary arithmetic negation operator. (See Unary Arithmetic Operators in Chapter 4 for information about this operator.)  If an integer constant begins with the letters 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal.   The following lines are equivalent:  0x1C   /* = Hexadecimal representation for decimal 28 */ 034    /* = Octal representation for decimal 28 */  No white-space characters can separate the digits of an integer constant. These examples show valid decimal, octal, and hexadecimal constants.   /* Decimal Constants */ 10 132 32179  /* Octal Constants */ 012 0204 076663  /* Hexadecimal Constants */ 0xa or 0xA 0x84 0x7dB3 or 0X7DB3 /*** *limits.h - implementation dependent values * *       Copyright (c) Microsoft Corporation.  All rights reserved. * *Purpose: *       Contains defines for a number of implementation dependent values *       which are commonly used in C programs. *       [ANSI] * *       [Public] * ****/  #pragma once  #include <crtdefs.h>  #ifndef _INC_LIMITS #define _INC_LIMITS  #ifndef _CRTBLD /* This version of the header files is NOT for user programs.  * It is intended for use when building the C runtimes ONLY.  * The version intended for public use will not have this message.  */ #error ERROR: Use of C runtime library internal header file. #endif  /* _CRTBLD */  #define CHAR_BIT      8         /* number of bits in a char */ #define SCHAR_MIN   (-128)      /* minimum signed char value */ #define SCHAR_MAX     127       /* maximum signed char value */ #define UCHAR_MAX     0xff      /* maximum unsigned char value */  #ifndef _CHAR_UNSIGNED #define CHAR_MIN    SCHAR_MIN   /* mimimum char value */ #define CHAR_MAX    SCHAR_MAX   /* maximum char value */ #else  /* _CHAR_UNSIGNED */ #define CHAR_MIN      0 #define CHAR_MAX    UCHAR_MAX #endif  /* _CHAR_UNSIGNED */  #define MB_LEN_MAX    5             /* max. # bytes in multibyte char */ #define SHRT_MIN    (-32768)        /* minimum (signed) short value */ #define SHRT_MAX      32767         /* maximum (signed) short value */ #define USHRT_MAX     0xffff        /* maximum unsigned short value */ #define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */ #define INT_MAX       2147483647    /* maximum (signed) int value */ #define UINT_MAX      0xffffffff    /* maximum unsigned int value */ #define LONG_MIN    (-2147483647L - 1) /* minimum (signed) long value */ #define LONG_MAX      2147483647L   /* maximum (signed) long value */ #define ULONG_MAX     0xffffffffUL  /* maximum unsigned long value */ #define LLONG_MAX     9223372036854775807i64       /* maximum signed long long int value */ #define LLONG_MIN   (-9223372036854775807i64 - 1)  /* minimum signed long long int value */ #define ULLONG_MAX    0xffffffffffffffffui64       /* maximum unsigned long long int value */  #define _I8_MIN     (-127i8 - 1)    /* minimum signed 8 bit value */ #define _I8_MAX       127i8         /* maximum signed 8 bit value */ #define _UI8_MAX      0xffui8       /* maximum unsigned 8 bit value */  #define _I16_MIN    (-32767i16 - 1) /* minimum signed 16 bit value */ #define _I16_MAX      32767i16      /* maximum signed 16 bit value */ #define _UI16_MAX     0xffffui16    /* maximum unsigned 16 bit value */  #define _I32_MIN    (-2147483647i32 - 1) /* minimum signed 32 bit value */ #define _I32_MAX      2147483647i32 /* maximum signed 32 bit value */ #define _UI32_MAX     0xffffffffui32 /* maximum unsigned 32 bit value */  /* minimum signed 64 bit value */ #define _I64_MIN    (-9223372036854775807i64 - 1) /* maximum signed 64 bit value */ #define _I64_MAX      9223372036854775807i64 /* maximum unsigned 64 bit value */ #define _UI64_MAX     0xffffffffffffffffui64  #if _INTEGRAL_MAX_BITS >= 128 /* minimum signed 128 bit value */ #define _I128_MIN   (-170141183460469231731687303715884105727i128 - 1) /* maximum signed 128 bit value */ #define _I128_MAX     170141183460469231731687303715884105727i128 /* maximum unsigned 128 bit value */ #define _UI128_MAX    0xffffffffffffffffffffffffffffffffui128 #endif  /* _INTEGRAL_MAX_BITS >= 128 */  #ifndef SIZE_MAX #ifdef _WIN64 #define SIZE_MAX _UI64_MAX #else  /* _WIN64 */ #define SIZE_MAX UINT_MAX #endif  /* _WIN64 */ #endif  /* SIZE_MAX */  #if __STDC_WANT_SECURE_LIB__ /* While waiting to the C standard committee to finalize the decision on RSIZE_MAX and rsize_t,  * we define RSIZE_MAX as SIZE_MAX  */ #ifndef RSIZE_MAX #define RSIZE_MAX SIZE_MAX #endif  /* RSIZE_MAX */ #endif  /* __STDC_WANT_SECURE_LIB__ */   #endif  /* _INC_LIMITS */

user: user-3
content: 引用 7 楼 xinsuixiaofeiyu 的回复:引用 2 楼 jerry_dqh 的回复:申请的是线性空间，进程空间，并不能完全对应物理内存，你的64位的程序吧，32位的程序开始的也不能申请这么大。但程序没报错啊，很奇怪。4楼的是对的。但不明白为什么只有那么多内存   溢出了。

user: user-4
content: 数值溢出。。。

user: user-5
content: 因为windows由虚拟内存机制

user: user-6
content: 引用 6 楼 xinsuixiaofeiyu 的回复:[quote=引用 4 楼 zjq9931 的回复:] 32位上面运行了一下： 结果是：832569344 794M  #include<stdio.h> int main() { 	size_t sA; 	sA=sizeof(int)*20000*200000*200000*20; 	printf("%u\n", sA); 	return 0; } 32位的话sizeof（int）是4个字节啊，那sizeof(int)*20000*200000*200000*20明显大于794M啊，是不是这个式子计算式把常数做了类型转换呢？[/quote] 是的，超范围的数，都会截断。只要32位的（32位平台）。由于是无符号的，所以，不会出现负数，但强制转换成有符号的就会有可能是负数了，这个时候就有问题了。

user: user-7
content: 超出类型，溢出了，最终变成 832564344，也就是越位793MB 如果最后是乘以200，溢出后变成4030726144，也就是3.8G 显然，前者会成功，后者会失败

user: user-0
content: 引用 2 楼 jerry_dqh 的回复:申请的是线性空间，进程空间，并不能完全对应物理内存，你的64位的程序吧，32位的程序开始的也不能申请这么大。但程序没报错啊，很奇怪。4楼的是对的。但不明白为什么只有那么多内存

user: user-0
content: 引用 4 楼 zjq9931 的回复:32位上面运行了一下： 结果是：832569344 794M  #include<stdio.h> int main() { 	size_t sA; 	sA=sizeof(int)*20000*200000*200000*20; 	printf("%u\n", sA); 	return 0; } 32位的话sizeof（int）是4个字节啊，那sizeof(int)*20000*200000*200000*20明显大于794M啊，是不是这个式子计算式把常数做了类型转换呢？

user: user-8
content: 应该是64位的。

user: user-6
content: 32位上面运行了一下： 结果是：832569344 794M  #include<stdio.h> int main() { 	size_t sA; 	sA=sizeof(int)*20000*200000*200000*20; 	printf("%u\n", sA); 	return 0; }

user: user-6
content: malloc的参数是一个 unsigned integer 把那个数据计算结构放到一个无符号整形里面就是申请的内存空间。 看似已经超出来范围，但得到的整形数的结果不会超出整形数。

user: user-9
content: 申请的是线性空间，进程空间，并不能完全对应物理内存，你的64位的程序吧，32位的程序开始的也不能申请这么大。

user: user-10
content: sizeof(int)*20000*200000*200000*20溢出了，实际可能不是很大...