|
Mem allocation with function pointers?
I just posted this, but it didnt appear, so I'll try again...
This problem has really been bugging me.
Im making a scripting engine, and to do it I need what I guess you could call a function binding interface, something I can use to call functions by name. When I try to bind more than 1 function, the thing crashes, and I assume its a memmory problem.
Heres the code:
More specificly, one of those "performed an illegal opperation" windows pops up. The details: "SCRIPTING caused an invalid page fault in
module SCRIPTING.EXE at 0167:00404cb0."
I didnt think I had to use malloc there, but I thought it might fix it. It obviously didnt, or I wouldnt be posting.
Thanks.
Edited by - kranomano on April 14, 2001 9:04:08 PM
That should be "new char[strlen(...)+1]". Otherwise the NULL has no where to go
.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
![](smile.gif)
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
Also, you what''s the "Funcs = new func_t;" all about? You should allocate the entire array of func_t''s at some point, or make a linked list out of them.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
And another thing, you don''t need to allocate space for a function pointer, it just points to where the function is already in memory.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
April 14, 2001 08:43 PM
Well, I think I see one problem right here...
Funcs[nFuncs-1].cmd = new char[strlen(str)];
strcpy(Funcs[nFuncs-1].cmd, str);
In C/C++ strings are terminated with a zero. The strlen function returns the length of the string up to, but not including, the terminating zero. In otherwords, you''re allocating one less character than you actually need. When strcpy tries to copy over the terminator, you get the crash. Try this instead:
Funcs[nFuncs-1].cmd = new char[strlen(str)+1];
strcpy(Funcs[nFuncs-1].cmd, str);
Also, I don''t think you need to allocate memory for your function pointer. You should be able to ditch this line:
Funcs[nFuncs-1].fPtr = (void (*)(void)) malloc(sizeof(func));
- Matt
Funcs[nFuncs-1].cmd = new char[strlen(str)];
strcpy(Funcs[nFuncs-1].cmd, str);
In C/C++ strings are terminated with a zero. The strlen function returns the length of the string up to, but not including, the terminating zero. In otherwords, you''re allocating one less character than you actually need. When strcpy tries to copy over the terminator, you get the crash. Try this instead:
Funcs[nFuncs-1].cmd = new char[strlen(str)+1];
strcpy(Funcs[nFuncs-1].cmd, str);
Also, I don''t think you need to allocate memory for your function pointer. You should be able to ditch this line:
Funcs[nFuncs-1].fPtr = (void (*)(void)) malloc(sizeof(func));
- Matt
Your instinct should tell you that something''s wrong when you have two lines that assign different values to the same thing:
A function isn''t like a variable; you can''t allocate memory for a function in the heap and then assign the "data" of the function to that memory. Functions exist in the executable, and are loaded by the operating system into a different logical part of memory. Therefore, malloc is unnecessary and inappropriate. Those lines aren''t the source of your problem--only the second line should be necessary.
Your problem is in the way you''re trying to implement a dynamic array:
Funcs points to a single func_t, not an array. That''s the reason why it works for the first one only--at that point, nFuncs-1 = 0 so Funcs[0] will just return the pointer you just allocated. However, once you get more than one, nFuncs-1 will point to memory you have not allocated. Furthermore, as you add more functions and overwrite Funcs, you lose the prior pointers meaning you''ll never be able to deallocate that memory--i.e. you''re now leaking.
Your choices are:
1) Implement a real dynamic array the correct way.
2) Use STL''s vector to implement a dynamic array; you should be able to do this without having to change func_t since it''s currently a shallow structure (two pointer)
3) Since you''ll probably never search for the Nth function anyway, and are more likely so search for functions by name, it would be more appropriate to use an STL map<string, FPTR> where FPTR is your typedef of a pointer to your function type.
Problem is, I don''t know if you''re using C or C++. If you''re C++, I''d pick option 3. If you''re not, let me know and I''ll post routines to make a decent dynamic array--though at that point I''d consider using a linked list instead for obvious reasons (not going to search for Nth function, only going to search by name).
|
A function isn''t like a variable; you can''t allocate memory for a function in the heap and then assign the "data" of the function to that memory. Functions exist in the executable, and are loaded by the operating system into a different logical part of memory. Therefore, malloc is unnecessary and inappropriate. Those lines aren''t the source of your problem--only the second line should be necessary.
Your problem is in the way you''re trying to implement a dynamic array:
|
Funcs points to a single func_t, not an array. That''s the reason why it works for the first one only--at that point, nFuncs-1 = 0 so Funcs[0] will just return the pointer you just allocated. However, once you get more than one, nFuncs-1 will point to memory you have not allocated. Furthermore, as you add more functions and overwrite Funcs, you lose the prior pointers meaning you''ll never be able to deallocate that memory--i.e. you''re now leaking.
Your choices are:
1) Implement a real dynamic array the correct way.
2) Use STL''s vector to implement a dynamic array; you should be able to do this without having to change func_t since it''s currently a shallow structure (two pointer)
3) Since you''ll probably never search for the Nth function anyway, and are more likely so search for functions by name, it would be more appropriate to use an STL map<string, FPTR> where FPTR is your typedef of a pointer to your function type.
Problem is, I don''t know if you''re using C or C++. If you''re C++, I''d pick option 3. If you''re not, let me know and I''ll post routines to make a decent dynamic array--though at that point I''d consider using a linked list instead for obvious reasons (not going to search for Nth function, only going to search by name).
Alright, thanks guys. I knew it couldnt be that simple. Heh.
I guess I should really learn how to use STL...
So Stoffel, how would _you_ implement a dynamic array? Ive been programming for a while but somehow Ive managed to avoid doing a lot of things correctly for quite some time. I hope it doesnt involve linked lists. I hate em.
The reason I was using new there was because I tried that on a whim for something else, and for some reason it worked... man C++ is wierd. But very fun!
Oh and btw, this is _very_ prelimiary code. I just whipped this up in a few min to get a feel for how I was going to impliment the real thing.
Edited by - kranomano on April 14, 2001 10:30:47 PM
I guess I should really learn how to use STL...
So Stoffel, how would _you_ implement a dynamic array? Ive been programming for a while but somehow Ive managed to avoid doing a lot of things correctly for quite some time. I hope it doesnt involve linked lists. I hate em.
The reason I was using new there was because I tried that on a whim for something else, and for some reason it worked... man C++ is wierd. But very fun!
Oh and btw, this is _very_ prelimiary code. I just whipped this up in a few min to get a feel for how I was going to impliment the real thing.
Edited by - kranomano on April 14, 2001 10:30:47 PM
Sorry to say it, but a linked list is the most suited structure for what you're tring to do. I can show you the double linked list structure that I use, it's pretty basic:
Hope fully I didn't mess that up, I had to edit a lot of little peices of it since I ripped it out of another project.
[EDIT: I did mess it up a little
]
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP's Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
Edited by - Null and Void on April 14, 2001 10:44:31 PM
|
Hope fully I didn't mess that up, I had to edit a lot of little peices of it since I ripped it out of another project.
[EDIT: I did mess it up a little
![](tongue.gif)
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP's Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
Edited by - Null and Void on April 14, 2001 10:44:31 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement