我使用Visual Studio 2008与.NET Framework 3.5和制作C++/CLI/.NET Forms应用程序; 一个游戏的编辑.除编辑器之外的所有游戏代码都是纯C++.我已经到了需要从非托管代码调用的Forms代码中回调的地步.
我正在使用GetFunctionPointerForDelegate.
首先,我在Form类中有这些:
public: delegate void ADelegate(Int32, float, float, float, Int32);
public: static ADelegate^ delegateInstance;
Run Code Online (Sandbox Code Playgroud)
然后我在Form构造函数中设置回调:
// Set up World Objects callback
typedef void (__stdcall *WorldLocationUpdateCallback)(Int32, float, float, float, Int32);
WorldLocationUpdateCallback _callback;
// Create an instance of a delegate, using GetFunctionPointerForDelegate
delegateInstance = gcnew ADelegate( this, &MainForm::InitWorldObjectPane );
// Convert the delegate to a pointer
IntPtr anIntPtr = Marshal::GetFunctionPointerForDelegate( delegateInstance );
// Cast the pointer into a function pointer
_callback = static_cast<WorldLocationUpdateCallback>(anIntPtr.ToPointer());
CEditorUI::UI()->setWorldLocationUpdateCallback( (void*)_callback …Run Code Online (Sandbox Code Playgroud) 是否有任何工具(最好在Linux上)可以在参数定义为较小的数组时发出警告,然后原型指定?
例如:
void somefunc(float arg[10]); /* normally this would be defined in a header */
void my_func(void)
{
float arg[2];
somefunc(arg); /* <-- this could be a warning */
}
Run Code Online (Sandbox Code Playgroud)
我意识到这不是无效的代码,但它可以解决一些常见的错误,如果它可以警告它(最近碰到其中一个错误).
一些工具(例如clang静态检查器)会在函数位于同一个文件中并在数组边界外设置一个值时发出警告,但我想知道如果arg比原型单独小,是否有任何事情会发出警告.
我用过cppcheck,clang,smatch,splint,gcc's -Wextra ......但没有人抱怨这个.
1167 ptr = (void*)getcwd(cwd, MAX_PATH_LENGTH-1);
(gdb) n
1168 if (!ptr) {
(gdb) print ptr
$1 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) print &cwd
$2 = (char (*)[3500]) 0xbff2d96c
(gdb) print strlen(cwd)
$3 = 36
(gdb) print "%s",cwd
$4 = "/media/MMC-SD/partition1/aaaaaaaaaaa", '\0' <repeats 912 times>, "??O?001\000\000\000\000??027\000\000\000?3????EL?3?000??027\000\000\000\000\000\000\000\027\000\000\000\000??/?027\000\000\000?3????N????\230????3?000??027\000\000\000\000\000\000\000??000\000\000\000\001\000\000\000??M?3????\000\000\000\000.\231?027??w\005\b\001\000"...
(gdb) print "%s", ptr
$5 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) Quit
Run Code Online (Sandbox Code Playgroud)
为什么ptr正确打印字符串但不是cwd; 如果我尝试使用cwd,这也会影响程序并崩溃...
[编辑:事实证明崩溃是由于此变量上的一个愚蠢的缓冲区溢出... grr ...而不是gdb,但打印问题仍然有效]
我有一个服务器,它具有返回用户注册日期的功能.任何用户都可以看到任何非隐藏用户(在此示例中,只隐藏了user2).服务器dateRequest从客户端获取a 并使用id它来在用户文件中查找相应用户的日期:
package main
import (
"bytes"
"fmt"
)
const datelen = 10
//format: `name 0xfe date age 0xfd password`; each user is seperated by 0xff
var userfile = []byte("admin\xfe2014-01-0140\xfdadminpassword\xffuser1\xfe2014-03-0423\xfduser1password\xffuser2\xfe2014-09-2736\xfduser2password")
func main() {
c := clientInfo{0, 0, 0}
fmt.Println(string(getDate(dateRequest{c, user{0, "admin"}})))
fmt.Println(string(getDate(dateRequest{c, user{0, "admin______________"}})))
fmt.Println(string(getDate(dateRequest{c, user{1, "user1"}})))
//fmt.Println(string(getDate(dateRequest{c,user{2,"user2"}}))) // panic
fmt.Println(string(getDate(dateRequest{c, user{1, "user1_________________________________________________"}})))
}
func getDate(r dateRequest) []byte {
if r.id == 2 {
panic("hidden user")
}
user := bytes.Split(userfile, []byte{0xff})[r.id]
publicSection := bytes.Split(user, []byte{0xfd})[0]
return …Run Code Online (Sandbox Code Playgroud) 我得到运行时检查失败#2 - 当我运行以下代码时,在变量'obj'周围堆栈已损坏错误.我知道这是失败的,因为覆盖'obj'的边界会导致损坏堆栈.那么如何防止缓冲区溢出呢.
typedef struct _INFO {
int Count;
} Info, *InfoPtr;
#define MAX_COUNT 10
//void fn(Info(*obj)[MAX_COUNT])
void fn(Info (*obj)[MAX_COUNT])
{
for (int i = 0; i < 2; i++)
{
obj[i]->Count = i;
}
}
int main()
{
Info obj[MAX_COUNT];
fn(&obj);
return 1;
}
Run Code Online (Sandbox Code Playgroud) 在我的代码中,我使用的xyz是10个对象的数组.当我尝试使用unsigned int index访问数组元素时xyz[level],我得到'Buffer overrun '警告.从逻辑上讲,我很确定该级别不会超过10.如何避免此警告?