Thread: Question: Can a function be nameless?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    CANADA!
    Posts
    3

    Question: Can a function be nameless?

    I've been wondering about something for a while now. You can make nameless variables by allocating memory to pointers, such as
    Code:
    int *ptr = new int;
    from the tutorial. That I understand. What I wonder, however, is if it's possible to make an unnamed function by assigning a block of code to a function pointer. For example, would this work?:

    Code:
    #include <cstdarg>
     
    // this function should be called with only two int variables, the va_list is to access them
    int (*mult)(int, int, ...) = {
    
    va_list arglist; va_start(arglist, 0); // will this allow access to the first variable? return va_arg(arglist, int) * va_arg(arglist, int); }
    I know that wouldn't be very useful, and it wouldn't be good form, but is it at all possible to make a nameless function with something like this?

    If so, I can think of a way to make a more secure (but cumbersome) login feature for some programs: Instead of having the sign-in box accept a password, have it accept a variable assignment as the "password". The sign-in box could be connected to an interpreter, which will read and execute this assignment. Now, if you don't actually include a function that allows the user to log in as part of the program, but instead have the user input a new function into a variable, you could prevent access to only those who know not only what steps your program needs to perform to log in, but also which function pointer to use.


    (Oh, and if you're wondering about my account name, I like to use the same name on every forum, or as close to it as possible.)

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I don't understand your question, really. If a function is nameless, then how could one reference it? Or do you just mean design an interpreter than can convert an input expression to some arbitrary functionality? If so, yes, but then why go to all the trouble, when conventional means are sufficient?

    Or did you mean something else entirely?

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is something called lambda functions. There are available at C++0x, not C++. In other languages, as C# 3.0, you can use them. Functional languages (term?) generally use them.

    I don't know if they provide security

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    CANADA!
    Posts
    3
    Quote Originally Posted by Sebastiani View Post
    I don't understand your question, really. If a function is nameless, then how could one reference it? Or do you just mean design an interpreter than can convert an input expression to some arbitrary functionality? If so, yes, but then why go to all the trouble, when conventional means are sufficient?

    Or did you mean something else entirely?
    What I mean is that the function itself doesn't have a name, just a pointer to it. The only way to use the function is through the function pointer. It's like how if you make a new array with new[] and assign it to a pointer, you'll only be able to access the array through that pointer, since the array itself doesn't have a name. Or how
    Code:
    int *ptr = new int;
    from the tutorial's pointer page works: it makes an int at ptr. This int doesn't actually have a name, so the only way to use it is to go through the pointer. Similarly, I wonder if you can make a function that can only be called through a pointer to it, since the function itself doesn't actually have a name for use in function calls. Also, if it is possible to do so, I'm not sure if using
    Code:
    va_start(arglist, 0);
    will actually allow access to the first variable, or if it'll cause an exception.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The answer is no. The closest thing is lambdas, and they will be available in the upcoming standard of C++, called C++0x.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Lambdas are in C# and if they are like that in C++ I will avoid them entirely.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have never read Wikipedia or VC++ blog for upcoming C++0x features?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    They have C++0x lambdas, as has been mentioned several times. Here's a simple example with lambdas. It's not the best way to do this, but it works.

    Code:
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <list>
    
    using namespace std;
    
    int main()
    {
         list<string> someList;
    
         someList.push_back("Test 1");
         someList.push_back("Test 2");
         someList.push_back("Test 3");
    
         for_each(someList.begin(), someList.end(), 
         [](string element)  //This is the start of the lambda
         { 
              cout << element << endl;  //Outputs Test 1, Test 2, Test 3
         });
         return 0;
    }

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It's not portable, but a program could allocate a chunk of memory, write machine language instructions into it, and call it through a function pointer. This is called dynamic code generation.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    I still haven't decided if they are putting lambda's in C++ for a real reason, or if they are just trying to make it more like C#. I suspect the later.

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by abachler View Post
    I still haven't decided if they are putting lambda's in C++ for a real reason.
    Brevity. Lambda code is shorter and more easily understood. There's a couple articles on C++0x that mention this. It should help transitioning and learning C++. It's not really necessary, but maybe in 10 years with the addition of other features we'll run into some benefits of no other language lambdas. In my testing it's numerous times faster than std::bind (if you *have* to recreate the std::function object every time), which I think makes sense, but maybe I'm not optimizing correctly. I just wish they would fix the dependent types bug.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Doesn't lambda functions also help passing a function in another function. Like instead of creating a compare function for qsort() just write it when calling it. If the compare function is really small (which can be) you can avoid one function.

    Like
    Code:
    qsort(a, b, c, [&](void* x, void* y) {int *xx= x, *yy= y; return *xx > *yy});
    Nevermind the 100% correctness of the syntax but you get the point

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, this is sort of why lambdas are so good. Otherwise you have to create a functor somewhere outside your function, create an object to it and pass it, or make a normal function and suffer overhead when you pass it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Alexei Alexandrescu called lambdas the biggest missed opportunity in C++03. The reason is that the STL works best with a very functional programming style (you keep passing functors to the generic algorithms), and having no lambdas in such an environment leads to lots of superfluous code, which is not even in the place where it's required.

    Back to the OP's question, lambdas for security is an idea that ignores the reality of code. It sounds good when you look at the source code, but once you realize how such code is translated to machine code, it is less attractive.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Sep 2009
    Location
    CANADA!
    Posts
    3
    First, I have to clarify: I probably could have worded it better, but I was talking about assigning a block of C++ code to a function pointer to dynamically make a function that didn't actually have a name. I haven't found much info on lambda functions (but then again, I admittedly haven't checked the VC++ log, and I don't exactly trust Wikipedia), though, so I had to ask. And in regards to brewbuck's post, does it have to be machine code, or would it be possible to make an interpreter that can change C++ code into machine code dynamically, so you can use it as something of a more secure password? (After all, if you were to make a program where the log-in function had to be manually inputted for a person to log in, it'd be pretty hard for someone to crack that 'password', I'd imagine. All you'd need to do is make a few functions that check whether the inputted line is code, whether it's malicious, and whether it's in the right format (specifically, if it's in the form
    Code:
    pointer = {code};
    (or would that be
    Code:
    pointer = &{code};
    ?), and then run it when you're sure. For added security, you could design the program so that the actual variable(s) needed to store log-in information changed each day, such as by using a class to use the information, and making a new instance of the class each day (you could either design the program to email you with the new instance's name or use a set name for each day, such as login_Mon, login_Tues, etc.).)

    I also thought of another use for it: debugging. You know how some IDEs allow you to change the value in variables on the fly? Well, if they allowed you to input new code and store its address in a function pointer, you could quickly try new functions during the testing phase without having to stop interpretation and rewrite the program, speeding up the process.

    Well, thanks for the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM