473,802 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1396

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*********@gm ail.comwrote in message
news:11******** **************@ 35g2000cwc.goog legroups.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*******@rock etmail.comwrote in message
news:EC******** *******@fe04.lg a...
"Murali Krishna" <pm*********@gm ail.comwrote in message
news:11******** **************@ 35g2000cwc.goog legroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3099
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 std::basic_ifstream<char,struct std::char_traits<char> >::open(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string< char,struct std::char_traits<char>,class std::allocator<char> >' to
3
1782
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 { template<typename T> static void func() {} };
10
1897
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. #include <stdio.h> char x="#include <stdio.h>%cchar x=%c%s%c;%cint main() {printf(x,10,34,x,34,10,10);return 0;}%c"; int main() {printf(x,10,34,x,34,10,10);return 0;}
4
4524
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 code. The class is an owner draw control implemented into a Control Library. The access is on a DLLs that support CLR with old syntax. The code compiled OK on Beta-1.
20
4964
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 a VIRTUAL function createList(). This createList() function is overloaded in another class named ct_server that inherits gs_object. in my code, it looks something like that :
11
11059
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
3563
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 it into my own user defined structures. Since the process of opening and retrieving data from the database is exactly the same for all struct types, and the only part that's different is how the data is copied into the diff structs, I was...
4
2019
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 <string> #include <map> #include <list>
6
2936
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 fault) raised in your program"
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9561
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10532
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10281
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10058
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3789
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.