|
Ah, you're right on both accounts.
Not that it would make much sense to have an incorrectly aligned pointer, but then the challenge didn't make much sense for practical use anyway...
|
|
|
|
|
The following code compiles and it does return the correct values:
int* f1(int i) { return 0; }
int* f2(int* pi) { return pi; }
union UPointers {
int*(*ag1[2])(int i);
struct SPointers {
int*(*g1)(int i);
int*(*g2)(int* pi);
} my_spointers;
} my_pointers;
int* foo(int i) {
int* (*h1)(int i);
my_pointers.my_spointers.g1 = &f1;
my_pointers.my_spointers.g2 = &f2;
h1 = my_pointers.ag1[1];
return h1(i);
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 12; i < 15; ++i) {
int* pi = foo(i);
std::cout << i << ' ' << pi << std::endl;
}
return 0;
}
The output is:
12 0000000C
13 0000000D
14 0000000E
The idea is to abuse a function pointer that is supposed to pass an int argument by assigning it to a function that expects an int* . I'm almost sure there is no implicit conversion involved, but I'm not too familiar with function pointers to be sure.
The 'conversion' happens when the function call writes the int argument to the stack, but the function that is called interprets it as a int*.
|
|
|
|
|
Dear Stefan,
I do indeed think (if I am not mistaken) that your code gets the job done without performing any casts.
I also enjoyed seeing you use a union for the job.
The solution I had in mind also employs unions, but does not use function pointers:
int* withoutCasts(int i)
{
union {int j; int* pint;} converter;
converter.j = i;
return converter.pint;
}
Thanks to everyone who spent time thinking on this, and if anyone has a different solution (possibly one without unions) then I'd love to hear it.
|
|
|
|
|
Originally my idea was to use a struct of function pointers and 'iterate' over them to arrive at a different pointer. However, I found that pointer arithmetic apparently doesn't work on function pointers. That is when I thought of union. It was only later that I realized the union construct can solve the task by itself. But since I already posted my solution, there was no point in 'watering it down'.
Another thing I thought of is overloading virtual functions with different return types. But it would require at least a downcast of the instance pointer:
class base {
public:
virtual ~base() {}
virtual int* foo(int i) { return &i; }
};
class derived : public base {
public:
virtual ~derived() {}
virtual int foo(int i) { return i; } };
int* bar(int i) {
base* caster = new derived; int* pi = caster->foo(i); delete caster;
return pi:
}
I wonder if I could get this to work if I put part of the code inside a constructor, before the construction of the vtable - but then the behaviour would be undefined
|
|
|
|
|
how can use tinyxml to mfc dialogbased application,i mean which files i have to add to my project to use tinyxml
|
|
|
|
|
|
Straight from the TinyXML website:
Quote: To Use in an Application:
Add tinyxml.cpp, tinyxml.h, tinyxmlerror.cpp, tinyxmlparser.cpp, tinystr.cpp, and tinystr.h to your project or make file. That's it! It should compile on any reasonably compliant C++ system. You do not need to enable exceptions or RTTI for TinyXML.
|
|
|
|
|
Nothing,Just an Edit
modified 8-Mar-13 8:04am.
|
|
|
|
|
This is the C/C++ forum. You might ask this in the PHP forum[^] in the Web Development section.
But you should be more spefic about the problem.
|
|
|
|
|
hi
in my property sheet i have to put install button instead of Next button and add event to that, how can i put that.
|
|
|
|
|
|
venkatesh52867 wrote: ...Next button...
Are you using a "wizard" dialog? If not, what is the Next button for? I have an example of hiding/adding buttons to property sheets here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Need JSON parser, please help
|
|
|
|
|
Did you Google[^] for, yet?
Veni, vidi, vici.
|
|
|
|
|
What exactly is your C/C++/MFC question?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
HELLO
I am pretty new in c and now trying to use gmp.
Am trying to decrypt a file using gmp on rsa encryption.how do i go abt the primes, p and q. i am getting a wrong output on the file
please i need help asap
thanks
|
|
|
|
|
etesi wrote: now trying to use gmp Which is?
etesi wrote: i am getting a wrong output on the file Well unless we can see what is the difference between that and the correct output, and some of the code that is causing the error, then it is unlikely that we can offer much to help you.
Use the best guess
|
|
|
|
|
I think the OP means this[^].
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|
|
Why would you want to concern yourself with the primes p & q? By p & q I assume you mean the primes used for generating the keys. If you already have an encrypted file that should mean you already have the key pair generated, if not, how did you encrypt the file in the first place?
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|
|
As Richard said, we need a lot more information than this if you want us to help you. If you're getting wrong results, then you're doing it wrong. At the very least, we need to know what you did, otherwise there's no telling what is your problem.
Regarding the primes, you shouldn't be needing them once the private and public keys are generated. Encryption and decryption only need those keys, not the primes they are generated from. As a matter of fact, breaking the code requires finding these primes, so knowing them at the time of de- or encryption would entirely miss the point!
See RSA Algorithm (Wiki) for details.
|
|
|
|
|
Hello!
I'm working on an application similar to MS Word, but on a much smaller scale.
My problem is that I do not know how to open a file just like Word does.
Word does something like this: if you are already in Word and you opened a file already and then you open another one from the file menu, than a new application appears with this file opened, just like the first file. But this application does not open a new WINWORD.EXE app. No matter how many files you open there's still just one WINWORD.EXE.
So, I want to do exactly like this in my application, but I don't know how!
If Word does not open a new app with the opening of a new file, this means that somehow the new interface opened with the file is linked with the initial dlls or with the files that have been loaded in memory.
I don't have a clue how to do that.
I'm working in C++, Visual Studio 2005, MFC.
Anybody has any idea?
Thanks!
|
|
|
|
|
dliviu wrote: So, I want to do exactly like this in my application, but I don't know how! You want to look into SDI.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
If you're using MFC, you can find an option in the project wizard called Multiple top-level documents under Application type .
If not, what you need to do is to create a new UI thread each time a new file is opened.
|
|
|
|
|
I'm using MFC. Thanks for the suggestion.
|
|
|
|