sizeof

How to apply sizeof() operator to non-static class member methods?

大城市里の小女人 提交于 2019-12-02 06:48:36
问题 struct MyClass { int foo () { return 0; } }; unsigned int size = sizeof(MyClass::foo); // obviously error Can we apply sizeof() to member methods from outside the class ? Do we need to declare object to get it ? Edit : I know that above code will give error (that's why word 'obviously'). Wanted to know if we can at all apply the sizeof() to a member method. I don't want to describe the use case for that in length. 回答1: You cannot obtain the size of a member-function , but you can obtain the

类的大小2

给你一囗甜甜゛ 提交于 2019-12-02 06:31:06
#include<stdio.h> #include<stdlib.h> #include<iostream> #include<iomanip> using namespace std; class A { public: int m_data1;//12 int m_data2;//16 void func1() {} void func2() {} virtual void vfunc1(){}//8 virtual void vfunc2(){}//8 }; class B : public A { public: int m_data3;//20 + 4(对齐) void func2() {} virtual void vfunc1(){} //16 }; class C : public B { public: int m_data1; int m_data4; void func2() {} virtual void vfunc1(){} }; class D { int a; }; int main() { cout << "sizeof(D):" << sizeof(D) << endl; cout << "sizeof(A):" << sizeof(A) << endl; cout << "sizeof(B):" << sizeof(B) << endl;

sizeof in static const member initialization

拜拜、爱过 提交于 2019-12-02 05:56:26
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? 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.4 [class.static] (emphasis mine) If an unqualified-id (5.1) is used in the definition of a static member

How to apply sizeof() operator to non-static class member methods?

做~自己de王妃 提交于 2019-12-02 05:33:31
struct MyClass { int foo () { return 0; } }; unsigned int size = sizeof(MyClass::foo); // obviously error Can we apply sizeof() to member methods from outside the class ? Do we need to declare object to get it ? Edit : I know that above code will give error (that's why word 'obviously'). Wanted to know if we can at all apply the sizeof() to a member method. I don't want to describe the use case for that in length. You cannot obtain the size of a member-function , but you can obtain the sizeof a pointer-to-member-function : int size = sizeof( &MyClass::foo ); The same goes for non-member

视频帧双缓冲区的两个版本

丶灬走出姿态 提交于 2019-12-02 05:08:32
视频帧双缓冲区的第一个版本,仅缓冲视频帧,CSharedCriticalSection是临界区保护类 class CFrameCache{ public: CFrameCache() : pop_frame_ptr_(nullptr) , push_frame_ptr_(nullptr) , width_(0), hight_(0), channels_(0){ } ~CFrameCache() { if(pop_frame_ptr_) { delete[] pop_frame_ptr_; pop_frame_ptr_ = nullptr; } if(push_frame_ptr_){ delete[] push_frame_ptr_; push_frame_ptr_ = nullptr; } } void push_frame(const char* frame, int width, int hight, int channels){ if(nullptr == frame || width < 0 || hight < 0 || channels < 0) { return ; } CSharedMutex lock(mutex_); if(push_frame_ptr_ == nullptr && pop_frame_ptr_ == nullptr) { width_ =

Struggling to get number of chars in char* [duplicate]

六眼飞鱼酱① 提交于 2019-12-02 04:55:59
This question already has an answer here: How to get the number of characters in a std::string? 12 answers C sizeof char pointer 5 answers char* string = "hello there"; cout << sizeof( string ); // prints 4, which is the size of pointer cout << sizeof( *string ); // prints 1, which is the size of char How do I get the number of characters contained in the string (11)? It's strlen you want for that, not sizeof . The first counts the number of characters up to the terminating NUL while the second gives you the size of the type which, in this case, is a pointer rather than the underlying array of

sizeof argv[1] not working

♀尐吖头ヾ 提交于 2019-12-02 04:35:41
I'm really new to C and all I know is that the error is related to oldname and newname not be initialized #include <stdio.h> int main (int argc, char const *argv[]) { int result; int lengthOne; int lengthTwo; lengthOne = sizeof(argv[0]); lengthTwo= sizeof(argv[1]); char oldname[lengthOne] = argv[0]; char newname[lengthOne] = argv[1]; result = rename(oldname, newname); if (result == 0) { puts "File renamed"; } else { perror "ERROR: Could not rename file"; } return 0; } app.c: In function ‘main’: app.c:11: error: variable-sized object may not be initialized app.c:12: error: variable-sized object

what should strlen() really return in this code?

痞子三分冷 提交于 2019-12-02 04:10:54
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char qq[] = {'a' , 'b' , 'c' , 'd'}; char qqq[] = "abcd"; printf("%d\n" , sizeof qq / sizeof qq[0]); // line A printf("%d\n" , strlen(qq)); // line B printf("%d\n" , sizeof qqq / sizeof qqq[0]); // line C printf("%d\n" , strlen(qqq)); // line D system("pause"); return 0; } Look at the code above. The array qq has four elements in it and the array qqq has five elements in it the last of which is '\0' . So I understand

C++课堂练习二

北战南征 提交于 2019-12-02 03:45:43
1、数据类型及其所占字节数 #include<iostream> #include<string> using namespace std; int main(){ cout << "***************************************" << endl; cout << "int:"<< sizeof(int) << endl; cout << "short int:"<< sizeof(short int) << endl; cout << "long int:"<< sizeof(int) << endl; cout << "unsigned int:"<< sizeof(unsigned int) << endl; cout << "unsigned short int:"<< sizeof(unsigned short int) << endl; cout << "unsigned long int:"<< sizeof(unsigned long int) << endl; cout << "char:"<< sizeof(char) << endl; cout << "unsigned char:"<< sizeof(unsigned char) << endl; cout << "bool:"<< sizeof(bool) << endl;

Extra bytes when declaring a member of a struct as uint32_t

假如想象 提交于 2019-12-02 03:39:24
问题 I have a problem when using the uint32_t type from the stdint.h library. If I run the following code (on Ubuntu linux 11.10 x86_64, g++ version 4.6.1): #include "stdint.h" #include <iostream> using std::cout; typedef struct{ // api identifier uint8_t api_id; uint8_t frame_id; uint32_t dest_addr_64_h; uint32_t dest_addr_64_l; uint16_t dest_addr_16; uint8_t broadcast_radius; uint8_t options; // packet fragmentation uint16_t order_index; uint16_t total_packets; uint8_t rf_data[]; } xbee_tx_a;