By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Function that reproduces itself

P: n/a
Vig
What is a functio nthat produces itself called? I can;t recall for the
life of me...

Thanks
Cheers!
--
Vig
Jul 8 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a

Vig wrote:
What is a functio nthat produces itself called? I can;t recall for the
life of me...

Thanks
Cheers!
--
Vig
function that produces itself? I haven't seen such kind of function.

-- Murali Krishna

Jul 8 '06 #2

P: n/a

Vig wrote:
What is a functio nthat produces itself called? I can;t recall for the
life of me...
Not exactly a C++ question, but ...

If you mean program rather than function then are you thinking of this?
http://en.wikipedia.org/wiki/Quine

Gavin Deane

Jul 8 '06 #3

P: n/a
Vig
Gavin Deane wrote:
Vig wrote:
>What is a functio nthat produces itself called? I can;t recall for the
life of me...

Not exactly a C++ question, but ...

If you mean program rather than function then are you thinking of this?
http://en.wikipedia.org/wiki/Quine

Gavin Deane
YESS!!!

Life saver!

Cheers!
--
Vig
Jul 8 '06 #4

P: n/a
Vig posted:
What is a functio nthat produces itself called? I can;t recall for the
life of me...

Thanks
Cheers!

Asexual?

--

Frederick Gotham
Jul 8 '06 #5

P: n/a

Vig wrote:
Gavin Deane wrote:
Vig wrote:
What is a functio nthat produces itself called? I can;t recall for the
life of me...
Not exactly a C++ question, but ...

If you mean program rather than function then are you thinking of this?
http://en.wikipedia.org/wiki/Quine

Gavin Deane
YESS!!!

Life saver!

Cheers!
--
Vig
Ah.. I thought there is no such kind of thing in the world. Sorry for
that.

There are many helping hands in this group like Gavin Deane.

-- Murali Krishna

Jul 8 '06 #6

P: n/a
"Murali Krishna" <pm*********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
>
Vig wrote:
>What is a functio nthat produces itself called? I can;t recall for the
life of me...

Thanks
Cheers!
--
Vig

function that produces itself? I haven't seen such kind of function.
The question has already been answered, but I had to try out of curiosity.
This program gives a memory violation trying to write to memory on the 2nd
call to FuncP. I was sure it wouldn't work, but had to see what would
happen anyway.

#include <string>
#include <iostream>

int TestFunction()
{
return 2;
}

typedef int (*Func)();

int main()
{
Func FuncP;
FuncP = TestFunction;

int x = FuncP();

std::cout << x << std::endl;

FuncP = (Func)malloc( 1000 );
memcpy( FuncP, TestFunction, 1000 );

x = 4;
x = FuncP(); // Crashes here

free(FuncP);

std::cout << x << std::endl;

std::string wait;
std::cin >wait;

}
Jul 12 '06 #7

P: n/a
Jim Langston wrote:
[..]
The question has already been answered, but I had to try out of
curiosity. This program gives a memory violation trying to write to
memory on the 2nd call to FuncP. I was sure it wouldn't work, but
had to see what would happen anyway.

#include <string>
#include <iostream>

int TestFunction()
{
return 2;
}

typedef int (*Func)();

int main()
{
Func FuncP;
FuncP = TestFunction;

int x = FuncP();

std::cout << x << std::endl;

FuncP = (Func)malloc( 1000 );
So, here 'FuncP' is a pointer to *data*. Even though you cast it to
a pointer to function, it doesn't really point to any function, does it?
memcpy( FuncP, TestFunction, 1000 );
Now, since 'TestFunction' is not a pointer to an object, the behaviour
of that code is undefined. But even if we assume that you're allowed
to read bytes from the memory location behind 'TestFunction', you're
storing those bytes into data memory.
>
x = 4;
x = FuncP(); // Crashes here
And here you're asking to treat the data as if it were *code*.

In modern OSes, you cannot execute data unless you have special
permissions or changed permissions (or properties) of the memory
where you want to create code. Of course it doesn't work.
>
free(FuncP);

std::cout << x << std::endl;

std::string wait;
std::cin >wait;

}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '06 #8

P: n/a
Jim Langston wrote:
The question has already been answered, but I had to try out of curiosity.
This program gives a memory violation trying to write to memory on the 2nd
call to FuncP. I was sure it wouldn't work, but had to see what would
happen anyway.
You need to know how to allocate memory with execute permission in your
operating system (if it allows that), and if the machine code of your
processor is relocatable, or if the compiler has some option to generate
relocatable code. A general portable way does not exists.

--
Salu2
Jul 12 '06 #9

P: n/a

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:EC***************@fe04.lga...
"Murali Krishna" <pm*********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...

Vig wrote:
What is a functio nthat produces itself called? I can;t recall for the
life of me...

Thanks
Cheers!
--
Vig
function that produces itself? I haven't seen such kind of function.

The question has already been answered, but I had to try out of curiosity.
This program gives a memory violation trying to write to memory on the 2nd
call to FuncP. I was sure it wouldn't work, but had to see what would
happen anyway.
How about recursive function?

regards,
Sukumar Raghavan
Jul 13 '06 #10

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:e9**********@news.datemas.de...
Jim Langston wrote:
>[..]
The question has already been answered, but I had to try out of
curiosity. This program gives a memory violation trying to write to
memory on the 2nd call to FuncP. I was sure it wouldn't work, but
had to see what would happen anyway.

#include <string>
#include <iostream>

int TestFunction()
{
return 2;
}

typedef int (*Func)();

int main()
{
Func FuncP;
FuncP = TestFunction;

int x = FuncP();

std::cout << x << std::endl;

FuncP = (Func)malloc( 1000 );

So, here 'FuncP' is a pointer to *data*. Even though you cast it to
a pointer to function, it doesn't really point to any function, does it?
Definately. And I knew this when I did the malloc.
> memcpy( FuncP, TestFunction, 1000 );

Now, since 'TestFunction' is not a pointer to an object, the behaviour
of that code is undefined. But even if we assume that you're allowed
to read bytes from the memory location behind 'TestFunction', you're
storing those bytes into data memory.
I actually expected to get an error here about an illegal memory read.
Although I didn't. I guess my OS allows reading of data in the program
segment.
> x = 4;
x = FuncP(); // Crashes here

And here you're asking to treat the data as if it were *code*.

In modern OSes, you cannot execute data unless you have special
permissions or changed permissions (or properties) of the memory
where you want to create code. Of course it doesn't work.
Right, and I didn't think it would. I didn't expect to get a write error,
however, but had expected to get a read or execute error. I really can't
think of what was trying to be written during the execution. Perhaps it
actually was an execution error but they didn't catch that specific error as
such and just explained it as a write error.
> free(FuncP);

std::cout << x << std::endl;

std::string wait;
std::cin >wait;
This actually made me start to think a little bit about what would be
required to create such a function. I think I would have to get down into
the OS level. I think there would be many, may problems with this however,
such as the jmp statements which would get their jump points from a data
program segment which would have to be changed.

I think it would not be trivial and would wind up being more like creating
an OS itself then a simple program.
Jul 13 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.