传递指针静态全局变量用C [英] Passing pointer to static global variable in C

查看:292
本文介绍了传递指针静态全局变量用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是安全的指针传递给一个静态结构到功能?

我有这样的事情:

  mymodule.c的:静态MYEVENT_STRUC_T事件;
uint_32 _myRtos_set_event(MYEVENT_STRUCT_T * event_ptr,uint_32面罩);uint_32 mymodule_set_event(uint_32 event_mask){    / *内核函数* /
    / *定义以外mymodule.c的* /
    / *它是安全的使用和功放叫它;事件? * /
    返回_myRtos_set_event(安培;事件,event_mask);}


解决方案

这是安全的。 静态并不意味着不能被模块外使用,而是不能在模块外部引用,即符号本身不会出口,但变量仍处于可模块之间使用以及方法的存储器某处存在。

我不明朗的唯一的事情是,我不知道它是安全的通过指针传递从用户模式数据到内核模式,如果这是你在做什么。如果我没有记错,有你应该调用从用户空间拷贝内存内核空间中的一些功能。不能完全肯定,但。

Is it safe to pass a pointer to a static struct to a function?

I have something like this:

mymodule.c:

static MYEVENT_STRUC_T event;
uint_32 _myRtos_set_event(MYEVENT_STRUCT_T* event_ptr, uint_32 mask);

uint_32 mymodule_set_event(uint_32 event_mask){   

    /* Kernel function */
    /* Defined outside mymodule.c,*/
    /* Is it safe to call it with &event?  */
    return _myRtos_set_event(&event, event_mask);

}

解决方案

It is safe. static doesn't mean "can't be used outside the module", but rather "can't be referenced outside the module", i.e. the symbol itself won't be exported, but the variable still exists somewhere in the memory of the process an can be used between modules as well.

The only thing I'm uncertain of is that I'm not sure it's safe to pass data from user-mode to kernel-mode via pointer, if that's what you're doing. If I recall correctly, there is some function you're supposed to call that copies memory from user-space to kernel-space. Not entirely sure, though.

这篇关于传递指针静态全局变量用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