sizeof

Using sizeof() on malloc'd memory [duplicate]

让人想犯罪 __ 提交于 2019-11-27 08:54:21
Possible Duplicate: newbie questions about malloc and sizeof I am trying to read strings into a program. When I noticed that the strings were sometimes being corrupted, I tried the following code: void *mallocated = malloc(100); printf("sizeof(mallocated) = %d\n", sizeof(mallocated)); According to my program, the size of mallocated was 8 , even though I allocated 100 bytes for it. Because of this, whenever I try to store a string longer than 8 bytes, everything after the 8th byte will sometimes disappear. Why is this happening, and how can I prevent it? Because the size of the "string" pointer

What does sizeof do?

一个人想着一个人 提交于 2019-11-27 08:53:41
What is the main function of sizeof (I am new to C++). For instance int k=7; char t='Z'; What do sizeof (k) or sizeof (int) and sizeof (char) mean? sizeof(x) returns the amount of memory (in bytes) that the variable or type x occupies. It has nothing to do with the value of the variable. For example, if you have an array of some arbitrary type T then the distance between elements of that array is exactly sizeof(T) . int a[10]; assert(&(a[0]) + sizeof(int) == &(a[1])); When used on a variable, it is equivalent to using it on the type of that variable: T x; assert(sizeof(T) == sizeof(x)); As a

problem with sizeof operator

不想你离开。 提交于 2019-11-27 08:47:38
As i want to find array size dynamically in function, i used sizeof operator. But i got some unexpected result. here is one demo program to show you, what i want to do. //------------------------------------------------------------------------------------------ #include <iostream> void getSize(int *S1){ int S_size = sizeof S1/sizeof(int); std::cout<<"array size(in function):"<<S_size<<std::endl; } int main(){ int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6}; getSize(S); std::cout<<"array size:"<<sizeof S/sizeof(int)<<std::endl; return 0; } //--------------------------------------------

ZROI 2019 暑期游记

孤街浪徒 提交于 2019-11-27 08:46:19
ZROI 游记 在自闭中度过了17天 挖了无数坑,填了一点坑 所以还是有好多坑啊zblzbl 挖坑总集: 时间分治 差分约束 Prufer序列 容斥 树上数据结构 例题C (和后面的例题) 点分 最大流、费用流、上下界 Hero meet devil(dp套dp) Pollards’ Rho CRT & exCRT BSGS & exBSGS NTT & FFT 以及 分治NTT & FFT (& 原根) Cipolla 算法(二次剩余) Min25 ZROI D1 T1 小K与集合 题目传送门 首先知道,如果有多于k个1,显然直接把k个1分别放到不同的组,然后剩下的随便放,第一回合就没法拿了。 那么如果只有k-1个1呢,稍微想一下知道剩下那个组至少必须放k个2。因为就算拿到了2的组,会得到k个1。 然后感性想一想,k个i可以当一个i-1。没错,这题这么贪心是对的。 证明咕咕咕了。 这个玩意实现起来有点麻烦,我写的是递归。用ned(x,w)表示需要拿1个x到w组,如果没有x了递归到进行k次ned(x-1,w)。记忆化一下是不是这个数字已经凑不出来了。 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> using namespace std;

Reliably determine the number of elements in an array

孤街醉人 提交于 2019-11-27 08:39:40
Every C programmer can determine the number of elements in an array with this well-known macro: #define NUM_ELEMS(a) (sizeof(a)/sizeof 0[a]) Here is a typical use case: int numbers[] = {2, 3, 5, 7, 11, 13, 17, 19}; printf("%lu\n", NUM_ELEMS(numbers)); // 8, as expected However, nothing prevents the programmer from accidentally passing a pointer instead of an array: int * pointer = numbers; printf("%lu\n", NUM_ELEMS(pointer)); On my system, this prints 2, because apparently, a pointer is twice as large as an integer. I thought about how to prevent the programmer from passing a pointer by

动态规划23题解析

