pointers

What exactly is a reference in C#

人盡茶涼 提交于 2020-12-11 04:24:14
问题 From what I understand by now, I can say that a reference in C# is a kind of pointer to an object which has reference count and knows about the type compatibility. My question is not about how a value type is different than a reference type, but more about how a reference is implemented. I have read this post about what differences are between references and pointers, but that does not cover that much about what a reference is but it it's describing more it's properties compared with a

Is pointer just an integer?

落爺英雄遲暮 提交于 2020-12-09 05:09:32
问题 If I know the address of an data object, could I store the address as an integer and operate the integer as a pointer? For example, void main(){ long a = 101010; long *p = &a; long b = p; printf("%lld\n", *(long*)b); } Is it always safe? Comments: long b = p; produces a warning: Initialization makes integer from pointer without a cast However, the program prints 101010 . 回答1: It's not guaranteed by the standard that such cast would always work. To store a pointer in an integral type, use

printing a char pointer … what happens?

别来无恙 提交于 2020-12-05 10:56:31
问题 i'm new to C and i've got a question about char pointers and what it will print . take a look : int main() { char *p1="ABCD"; p1="EFG"; printf ("%s",p1); return 0; } it will print EFG and now : int main() { char *p1="ABCD"; //p1="EFG"; printf ("%s",p1); return 0; } and it will give you ABCD The point that I don't get is what exactly *p1 is ? Is it a number of an address that contains a char value ? Is it a char ? what is in *p1 right now ? Why is it const ? 回答1: From the C Standard: http:/

How to pass pointer to function and dynamically allocate memory within function C++

时光怂恿深爱的人放手 提交于 2020-12-05 07:23:53
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

How to pass pointer to function and dynamically allocate memory within function C++

£可爱£侵袭症+ 提交于 2020-12-05 07:22:20
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

Can a C compiler generate an executable 64-bits where pointers are 32-bits?

谁说我不能喝 提交于 2020-12-01 09:02:18
问题 Most programs fits well on <4GB address space but needs to use new features just available on x64 architecture. Are there compilers/platforms where I can use x64 registers and specific instructions but preserving 32-bits pointers to save memory? Is it possible do that transparently on legacy code? What switch to do that? OR What changes on code is it necessary to get 64-bits features while keep 32-bits pointers? 回答1: A simple way to circumvent this is if you'd have only few types for your

Can a C compiler generate an executable 64-bits where pointers are 32-bits?

别说谁变了你拦得住时间么 提交于 2020-12-01 09:02:09
问题 Most programs fits well on <4GB address space but needs to use new features just available on x64 architecture. Are there compilers/platforms where I can use x64 registers and specific instructions but preserving 32-bits pointers to save memory? Is it possible do that transparently on legacy code? What switch to do that? OR What changes on code is it necessary to get 64-bits features while keep 32-bits pointers? 回答1: A simple way to circumvent this is if you'd have only few types for your

Does it make sense to combine optional with reference_wrapper?

雨燕双飞 提交于 2020-12-01 01:33:48
问题 It occurred to me that in C++ it is possible to use the type std::optional<std::reference_wrapper<T>> . An object of this type is essentially a reference to an object of type T or a null value, i.e., pretty much a pointer. My questions: Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T* ? Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T* ? 回答1: Is there any

Does it make sense to combine optional with reference_wrapper?

拥有回忆 提交于 2020-12-01 01:29:05
问题 It occurred to me that in C++ it is possible to use the type std::optional<std::reference_wrapper<T>> . An object of this type is essentially a reference to an object of type T or a null value, i.e., pretty much a pointer. My questions: Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T* ? Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T* ? 回答1: Is there any

Read access violation using m_pRenderTarget with winapi due to non-NULL pointer

只谈情不闲聊 提交于 2020-11-30 01:40:45
问题 I am following the Microsoft Docs for Windows api. I a currently on chapter 4 and trying to draw an ellipse. The m_pRenderTarget is declared in the class App.h. In the function OnRender(HWND hwnd) I am trying to use it to draw the geometry (ellipse). However, I am getting the following error: Exception thrown: read access violation. this-> m_pRenderTarget was 0x38. After some debugging, I noticed the in the HRESULT App::CreateDeviceResources(HWND hwnd) function, m_pRenderTarget was for some