sizeof

sizeof和sizeof(string)的问题

删除回忆录丶 提交于 2019-12-07 14:31:11
今天看《程序员面试宝典》一书(为了应付将要到来的微软笔试),看到了sizeof(string)这个问题。在Dev C++上测试的结果是4,很不明白。上网搜了一下,得到如下结果: string strArr1[]={"Trend", "Micro", "Soft"}; sizeof(strArr1)=12 转自: http://apps.hi.baidu.com/share/detail/30398570 关于sizeof(string),今天看那本面试宝典的时候看到这个表达式,有点吃惊,书上写着sizeof(string)=4;当时很纳闷,难道分配4个字节大小的内存给string吗?查阅了相关资料得出结论: string的实现在各库中可能有所不同,但是在同一库中相同一点是,无论你的string里放多长的字符串,它的sizeof()都是固定的,字符串所占的空间是从堆中动态分配的,与sizeof()无关。 sizeof(string)=4可能是最典型的实现之一,不过也有sizeof()为12、32字节的库实现。 但是VC6.0测试后sizeof(string)=16.还是跟编译器有关 #include<iostream> using namespace std; void main(void) { string a[] = {"aaaaa","bbbb","ccc"}; int x =

窥探JVM内存分配和回收的过程

百般思念 提交于 2019-12-07 12:08:35
一、环境 JDK 垃圾收集器 是否启用TLAB 通用JVM参数(堆内存分配见下图) 1.6.0_65 Serial + Serial Old 否 -Xms20m -Xmx20m -Xmn10m -XX:SurvivorRatio=8 二、说明 Minor GC 发生在 新生代 ,当 Eden 区域没有足够空间进行分配 Java对象大多具有 短命 的特性 Minor GC 非常频繁 ,速度也比较 快 Major GC / Full GC 发生在老年代 出现Major GC, 经常 伴随至少一次Minor GC SpeedOf (Minor GC) ≈ 10 * SpeedOf (Major GC) 三、示例 1. 对象优先分配在 Eden区 1.1 说明 新对象,优先考虑分配在 Eden 区域 如果Eden区域没有 足够的空间 容纳新对象,进行GC 如果老年代有 足够的连续空间 用来存储所有新生代对象(或历次晋升的平均大小) ⇒ Minor GC 如果对象太大,以至于 Survivor 区域 无法容纳 ,对象直接晋升到 老年代 否则使用复制算法, 复制 到Survivor区域 否则 ⇒ 先进行一次 Minor GC ,若仍不满足上述条件,进行 Full GC 若 Full GC 后依然内存不足, 1.2 代码 # 代码 public class TestAllocation {

C语言重要函数 memcpy与memmove,memset

江枫思渺然 提交于 2019-12-07 09:15:05
包含头文件: #include <stdlib.h> 1>:malloc calloc realloc free函数 //动态内存分配函数 三个函数的声明分别是: void* malloc(unsigned size); malloc()函数有一个参数,即要分配的内存空间的大小: void* calloc(size_t nelem, size_t elsize); calloc()函数有两个参数,分别为元素的数目和每个元素的大小,这两个参数的乘积就是要分配的内存空间的大小。 如果调用成功,函数malloc()和函数calloc()都将返回所分配的内存空间的首地址。 malloc和calloc都可以分配内存区,但malloc一次只能申请一个内存区,calloc一次可以申请多个内存区.另外calloc会把分配来的内存区初试化为0,malloc不会进行初始化. void* realloc(void* ptr, unsigned newsize); realloc是给一个已经分配了地址的指针重新分配空间,参数ptr为原有的空间地址,newsize是重新申请的地址长度 free的调用形式为free(void*ptr):释放ptr所指向的一块内存空间。 #i nclude <stdio.h> #i nclude <stdlib.h> main() { int *p=NULL; p=(int *

Does the sizeof operator work in preprocessor #if directives?

