sizeof

Understanding sizeof(char) in 32 bit C compilers

我们两清 提交于 2019-11-29 06:14:36
(sizeof) char always returns 1 in 32 bit GCC compiler. But since the basic block size in 32 bit compiler is 4, How does char occupy a single byte when the basic size is 4 bytes??? Considering the following : struct st { int a; char c; }; sizeof(st) returns as 8 as agreed with the default block size of 4 bytes (since 2 blocks are allotted) I can never understand why sizeof(char) returns as 1 when it is allotted a block of size 4. Can someone pls explain this??? I would be very thankful for any replies explaining it!!! EDIT : The typo of 'bits' has been changed to 'bytes'. I ask Sorry to the

6-2 顺序表操作集 (20 分)

好久不见. 提交于 2019-11-29 05:57:02
题目地址: https://pintia.cn/problem-sets/15 /problems/725 顺序表基本操作 注意初始化空表的时候 List L = (List)malloc(sizeof(List)) 会导致答案错误,但是本地编译并不会报错 正确写法应该是 List L = (List)malloc(sizeof(struct LNode)) List MakeEmpty() { List L = (List)malloc(sizeof(struct LNode));//sizeof(list)会导致答案错误--++ L->Last = -1; return L; } Position Find(List L, ElementType X) { for(int i = 0; i <= L->Last; ++i) { if(L->Data[i] == X) return i; } return ERROR; } bool Insert(List L, ElementType X, Position P) { if(L->Last+1 == MAXSIZE) { printf("FULL"); return false; } if(P > L->Last+1 || P < 0) { printf("ILLEGAL POSITION"); return false; }

ICPC Asia Nanning 2017

旧巷老猫 提交于 2019-11-29 05:18:30
比赛地址: https://www.jisuanke.com/contest/3107?view=challenges A、签到,大概就是输出几个字符串 #include <bits/stdc++.h> using namespace std; int t,n; int main(){ scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;++i) printf("Abiyoyo, Abiyoyo.\n"); printf("Abiyoyo, yo yoyo yo yoyo.\n"); printf("Abiyoyo, yo yoyo yo yoyo.\n"); } return 0; } F、签到2.0,上大数板子搞一下,猜了个结论:答案为最大的k使得2^k<=n 后来跟rhy讨论了下,每轮淘汰掉第1、3、5、7、9个人的话,相当于不断把n除以2,最后留下的必定是一个2^k #include <bits/stdc++.h> #define MAXN 9999 #define MAXSIZE 1000 #define DLEN 4 using namespace std; class BigNum { private: int a[210]; int len; public: BigNum(){ len = 1

sizeof 和 strlen 的区别

拜拜、爱过 提交于 2019-11-29 05:07:24
区别 1.sizeof 是运算符,strlen 是函数。 2.sizeof 可以用类型做参数,strlen 只能用 char* 做参数,且必须是以 \0 结尾的。 3.sizeof 操作符的结果类型是 size_t ,它在头文件中 typedef 为 unsigned int 类型。该类型保证能容纳实现所建立的最大对象的字节大小。 4.编译器在编译时就计算出了sizeof 的结果。而strlen 函数必须在运行时才能计算出来。并且 sizeof计算的是分配时数据类型占内存空间的大小,而 strlen 计算的是字符串实际的长度。 char str[20]="0123456789"; int a=strlen(str); // a=10, strlen 计算字符串的长度,以结束符 `'\0'` 为字符串结束。 int b=sizeof(str); // b=20, sizeof 计算的则是分配的数组 str[20] 所占的内存空间的大小, 不受里面存储的内容改变。 5.sizeof 后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为 sizeof 是个操作符不是个函数。 6.当适用一个结构类型或变量时,sizeof 返回实际的大小;当适用一静态地空间数组, sizeof 归还全部数组的尺寸;sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸。 7.数组做

invalid application of 'sizeof' to incomplete type 'struct array[]'

北慕城南 提交于 2019-11-29 05:03:37
I am trying to organize my project by splitting commands up into separate files for easier maintenance. The issue I am having is trying to iterate over the array of commands defined at compile time. I have created a dumbed down example that reproduces the error I am getting. . ├── CMakeLists.txt ├── commands │ ├── CMakeLists.txt │ ├── command.c │ ├── command.h │ ├── help_command.c │ └── help_command.h └── main.c ./CMakeLists.txt PROJECT(COMMAND_EXAMPLE) SET(SRCS main.c) ADD_SUBDIRECTORY(commands) ADD_EXECUTABLE(test ${SRCS}) commands/CMakeLists.txt SET(SRCS ${SRCS} command.c help_command.c)

Linker Script用法解析(二) Clear_table & Copy_table