你说的曾经没有我的故事 提交于 2019-11-27 08:31:45
最近两周做了动态规划的23道经典好题,涉及到区间、树形、数位等三种动态规划类型,现在将这23道题的题解写在下面,方便大家借鉴以及我加深记忆。 upd at:20190814 20:46.T7二叉苹果树 1、石子合并 经典的区间DP问题,枚举合并的堆数作为阶段,设f[i][j]表示i->j这段区间内的最优方案,考虑在这段区间内枚举断点k,不难得到f[i][j]=min(f[i][k]+f[k+1][j]+sum(i,j))(最大值同理)。破环为链后直接进行DP即可。 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} if(f)return x;return -x; } int n,a[1005],f[505][505],g[505][505],prefix[505],minn=21374404,maxn; int main() { //freopen("A.in","r"

First_CVE

ぐ巨炮叔叔 提交于 2019-11-27 08:29:12
First_CVE远程溢出攻击 CVE-2013-4730 概述 通过文档得知,此软件由于未能有效处理FTP命令的字符长度,进而引发栈溢出漏洞,导致攻击者可以远程执行任何命令。 令人激动的是,用于FTP登陆的“USER”命令即可出发此漏洞,也就是说我们在未提前获得目标的FTP访问权限的前提下,即可对其进行溢出攻击,因此这个漏洞造成的影响非常严重。 准备 新建项目-桌面向导-关闭安全开发生命周期(SDL)检查 ​#include "pch.h"#include <iostream>#include <WinSock2.h>#pragma comment(lib,"Ws2_32.lib")#include <windows.h>int main(){ // 1.初始化Winsock服务 WSADATA stWSA; WSAStartup(0x0202, &stWSA);​ // 2.创建一个原始套接字 SOCKET stListen = INVALID_SOCKET; stListen = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, 0);​ // 3.在任意地址(INADDR)ANY)上绑定一个端口21 SOCKADDR_IN stService; stService.sin_addr.s_addr = inet_addr(

memset()函数的赋值问题

喜欢而已 提交于 2019-11-27 08:21:39
用memset进行赋值操作 int a[3]; char b[3]; memset(a,0,sizeof(a)); 0 0 0 memset(a,1,sizeof(a)); 16843009 16843009 16843009 memset(a,-1,sizeof(a)); -1 -1 -1 memset(b,'a',sizeof(b)); a a a 参考表 sizeof(char)=1; sizeof(int)=4; sizeof(float)=4; sizeof(long)=4; sizeof(long long)=8; sizeof(double)=8; 下面对这个现象进行解释 memset是按照字节进行赋值的 它是对要进行赋值的变量的后八位二进制进行赋值 因为2^8等于256,已经完全适用于字符的ASCII了 但是呢,对于整数来说 对于1来说 1的二进制是00000000 00000000 00000000 00000001 去后面的8位00000001 而int型占4个字节,当初话的时候是进行每个字节变成00000001 就会变成0x01010101 00000001 00000001 00000001 00000001 十进制是16843009 再来看0 二进制是00000000 00000000 00000000 00000000, 取后8位00000000

'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=] [duplicate]

試著忘記壹切 提交于 2019-11-27 08:21:16
This question already has an answer here: How can one print a size_t variable portably using the printf family? 12 answers I keep getting compile warnings but I don't know how to fix it: '%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [ The program runs fine but I still get the compile warnings: /* Sizeof.c--Program to tell byte size of the C variable */ #include <stdio.h> int main(void) { printf("\nA Char is %d bytes", sizeof( char )); printf("\nAn int is %d bytes", sizeof( int )); printf("\nA short is %d bytes", sizeof( short )); printf("\nA long is %d bytes"

“Invalid application of 'sizeof' to interface 'Fraction' in non-fragile ABI” in Objective-C

。_饼干妹妹 提交于 2019-11-27 08:16:27
问题 I'm studying Steven Kochan's "Programming in Objective-C 2.0". We created a Fraction object with two int instance variables. Later in the book Kochan uses the sizeof statement on a Fraction object's pointer myFract: sizeof(*myFract) When I do this, I receive a compile error: Invalid application of 'sizeof' to interface 'Fraction' in non-fragile ABI http://clang.llvm.org/compatibility.html#sizeof-interface states this error could occur for an object who's size can change but a Fraction