Thread: Memory Allocation

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    Memory Allocation

    Okay, I'm not sure exactley what this would be called, but I suppose I could say I'm trying to allocate memory for a function.... heres an example:

    Code:
    struct command_type
    {
    	void	(*funp)(CHAR_DATA *ch, char *args);  //function
    };
    
    struct command_type cmd;
    
    ....
    
    char *com="walk_north";  //name of function to call
    cmd.function=com;
    ((cmd.funp))(ch, args);  //called the function

    Okay, heres my problem. I need cmd.funp to equal the value of the character array 'com'. But they are not the same data types. So how would I allocate the memory of cmd.funp, and then assign the com character string to cmd.funp? Am I making sense?

    Remember that if I change the value of com, then funp changes as well, due to using pointers. so I need cmd.funp to not point to com, but be the same value. Thus giving it a function name. -smashes head on moniter-

    Thanks for any help, hope I explained it well enough.

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I don't know what you're trying to do, but it doesn't sound like a good idea at all.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    yeah it is

    Actually, its a very good idea. I'm writing a MUD game and when a player sends a command to the game server, I compare the command sent to the existing function names that I give it, ifit compares currectley then it calls that function. Sorta like a dynamic function call.
    [Strut]

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You are a confused man

    Look, you can technically cast the char array into a function pointer, but this will not do anything (but possiblly crash your computer!)

    Having said that...enjoy.


    Code:
    struct command_type
    {
     void (*function)(char *, char *);  //function
    };
    
    
    int main(){
    struct command_type cmd;
    char *com = "walk_north";
    char *crash, *me;
    cmd.function = (void(*)(char*, char*))com;
    cmd.function(crash, me);
    
    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Perhaps a
    Code:
     
    std::map<string , void(*)(char*,char*)>
    is what you want?
    Read some STL tutorial.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Actually, its a very good idea


    Ok, look. what you're referring to is a function table:

    void North(){}
    void South(){}

    char *cmd[] = { { "north" }, {"south"} };

    void (*fp[2])(void) = { {North}, {South} };


    //...


    if((strcmp(cmd[i], input))==0)
    fp[i]();
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Here's an elabortion of Sang- drax's suggestion....You need a decent stl aware compiler....Codewarrior.....Mingw (IE with devc++)...it will choke to death on VC++6

    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    void Func1(void);
    void Func2(void);
    void Func3(void);
    typedef void(*funp)(void);//this is a nead typedef for our func pointer
    
    
    
    int main(){
    
    	typedef std::map<std::string,funp> MYMAP;//another typedef to save typing
    	MYMAP MyMap;//We create a map object
    
    	//Now we add each func name as a string and a pointer to its function
    	MyMap.insert(MYMAP::value_type(std::string("Func1"),Func1));
    	MyMap.insert(MYMAP::value_type(std::string("Func2"),Func2));
    	MyMap.insert(MYMAP::value_type(std::string("Func3"),Func3));
    	
    	std::string str;
    	
    	while(1){
    		
    		std::cout << "Enter Function Name" << std::endl;
    		std::cout << "Func1, Func2 or Func3..or exit to quit" << std::endl;
    		std::getline(std::cin,str);
    		std::cout << std::endl;
    		if(!str.compare("exit"))break;
    		if(MyMap.find(str) == MyMap.end()){//If not in map..try again!
    			std::cout << "Invalid choice" << std::endl;
    			continue;
    		}		
    		(MyMap[str])();//Get the function ptr and call it
    		continue;
    	}
    	
    	std::cout << "That's all folks!!!" << std::endl;
    
    }
    
    void Func1(void){
    	std::cout << "Congrats!!..You called Func1" << std::endl << std::endl;
    }
    void Func2(void){
    	std::cout << "Congrats!!..You called Func2" << std::endl << std::endl;
    }
    void Func3(void){
    	std::cout << "Congrats!!..You called Func3" << std::endl << std::endl;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    er...

    Well things seem to be a little... out of proportion, you could say. Let me elaborate on the code. Please note I had a previous working version that someone wrote for me, but I changed my command system in the MUD game, so Its been re-written.

    Code:
    struct command_type
    {
    	void	(*funp)(CHAR_DATA *ch, char *args);
    };
    
    ....
    
    const struct command_type player_commands[] = {
    	{ c_bug	},
    
    	{ c_editcmd },
    
    	{ c_halt	},
    
    	{ c_idea	},
    
    	{ c_logview },
    
    	{ c_makecmd },
    
    	{ c_motd	},
    
    	{ c_password },
    
    	{ c_quit	},
    
    	{ c_reset_char },
    
    	{ c_save	},
    
    	{ c_savecmds },
    
    	{ c_say	},
    
    	{ c_show_flags	},
    
    	{ c_toggle_flag },
    
    	{ c_typo	},
    
    	{ c_who	},
    
    	{ NULL	}
    };
    
    void command_interp(CHAR_DATA *ch, char *input)
    {
    	char *command;
    	char *args;
    	COMMAND_DATA *cmd;
    	int i;
    	bool bad_c=FALSE;
    	// Note: GNU claims strtok is very bad, the security concerned could
    	// change this to something else.
    	command = strtok(input, " ");
    	args = strtok(NULL, "");
    
    	if((input[0]==' ') || (input==NULL))
    		bad_c=TRUE;
    /*
    	for (i=0; player_commands[i].name[0] != '\0'; i++) {
    		if(bad_c==TRUE)
    			break;
    		if (!strcmpi(command, player_commands[i].name)) {
    			((player_commands[i].funp))(ch, args);
    			return;
    		}
    	} */
    
    	for(cmd=first_command;cmd!=NULL;cmd=cmd->next){
    		if(bad_c==TRUE)
    			break;
    		if(!strcmpi(command, cmd->name)) {
    				((cmd->function))(ch, args);
    				return;
    		}
    	}
    
    	bad_command(ch);
    }

    Well, perhaps that will clear up exactley what I'm trying to do? Oh, and one more thing. I'm using MSVC++ 6.0 IDE to edit the code, but its being written to run under linux, or *unix environments. I'm using cygwin to compile under windows.

    Thanks again.
    [Strut]

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM