pointers

Does casting pointers to integers define a total order on pointers?

旧时模样 提交于 2021-02-07 18:42:28
问题 (related to my previous question) In QT, the QMap documentation says: The key type of a QMap must provide operator<() specifying a total order . However, in qmap.h , they seem to use something similar to std::less to compare pointers: /* QMap uses qMapLessThanKey() to compare keys. The default implementation uses operator<(). For pointer types, qMapLessThanKey() casts the pointers to integers before it compares them, because operator<() is undefined on pointers that come from different memory

casting member function pointer

感情迁移 提交于 2021-02-07 14:48:36
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

casting member function pointer

蓝咒 提交于 2021-02-07 14:47:31
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

Why does C++ show characters when we print the pointer to a character type? [duplicate]

一个人想着一个人 提交于 2021-02-07 13:28:37
问题 This question already has answers here : Why does cout print char arrays differently from other arrays? (4 answers) Closed 6 years ago . Consider the following code: char char_a = 'A'; int int_b = 34; char* p_a = &char_a; int* p_b = &int_b; cout<<"Value of int_b is (*p_b) :"<< *p_b<<endl; cout<<"Value of &int_b is (p_b) :"<< p_b<<endl; cout<<"Value of char_a is (*p_a) :"<< *p_a<<endl; cout<<"Value of &char_a is (p_a) :"<< p_a<<endl; When I run it, output is: So why doesn't it show the address

Why does C++ show characters when we print the pointer to a character type? [duplicate]

混江龙づ霸主 提交于 2021-02-07 13:25:06
问题 This question already has answers here : Why does cout print char arrays differently from other arrays? (4 answers) Closed 6 years ago . Consider the following code: char char_a = 'A'; int int_b = 34; char* p_a = &char_a; int* p_b = &int_b; cout<<"Value of int_b is (*p_b) :"<< *p_b<<endl; cout<<"Value of &int_b is (p_b) :"<< p_b<<endl; cout<<"Value of char_a is (*p_a) :"<< *p_a<<endl; cout<<"Value of &char_a is (p_a) :"<< p_a<<endl; When I run it, output is: So why doesn't it show the address

Should I always use size_t when indexing arrays?

安稳与你 提交于 2021-02-07 11:24:35
问题 Do I need to use size_t always when indexing an array even if the array is not big enough to exceed the size of an int? It's not a question about when I should use size_t . I just want to know if, for example, a program having 2GB of available memory (all of these fields can be indexed by an int32) but with this memory being (virtual memory) assigned to the "fields" 14GB - 16GB of the computer's RAM. Would it always fail when indexing the memory if I used an int32 instead of a size_t (or an

Recreate the strstr() function

风流意气都作罢 提交于 2021-02-07 09:38:47
问题 Hello i am trying to make my own strstr() function and i can't figure out why it is returning a segmentation fault.I am trying to search a string within another string and then return a pointer to the first 'same' letter. Any help would be appreciated. This is my code: char* ms_search(char *Str1,char* Str2){ char* p = NULL; int i,k=0,j = 0; for(i = 0;i < ms_length(Str1); i++){ if(Str1[i] == Str2[k]){ if(k == 0){ p = &Str1[i]; j= i; } if(k == ms_length(Str2)){ break; } k++; } else{ if(Str1[i]

C++ copies of objects with abstract class pointers

删除回忆录丶 提交于 2021-02-07 07:59:34
问题 Consider the Container class, which basically stores a vector of unique_ptr s of Box objects and can perform some computations on them. class Container { private: std::vector<std::unique_ptr<Box> > boxes_; public: Container(std::vector<std::unique_ptr<Box> > &&boxes): boxes_(std::move(boxes)){} double TotalVolume() { /* Iterate over this->boxes_ and sum */ } }; Here, Box is an abstract class that has a pure virtual method such as double Box::Volume() . Now, suppose that I instantiate a

C++ copies of objects with abstract class pointers

你说的曾经没有我的故事 提交于 2021-02-07 07:55:10
问题 Consider the Container class, which basically stores a vector of unique_ptr s of Box objects and can perform some computations on them. class Container { private: std::vector<std::unique_ptr<Box> > boxes_; public: Container(std::vector<std::unique_ptr<Box> > &&boxes): boxes_(std::move(boxes)){} double TotalVolume() { /* Iterate over this->boxes_ and sum */ } }; Here, Box is an abstract class that has a pure virtual method such as double Box::Volume() . Now, suppose that I instantiate a

glibc detected malloc(): memory corruption in C

瘦欲@ 提交于 2021-02-07 06:48:18
问题 I am trying to compile and code written in C under linux, and got this error message: glibc detected malloc(): memory corruption and I cannot find out why... the substring() just return you part of the original string by giving the starting index and length. e.g. substring("this is example",0,4) = "this"; char *substring(char* str, int start, int length) { char *newString = (char *)malloc(length * sizeof(char)); int i, x = 0; int end=start+length-1; for(i = start ; i <= end; i++){ newString[x