473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Function that reproduces itself

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
10 1367

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

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
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
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

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
"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
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daniel Fudge | last post by:
I can't send a string from a dymanic array to a function. At the getpts function declarationon (line 14), I get the following error. "main.cpp(14) : error C2664: 'void __thiscall...
3
by: Marco Jez | last post by:
The following code reproduces a problem I'm having with the GCC compiler. It compiles fine on MSVC, so I'm wondering whether it is legal C++ code or not. template<typename C> struct Provider {...
10
by: Robert Rotstein | last post by:
Following is a C program, taken from http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C, which has the curious property that, when executed, it produces its own source code as output. ...
4
by: Adriano Coser | last post by:
I'm getting the following error: error C3767: PictureSource - candidate function(s) not accessible when accessing a public member function of a managed class, either from managed or unmanaged...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
11
by: Marco Wedekind | last post by:
Hello all, I have a strange compiler behaviour with this code: ---- Begin of code snippet ---- class Base { public: static unsigned int ClassId();
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
4
by: David | last post by:
Hi all, something wrong with my code, it seems that the destructor function has been called twice. I don't know why.here is my codes Fixture.h #ifndef _FIXTURE_H #define _FIXTURE_H #include...
6
by: hadad.yaniv | last post by:
Hello, i am new to c++, i hav a vector of typed object: vector<Man*People; When i do a second pushback, even for the same object the program crash say: "An Access Violation (Segmentation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.