sizeof

Knowing the size of the array using pointer

孤者浪人 提交于 2019-12-18 09:45:01
问题 How can i know the size of the array using a pointer that is allocated using malloc? #include <stdio.h> int main(){ int *ptr = (int *)malloc(sizeof(int * 10)); printf("Size:%d",sizeof(ptr)); free(ptr_one); return 0; } I get only the size of the pointer in this case which is 8.How to modify the code to get the size of array which will be 40. 回答1: You cannot. You will need to do the bookkeeping and keep track of it yourself. With new you allocate dynamic memory and while deallocating the memory

What is the sizeof std::array<char, N>? [duplicate]

橙三吉。 提交于 2019-12-18 07:44:58
问题 This question already has an answer here : Is the size of std::array defined by standard (1 answer) Closed 5 years ago . What does the C++ standard say about what sizeof(std::array<char, N>) should be (for some constant N )? In a comment to a different question, it was mentioned that std::array is not always "stack allocated". The comment was in response to a different comment that speculated that putting a too large of a constant for std::array that is declared as a local variable could

Understanding sizeof(char) in 32 bit C compilers

只谈情不闲聊 提交于 2019-12-18 04:40:42
问题 (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

How can I get sizeof a vector::value_type?

允我心安 提交于 2019-12-18 03:05:24
问题 I want to get sizeof of the type that is contained in a vector. Here is what I tried: #include <iostream> #include <vector> int main() { std::vector<uint> vecs; std::cout << sizeof(vecs.value_type) << std::endl; return 0; } From my understanding this should be correct. However, when compiling with GCC 4.8.1 this is what I get: test-sizeof.cpp: In function ‘int main()’: test-sizeof.cpp:7:27: error: invalid use of ‘std::vector<unsigned int>::value_type’ std::cout << sizeof(vecs.value_type) <<

第5课 引用的本质分析

百般思念 提交于 2019-12-18 00:35:03
引用作为变量别名而存在,因此在一些场合可以代替指针 引用相对于指针来说具有更好的可读性和实用性 swap函数的实现对比如下: 注意:   函数中的引用形参不需要进行初始化。 示例程序如下: 形参没有初始化,而是在第15行调用的时候对引用形参进行初始化。 const引用: 当使用常量对const引用进行初始化时,C++编译器会为常量值分配空间,并将引用名作为这段空间的别名。 例: 结论:   使用常量对const引用初始化后将生成一个只读变量。普通引用不能使用常量值进行初始化,但是const引用可以。 我们不能直接改变const引用的值,但是可以通过取地址的方式间接的改变这块内存的值。 示例程序: 1 #include <stdio.h> 2 3 void Example() 4 { 5 printf("Example:\n"); 6 7 int a = 4; 8 const int& b = a; 9 int* p = (int*)&b; 10 11 //b = 5; 12 13 *p = 5; 14 15 printf("a = %d\n", a); 16 printf("b = %d\n", b); 17 } 18 19 void Demo() 20 { 21 printf("Demo:\n"); 22 23 const int& c = 1; 24 int* p = (int

[学习笔记]网络流24题

落花浮王杯 提交于 2019-12-17 19:11:03
不按24题的顺序,按我做题的顺序 1、飞行员配对方案问题 建个图,跑遍匈牙利,让飞行员给其对应的飞机连一条边 #include <bits/stdc++.h> using namespace std; const int maxn=10000+10; int n,m,e,head[maxn],to[maxn<<1],nxt[maxn<<1],vis[maxn],tot,ans,match[maxn]; inline void add(int x,int y){ to[++tot]=y; nxt[tot]=head[x]; head[x]=tot; } int dfs(int x){ for(int i=head[x];i;i=nxt[i]){ int y=to[i]; if(!vis[y]){ vis[y]=1; if(!match[y]||dfs(match[y])){ match[y]=x;return 1; } } } return 0; } int main() { scanf("%d%d",&n,&m); int x,y;scanf("%d%d",&x,&y); while(x!=-1&&y!=-1){ if(x<=n&&y<=n+m) add(x,y); scanf("%d%d",&x,&y); } for(int i=1;i<=n;i++){ memset(vis,0

what's the mechanism of sizeof() in C/C++?

*爱你&永不变心* 提交于 2019-12-17 18:41:56
问题 It seems sizeof is not a real function? for example, if you write like this: int i=0; printf("%d\n", sizeof(++i)); printf("%d\n", i); You may get output like: 4 0 And when you dig into the assemble code, you'll find sth like this: movl $4, %esi leaq LC0(%rip), %rdi xorl %eax, %eax call _printf So, the compiler put directly the constant "4" as parameters of printf add call it. Then what does sizeof do? 回答1: You know, there's a reason why there are standard documents (3.8MB PDF); C99, section 6

Why is int typically 32 bit on 64 bit compilers?

﹥>﹥吖頭↗ 提交于 2019-12-17 18:08:41
问题 Why is int typically 32 bit on 64 bit compilers? When I was starting programming, I've been taught int is typically the same width as the underlying architecture. And I agree that this also makes sense, I find it logical for a unspecified width integer to be as wide as the underlying platform (unless we are talking 8 or 16 bit machines, where such a small range for int will be barely applicable). Later on I learned int is typically 32 bit on most 64 bit platforms. So I wonder what is the

What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

痞子三分冷 提交于 2019-12-17 16:44:11
问题 I'm dealing with some code at work that includes an expression of the form -(sizeof(struct foo)) i.e. the negation of a size_t , and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof returns an unsigned integral value of type size_t . I can't find any clear reference for specified behavior when negating an unsigned integer. Is there any, and if so, what is it? Edit: Ok, so there are some good

How do I find the size of a struct? [closed]

浪子不回头ぞ 提交于 2019-12-17 15:55:07
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . struct a { char *c; char b; }; What is sizeof(a)? 回答1: #include <stdio.h> typedef struct { char* c; char b; } a; int main() { printf("sizeof(a) == %d",