sizeof

C++常见面试题(一)

与世无争的帅哥 提交于 2019-12-02 11:12:28
C++和C的区别 设计思想上: C++是面向对象的语言,而C是面向过程的结构化编程语言 语法上: C++具有封装、继承和多态三种特性 C++相比C,增加了许多类型安全的功能,比如强制类型转换 C++支持范式编程,比如模板类、函数模板等 C++中指针和引用的区别 指针有自己的一块空间,而引用只是一个别名 使用sizeof计算一个指针的大小为4,而引用则是被引用对象的大小 指针可以初始化为空,而引用必须被初始化且必须是一个已有对象的引用 作为参数传递时,指针需要被解引用才可以对对象进行操作,而对引用的修改都会直接改变引用所指的对象 可以有const指针,但没有const引用 指针可以指向其他对象,但引用只能是一个对象的引用,不能被改变 如果返回动态内存分配的对象或者内存,必须使用指针,引用可能引起内存泄漏 指针和数组的区别 数组:数组用于存储多个相同类型数据的集合,地址是连续的 指针:指针相当于一个变量,存放的是内存中的地址 区别: • 赋值:同类型指针变量可以相互赋值,数组不行,只能一个一个元素的赋值或拷贝 • 存储方式:数组:数组在内存中是连续存放的,开辟一块连续的内存空间。数组是根据数组的下进行访问的,多维数组在内存中是按照一维数组存储的,只是在逻辑上是多维的。指针:指针很灵活,它可以指向任意类型的数据。指针的类型说明了它所指向地址空间的内存。 • 求sizeof

快速阅读裘宗燕《从问题到程序》1-6章问题总结

若如初见. 提交于 2019-12-02 10:55:52
第一章 程序设计与C语言 1.提出问题 计算机程序的编写从问题机器语言发展到汇编语言,再突破到各式各样的高级语言,逐步切合人类的理解力,极大简化了编程。由于英文编程起步早以及方便性,至今基本全部还是在使用英文编程,为什么使用汉字实现编程普及如此困难?(在使用VS编程时,发现软件编码使用的是UTF编码,解码后在窗口显示时所使用的解码是GBK,所以窗口上汉字的显示出现了乱码,因此引发思考) 2.问题解答 由于我们所使用的键盘都是输入字母,如果要用中文来写,首先还要通过输入法把那些字母转换成汉字,但是对于英文就不需要这层转化。于是,如果用中文,不得不说别说更容易理解,反而增加了麻烦,降低了效率。也就是说,谁方便快捷就选择谁,科技的发展跟国界和地域都没有太直接的关系,如果中文更好,自然有很多人使用中文。 然而,中文编程虽然很早就出现过,为何却没有流行起来呢。首先,这与我国的计算机的技术有关,中国计算机技术发展相对于美国来说晚了一些。因此我国的编程套路早就形成了,也就是说,早就习惯于使用英文编写。即使能够开发一套中文编程,不仅需要很大的成本,想要拓展也是有一定难度的。而且,编程所需的技术要求也是很高的,我国现有的技术未必能开发出比英文编程更加实用的中文编程。也正因为如此,中文编程在某种程度上制约了我国发展。因为电脑不是中国发明的,编程也同样不是。 第二章 数据对象与计算 1.提出问题

【C++】指针和引用的区别

ぃ、小莉子 提交于 2019-12-02 10:50:19
  面试中常会问到: “讲一讲指针和引用的区别” ,主要区别如下表所示: 指针 引用 变量 指针是一个变量,不过变量存储的是一个地址,指向内存中的一个存储单元 引用只是别名 解引用 使用时需要 dereference(*p) 不需要解引用 定义与初始化 可以初始化为nullptr,也可以不初始化(默认为空) 定义时必须初始化,且不能为空 能否修改内容 指针可以指向别的内容,指向的内容也可以修改 引用不能再引用别人 sizeof 指针 sizeof 得到指针的大小,32 位下就是 4 引用 sizeof 得到指向的对象的大小 自增、自减操作 ++、-- 操作是修改指针 修改引用对象的值 可否多级 可以有多级指针 引用只能一级 来源: https://blog.csdn.net/Bob__yuan/article/details/102753623

C语言-memset()

