sizeof

Getting the size in bytes of a vector [duplicate]

心不动则不痛 提交于 2019-12-03 12:49:30
This question already has an answer here: sizeof() std::vector (C++) 2 answers Sorry for this maybe simple and stupid question but I couldn't find it anywhere. I just don't know how to get the size in bytes of a std::vector. std::vector<int>MyVector; /* This will print 24 on my system*/ std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl; for(int i = 0; i < 1000; i++) MyVector.push_back(i); /* This will still print 24...*/ std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl; So how do I get the size of a vector?! Maybe by multiplying 24 (vector size) by the

Linux 13网络服务器与客户端ser,cli

自古美人都是妖i 提交于 2019-12-03 12:25:29
1.网络编程 1.1基本概念 目的: 实现进程间的通信 网络: 把多个主机连接起来,构成一个网络, 互联网: 把网络和网络 连接起来就构成了互联网。 ip: 在网络中唯一标示一台主机 端口: 在某个主机上唯一标示一个进程。 1.2网络模型 2.tcp编程流程 3. tcp实现 3.1tcp ser.c #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<assert.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.0.1"); int res=bind(sockfd,(struct sockaddr*)&saddr

How do you know how much space to allocate with malloc()?

你。 提交于 2019-12-03 11:53:24
I'm a total C newbie, I come from C#. I've been learning about memory management and the malloc() function. I've also came across this code: char *a_persons_name = malloc(sizeof(char) + 2); What I don't understand is how much space this is allocating for a_persons_name . Is it allocating 2 characters (eg. AB) or something else? I also know that you can sometimes get "lucky" with malloc and use unallocated space (which can result in data corruption and seg faults). So how do I know how much space I'm allocating and how much I will need? That snippet is allocating enough space for a 2-character

Why call sizeof operator with two arguments?

三世轮回 提交于 2019-12-03 11:11:19
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ... } (where var is a type) I was quite surprised to see what appeared to be two arguments to the sizeof operator. A quick scan of the ISO/ANSI C99 standard did not yield any secrets. I couldn't come up with any reading of the grammar that allowed a comma there. Searching Google Code, I was able to find an example of this syntax in some PPC code. Is this some PPC-specific syntax? What does it mean? EDIT: It turns out that both what I was looking at--as well as the linked code--is syntax specific to the WindRiver Diab

Determine `sizeof float` without compilation

拜拜、爱过 提交于 2019-12-03 10:32:10
I'd like to know the size of a float in GCC, without having to run the compiler. I know one option is to write a small function and have the compiler print out an assembly listing. There is limits.h , which contains the minimums and maximums, but is there something similar that tells the size of the different implicit types? I'm using GCC on Windows 7 x64; the target platform is ARM7 32 bit mode. Language is C. You can have GCC print out all of the default macros: gcc -dM -E - </dev/null | grep FLT Then you get lines like: #define __FLT_MANT_DIG__ 24 #define __FLT_MAX_EXP__ 128 Now you can

Setup the accelerator framework for fft on the iPhone

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have set a function to setup the accelerator, after i have read : Using the Apple FFT and Accelerate Framework iPhone FFT with Accelerate framework vDSP and apple docs. i did this : void fftSetup() { COMPLEX_SPLIT A; FFTSetup setupReal; uint32_t log2n; uint32_t n, nOver2; int32_t stride; uint32_t i; float *originalReal, *obtainedReal; float scale; uint32_t L = 1024; float *mag = new float[L/2]; log2n = 10 ; n = 1 << log2n; stride = 1; nOver2 = n / 2; printf("1D real FFT of length log2 ( %d ) = %d\n\n", n, log2n); for (i = 0; i < n; i++)

静态创建二叉树及其遍历

对着背影说爱祢 提交于 2019-12-03 10:07:52
我们以这个二叉树为例 1.构造二叉树的链式存储结构 1 struct BTNode{ 2 char data; //结点数据域 3 struct BTNode * pLchild; //左孩子指针-->指向左孩子 4 struct BTNode * pRchild; //右孩子指针-->指向右孩子 5 }; View Code 2.静态的创建二叉树 struct BTNode * createBTree() { struct BTNode* pa = (struct BTNode*)malloc(sizeof(struct BTNode)); struct BTNode* pb = (struct BTNode*)malloc(sizeof(struct BTNode)); struct BTNode* pc = (struct BTNode*)malloc(sizeof(struct BTNode)); struct BTNode* pd = (struct BTNode*)malloc(sizeof(struct BTNode)); struct BTNode* pe = (struct BTNode*)malloc(sizeof(struct BTNode)); pa->data = 'A'; pb->data = 'B'; pc->data = 'C'; pd->data

区间dp提升复习

跟風遠走 提交于 2019-12-03 09:55:32
区间 \(dp\) 提升复习 不得不说这波题真的不简单。。。 技巧总结: 1.有时候转移可以利用背包累和 2.如果遇到类似区间添加限制的题可以直接把限制扔在区间上,每次只考虑 \([l,r]\) 被 \([i,j]\) 完全包含的情况 [BZOJ4897] [Thu Summer Camp2016] 成绩单 典型的利用背包转移, \(dp[i][j]\) 表示处理完这段 \([i,j]\) 区间的答案 转移时一段区间可以被先处理掉或者直接从已经处理完的 \(dp[i][j]\) 中取过来,这个可以用一个背包维护 const int N=52,P=999983,INF=1e9; int n,a,b,w[N]; int dp[N][N]; int tmp[N][N][N]; int id[N*N]; inline void cmin(int &a,int b){ ((a>b)&&(a=b)); } inline void cmax(int &a,int b){ ((a<b)&&(a=b)); } int main(){ n=rd(),a=rd(),b=rd(); w[0]=0,w[n+1]=1001; rep(i,1,n) w[i]=rd(); rep(i,0,n+1) id[w[i]]=i; memset(dp,63,sizeof dp); rep(i,1,n) { int ma=0

Segmentation Fault when trying to use scanf on a struct

匿名 (未验证) 提交于 2019-12-03 09:52:54
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm pretty new to c and I'm pretty frustrated at the moment as well. Here's the code I have: typedef struct { char* fName; char* lName; char* pNum; char* address; char* email; } contactInfo; void addContact(){ contactInfo *contact; contact = (contactInfo *) malloc (sizeof(contactInfo)); printf("\n[Add a contact]\nFirst Name: "); scanf("%s", contact->fName); printf("%s", contact->fName); } For some reason when I enter a value for the scanf it gives me a segmentation fault. If I try to add a & in front of the contact->fName I get an error as

如何让计算机自己计算数组中元素的个数 sizeof

我们两清 提交于 2019-12-03 09:51:39
# include <stdio.h> int main() { int a[] = { 0,1,2,3,4,5,6,7,8,9,2,3,4,5,6 }; int x = sizeof(a) / sizeof(a[0]); printf("数组中的元素有%d个",x); return 0; } sizeof 给出整个数组所占内容的大小,单位是字节 sizeof(a)/sizeof(a[0]) sizeof(a[0])给出单位元素的大小,相除后得到数组中元素的个数 这样写代码,输入的个数改变代码不用变,程序自己会适应 来源: https://www.cnblogs.com/panghushalu/p/11786127.html