is it possible to pass arguments to the clone fn function?

This is a discussion on is it possible to pass arguments to the clone fn function? within the Linux Programming forums, part of the Platform Specific Boards category; hello! I've a problem with the clone.. Its definition is as follows: Code: int clone(int (*fn)(void *), void *child_stack, int ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    is it possible to pass arguments to the clone fn function?

    hello!
    I've a problem with the clone.. Its definition is as follows:

    Code:
     int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, .../* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
    if I define a simple
    Code:
     int function(void *unused)
    everything's ok: i can call for example
    Code:
    clone ((int (*)(void *)function,&stack[1024],....
    or also just
    Code:
    clone (function,&stack....
    the problem is if i want to cast the void* to, e.g. an int*
    i tried to do things like
    Code:
    int function (int *nPtr){...}
    int a;
    clone((int(*)(int *)function(&a),....
    clone (function ((int *)&a), ...
    but nothing works... where is the mistake? thank for every answer!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have a 4th argument to clone called "void *arg". This is passed unchanged along to the function you specify. Which by the way should be specified like this:
    Code:
    clone (function,&stack, ...
    You should NEVER use casts [unless you REALLY ABSOLUTELY know what you are doing] on function pointer arguments.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You don't do that. function must keep its prototype. If it wants to cast, you cast inside of function.
    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

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    The first argument expects a signature of "int (*fn)(void *)" - that's exactly what you should give it. Any parameter interpretations should be done within the function:
    Code:
    int foo(void *p)
    {
        int param = *((int*)p); // address of int passed
        // or 
        int param = (int)p; // int passed, cast as void*
    }
    // edit - of course I type this up and there's two replies already

    gg

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    thank you guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 10
    Last Post: 09-27-2005, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21