眉间皱痕 提交于 2019-12-02 09:43:06
1. memset()函数原型是extern void *memset(void *buffer, int c, int count) buffer:为指针或是数组, c:是赋给buffer的值, count:是buffer的长度. 这个函数在socket中多用于清空数组.如:原型是memset(buffer, 0, sizeof(buffer)) Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’; 例:char a[100];memset(a, ‘/0’, sizeof(a)); memset可以方便的清空一个结构类型的变量或数组。 如: struct sample_struct { char csName[16]; int iSeq; int iType; }; 对于变量: struct sample_strcut stTest; 一般情况下,清空stTest的方法: stTest.csName[0]=’/0’; stTest.iSeq=0; stTest.iType=0; 用memset就非常方便: memset(&stTest,0,sizeof(struct sample_struct)); 如果是数组: struct sample_struct TEST[10]; 则 memset(TEST,0,sizeof

Size of class with virtual function

旧城冷巷雨未停 提交于 2019-12-02 09:10:39
I was revising the C++ concepts, but I am stuck with a very simple code #include <iostream> using namespace std; class foo { public: //int i; void virtual foobar() { cout << "foobar\n"; } }; int main() { foo f; cout << sizeof(f) << endl; //cout << sizeof(f.i) << endl; return 1; } The output of the above code is 8 But when I removed comments from the code Output is 16 and 4 I did not understand when the class have no member variable present then VPTR size is 8 but after adding a variable size becomes 12. You're working on a platform where pointers are aligned to 8 bytes. Since the virtual table

Invalid read/write of size 8 2

痴心易碎 提交于 2019-12-02 08:16:25
While working on my school project I keep receiving following error from Valgrind after compiling my project on Unix school server and being unable to run the program, as I receive "Segmentation fault: 11". ==95183== Memcheck, a memory error detector ==95183== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==95183== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==95183== Command: ./Euler ==95183== ==95183== Invalid read of size 8 ==95183== at 0x400B65: GInit (Euler.c:64) ==95183== by 0x400DD1: main (Euler.c:118) ==95183== Address 0x1786100 is 0 bytes after

sizeof in static const member initialization

落花浮王杯 提交于 2019-12-02 08:08:37
问题 I have such code: class A { public: unsigned long a; static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++ }; I got compiler error in VC++ and no errors in IAR. Which compiler is right, what C++ standart says about it? 回答1: Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard. 9

针对各种字符串计算其所需存储空间长度(报告素材)

给你一囗甜甜゛ 提交于 2019-12-02 07:59:27
首先计算 单字节字符,宽字符 所需的储存空间长度分别是 sizeof(char) = 1; sizeof(wchar_t) = 2; 并举例说明各字符串所需存 储空间长度(byte为单位) char a[] = “12345”; //单 长度为6 char b[] = “1234中”; //多 长度为7 wchar_t c[] = L"12345";//宽 长度为12 实验结果截图: 来源: https://blog.csdn.net/ALPS233/article/details/102736109

How sizeof operator works in C? [duplicate]

孤街浪徒 提交于 2019-12-02 07:41:05
This question already has an answer here: Output data type of sizeof() operator 1 answer In this below code: #include<stdio.h> int main(void) { printf("%d",sizeof(int)); return 0; } When compiled on gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 compiler it gives warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=] printf("%d",sizeof(int)); Why I am getting this warning? Is it that return type of sizeof is 'long unsigned int' ? When I replaced '%d' with '%ld' the warning went. The sizeof operator is processed at compile time (and can be

Why the array size is 1 [duplicate]

匆匆过客 提交于 2019-12-02 07:11:48
Possible Duplicate: Sizeof an array in the C programming language? I'm trying to write a function that return 1s if a value is in the array. Here's the code: int inArrayInt(int iVal, int iArray[]) { int i; int arrayL = sizeof(*iArray) / sizeof(int); int flag = 0; for(i=0; i < arrayL; i++) { if(iVal == iArray[i]) { flag = 1; } } return flag; } The problem is that arrayL = sizeof(*iArray) / sizeof(int); always evaluates to 1, even if array contains 20 elements. Why? As a parameter int iArray[] is equivalent to int *iArray . So when you do int arrayL=sizeof(*iArray)/sizeof(int); You are actually