你。 提交于 2019-11-29 05:00:30
可执行文件中的.bss段和.data段分别存放未赋初值的全局变量和已赋初值的全局变量,两者的特点分别为: (1).bss段:①无初值,所以不占ROM空间;②运行时存储于RAM;③默认初值为0 (2).data段:①占用ROM空间,用于存放初值;②运行时存储于RAM;③程序启动时将其初值从ROM载入到RAM (ps:两者与.rodata及局部变量的区别:.rodata段存放只读变量即声明为static的变量,存储于ROM中;局部变量是在程序运行时才产生的,存储于栈——stack中。) 根据.bss段的性质,需要对其执行如下操作:①通过linker script给.bss段分配RAM空间;②在启动过程中将.bss段所占RAM空间初始化——由于未赋初值的全局变量默认值为0,因此将此RAM地址段的值全部设为0 根据.data段的性质,需要这样处理:①通过linker script给.data段分配RAM空间和ROM空间;②在启动过程中将.data段所占RAM空间初始化——将存储在ROM中的全局变量初值复制到其RAM空间从而完成全局变量的初始化 以上工作一般在Cpu的启动过程中完成,如英飞凌TC297的启动代码如下所示: void _Core0_start(void) {   ...... Ifx_C_Init(); /*Initialization of C runtime

How does sizeof calculate the size of structures

╄→尐↘猪︶ㄣ 提交于 2019-11-29 03:42:28
I know that a char and an int are calculated as being 8 bytes on 32 bit architectures due to alignment, but I recently came across a situation where a structure with 3 shorts was reported as being 6 bytes by the sizeof operator. Code is as follows: #include <iostream> using namespace std ; struct IntAndChar { int a ; unsigned char b ; }; struct ThreeShorts { unsigned short a ; unsigned short b ; unsigned short c ; }; int main() { cout<<sizeof(IntAndChar)<<endl; // outputs '8' cout<<sizeof(ThreeShorts)<<endl; // outputs '6', I expected this to be '8' return 0 ; } Compiler : g++ (Debian 4.3.2-1

数据结构学习第十七天

随声附和 提交于 2019-11-29 03:34:12
14:39:22 2019-09-01 学习 图的两种遍历方法: ①DFS 深度优先搜索(Depth First Search) ②BFS 广度优先搜索(Breadth First Search) //利用队列实现广度优先 //邻接表实现 及 利用 邻接表 实现 深度优先搜索(DFS) 1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<malloc.h> 4 //表示图的两种方法 5 //邻接矩阵 G[N][N] N个顶点从0到N-1编号 6 //邻接表 表示 7 /*Graph Create(); //建立并返回空图 8 Graph InsertVertex(Graph G, Vertex v); //将v插入G 9 Graph InsertEdge(Graph G, Edge e); //将e插入G 10 void DFS(Graph G, Vertex v); //从顶点v出发深度优先遍历图G 11 void BFS(Graph G, Vertex v); //从顶点v出发宽度优先遍历图G 12 void ShortestPath(Graph G, Vertex v, int Dist[]); //计算图G中顶点v到任意其它顶点的最短距离 13 void MST(Graph G); /

欧拉回路及例题

♀尐吖头ヾ 提交于 2019-11-29 03:24:14
欧拉回路 几个定义 性质与定理 定理1 推论1 定理2 推论2 性质1 性质2 算法主体 例题 uoj117求给定图的欧拉回路 poj1041求字典序最小的欧拉回路 poj1386Play on Words poj2230求无向图欧拉图要求每条边走两遍且方向不同 poj2513字符串的欧拉图 poj2337字典序 poj1637Sightseeing tour求混合图欧拉回路 HDU 2894Poj1392 欧拉回路 几个定义 设G (V,E)是一个图。 1.欧拉回路 图G中经过 每条边一次 并且 仅一次 的回路称作欧拉回路。 2.欧拉路径 图G中经过每条边一次并且仅一次的路径称作欧拉路径。 3.欧拉图 存在欧拉回路的图称为欧拉图。 4.半欧拉图 存在欧拉路径但不存在欧拉回路的图称为半欧拉图。 性质与定理 二、性质与定理 在以下讨论中,假设图 G不存在孤立点(度为0);否则,先将所有孤立点从图中删除。 显然,这样做并不会影响图G中欧拉回路的存在性。 我们经常需要判定一个图是否为欧拉图(或半欧拉图),并且找出一条欧拉回路(或欧 拉路径)。对于无向图有如下结论: 定理1 无向图G为欧拉图,当且仅当G为连通图且所有顶点的度为偶数。 证明: 必要性。 设图G的一条欧拉回路为C。由于C经过图G的每一条边,而图G没 有孤立点,所以C也经过图G的每一个顶点,G为连通图成立

内存对象管理器(基于数组和链表实现)

℡╲_俬逩灬. 提交于 2019-11-29 03:20:25
1.1 数组的特点 连续的内存空间分配并且顺序存储数据,使用之前需要先分配数组个数; 可以通过下标进行访问修改数据,时间复杂度为O(1); 空间效率不是很好,不能随意修改数组大小; 增删数据需要内存拷贝 1.2 链表的特点 内存空间分配是分散的,非连续的存储数据; 不能通过下标直接访问,查找的时间复杂度为O(n); 增删元素,只需要改变前后指针; 1.3 基于数组和指针实现的对象管理器 结合了数组和链表的优点,可以O(1)查找元素,O(1)增删元素; 需要预分配内存; 2.代码实现(C++) /** *@file ObjAllocator *@author jasonxiong *@date 2009-12-09 *@version 1.0 *@brief CObj对象分配类,即新版本的idxobj * * (1)一般用于大型对象的内存分配 */ #ifndef __OBJ_ALLOCATOR_HPP__ #define __OBJ_ALLOCATOR_HPP__ #include <stdio.h> namespace ServerLib { typedef enum enmObjAllocType { EOAT_ALLOC_BY_SELF = 0, //!<对象内存由ObjAllocator自已动态分配 EOAT_ALLOC_BY_SHARED_MEMORY = 1, //!