casting

Proper way of casting pointer types

你说的曾经没有我的故事 提交于 2019-12-17 17:39:09
问题 Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just

Executing machine code in memory

走远了吗. 提交于 2019-12-17 17:33:49
问题 I'm trying to figure out how to execute machine code stored in memory. I have the following code: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); unsigned int len = ftell(f); fseek(f, 0, SEEK_SET); char* bin = (char*)malloc(len); fread(bin, 1, len, f); fclose(f); return ((int (*)(int, char *)) bin)(argc-1, argv[1]); } The code above compiles fine in GCC, but when I try and execute the program from the command

reinterpret_cast in C#

帅比萌擦擦* 提交于 2019-12-17 16:50:33
问题 I'm looking for a way to reinterpret an array of type byte[] as a different type, say short[]. In C++ this would be achieved by a simple cast but in C# I haven't found a way to achieve this without resorting to duplicating the entire buffer. Any ideas? 回答1: You can achieve this but this is a relatively bad idea. Raw memory access like this is not type-safe and can only be done under a full trust security environment. You should never do this in a properly designed managed application. If your

How to tweak LISTAGG to support more than 4000 character in select query?

丶灬走出姿态 提交于 2019-12-17 16:41:36
问题 Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production. I have a table in the below format. Name Department Johny Dep1 Jacky Dep2 Ramu Dep1 I need an output in the below format. Dep1 - Johny,Ramu Dep2 - Jacky I have tried the 'LISTAGG' function, but there is a hard limit of 4000 characters. Since my db table is huge, this cannot be used in the app. The other option is to use the SELECT CAST(COLLECT(Name) But my framework allows me to execute only select queries and no PL

Difference between &(*similarObject) and similarObject? Are they not same?

痞子三分冷 提交于 2019-12-17 16:27:00
问题 Can someone please explain this to me dynamic_cast<SomeObject *>( &(*similarObject) ); What is the point of doing the address of a dereferenced pointer? Wouldn’t the pointer itself just be the address of it? 回答1: It may be that the type of similarObject has overloaded operator* and so it returns something whose address you're passing to dynamic_cast . &(*x) and x may not be always the same thing. For example, think of iterator: std::map<int, int>::iterator it = v.begin(); Then it and &(*it)

C# casting an inherited Generic interface

有些话、适合烂在心里 提交于 2019-12-17 16:19:07
问题 I'm having some trouble getting my head around casting an interface I've come up with. It's an MVP design for C# Windows Forms. I have an IView class which I implement on my form classes. There's also an IPresenter which I derive into various specific Presenters. Each Presenter will manage the IView differently depending on the role, for example opening the dialog to enter a new set of data with an AddPresenter as opposed to editing existing data with an EditPresenter which would preload data

C# casting an inherited Generic interface

◇◆丶佛笑我妖孽 提交于 2019-12-17 16:18:16
问题 I'm having some trouble getting my head around casting an interface I've come up with. It's an MVP design for C# Windows Forms. I have an IView class which I implement on my form classes. There's also an IPresenter which I derive into various specific Presenters. Each Presenter will manage the IView differently depending on the role, for example opening the dialog to enter a new set of data with an AddPresenter as opposed to editing existing data with an EditPresenter which would preload data

Java if ternary operator and Collections.emptyList()

ε祈祈猫儿з 提交于 2019-12-17 16:13:32
问题 Could you please explain why with the first return type the code can't be compiled? The message is : Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<String> . Is there inserted an explicit cast in the second case ? public class GenericsTest { private String getString() { return null; } public List<String> method() { String someVariable = getString(); //first return type //return someVariable == null ? Collections.emptyList() : Collections.singletonList

What is the priority of casting in java?

自作多情 提交于 2019-12-17 15:56:38
问题 if I have a line of code that goes something like int s = (double) t/2 Is it the same as int s = (double) (t/2) or int s = ((double) t)/2 ? 回答1: This should make things a bit clearer. Simply put, a cast takes precedence over a division operation, so it would be the same thing as give the same output as int s = ((double)t) / 2; Edit: As knoight has pointed out, this is not technically the same operation as it would be without the parentheses, since they have a priority as well. However, for

How does long to int cast work in Java?

痞子三分冷 提交于 2019-12-17 15:53:45
问题 This question is not about how a long should be correctly cast to an int, but rather what happens when we incorrectly cast it to an int. So consider this code - @Test public void longTest() { long longNumber = Long.MAX_VALUE; int intNumber = (int)longNumber; // potentially unsafe cast. System.out.println("longNumber = "+longNumber); System.out.println("intNumber = "+intNumber); } This gives the output - longNumber = 9223372036854775807 intNumber = -1 Now suppose I make the following change-