。_饼干妹妹 提交于 2019-12-07 07:32:46
问题 Can we use the sizeof operator in #if macros? If yes, how? And if not, why? Does the sizeof operator work in preprocessor #if directives? 回答1: No; the sizeof() operator does not work in C preprocessor conditional directives such as #if and #elif . The reason is that the C pre-processor does not know a thing about the sizes of types. You can use sizeof() in the body of a #define 'd macro, of course, because the compiler handles the analysis of the replacement text and the preprocessor does not

ARM cortex-M3 uint_fast32_t vs uint32_t

谁都会走 提交于 2019-12-07 06:34:59
问题 I am developing a program for an STM32Fx cortex-M3 series processor. In stdint.h the following are defined: typedef unsigned int uint_fast32_t; typedef uint32_t uint_least32_t; typedef unsigned long uint32_t; As I understand it. [u]int_fast[n]_t will give you the fastest data type of at least n bits. [u]int_least[n]_t will give you the smallest data type of at least n bits. [u]int[n]_t will give you the data type of exactly n bits. Also as far as i know sizeof(unsigned int) <= sizeof(unsigned

Does sizeof(float) always equal to sizeof(int) on all architectures?

99封情书 提交于 2019-12-07 06:28:46
问题 I'm seeing code allocating memory for float using sizeof(int) . I'm wondering whether sizeof(float) always equal to sizeof(int) on all architectures? float *pointer2Float = (float *) USER_DEFINED_MALLOC (...,..., sizeof(int)) Note: this USER_DEFINED_MALLOC isa wrapper for conventional malloc, I think. Thanks Regards 回答1: No, there are implementations (mainly embedded systems) with 16-bit int and 32-bit float . And of course, the sizes are allowed to be quite different per the standard. 回答2:

Operator sizeof() in C

巧了我就是萌 提交于 2019-12-07 06:24:24
问题 Consider the program main() { printf("%d %d %d",sizeof('3'),sizeof("3"),sizeof(3)); } output from a gcc compiler is: 4 2 4 Why is it so? 回答1: Assuming you are running on a 32-bit system: sizeof a character literal '3' is 4 because character literals are ints in C language (but not C++). sizeof "3" is 2 because it is an array literal with length 2 (numeral 3 plus NULL terminator). sizeof literal 3 is 4 because it is an int. 回答2: A few points to keep in mind: sizeof isn't a function, it's an

Custom byte size?

旧街凉风 提交于 2019-12-07 03:48:09
问题 So, you know how the primitive of type char has the size of 1 byte? How would I make a primitive with a custom size? So like instead of an in int with the size of 4 bytes I make one with size of lets say 16. Is there a way to do this? Is there a way around it? 回答1: Normally you'd just make a struct that represents the data in which you're interested. If it's 16 bytes of data, either it's an aggregate of a number of smaller types or you're working on a processor that has a native 16-byte

struct hack - zero sized array

我与影子孤独终老i 提交于 2019-12-07 02:17:25
问题 #include <iostream> using namespace std; struct node1{ char b[3]; int c[0]; }; struct node2{ int c[0]; }; struct node3{ char b[3]; }; int main() { cout << sizeof(node1) << endl; // prints 4 cout << sizeof(node2) << endl; // prints 0 cout << sizeof(node3) << endl; // prints 3 } My Question is why does the compiler allocate 0 bytes for int c[0] in node2 but allocate 1 byte for its when part of node1. I'm assuming that this 1 byte is the reason why sizeof(node1) returns 4 since without it (like

sizeof- function or macro? [duplicate]

[亡魂溺海] 提交于 2019-12-06 22:51:29
This question already has answers here : Why is sizeof considered an operator? (10 answers) Closed 4 years ago . In c, we are using the sizeof() for getting the size of the datatypes. So how it is defined. It is a macro or a function. Because we can use that as two ways, sizeof int and sizeof(int) so how this is defined in header file. It's neither. It's a built-in operator, whose value is computed at compile-time unless the argument is the name of a variable-length array (added in C99). The parentheses that you often see are not part of the "call", since sizeof is not a function. They are