使用指针和参考时发生非法指令

发布于 2025-01-24 21:05:07 字数 1152 浏览 4 评论 0原文

阅读,我有很多有关指针和参考的问题。相关代码如下所示:

void writeFromNonRT(const T& data)
{
    // get lock
    lock();

    // copy data into non-realtime buffer
    *non_realtime_data_ = data;
    new_data_available_ = true;

    // release lock
    mutex_.unlock();
}

要确定,我尝试编码类似的代码,例如:

#include <iostream>
using namespace std;

void pt_ref(int& data) 
{
    int *ptr;        
    ptr = &data;    // ptr points to "data"
    cout << "data's addres: "<< ptr <<"\n"; // print address
}

int main() 
{
    int x = 3;
    pt_ref(x);
    cout << "x's address: " << &x;
}
\\ output:
\\ data's addres: 0x7ffe05c17c4c
\\ x's address: 0x7ffe05c17c4c

此代码运行良好,但它与源代码仍然不同。

// these 2 lines are different.
*non_realtime_data_ = data;
ptr = &data;

因此,我尝试将ptr =&amp; data;更改为*ptr = data;,然后再次运行代码,发生了错误(“非法指令”)。

希望有人可以回答我,非常感谢。

PS:我在REPLIT在线编译器上运行了代码。

when reading the source codes of realtime_tools::RealtimeBuffer, I got lots of questions about the pointer and reference. The related codes are shown below:

void writeFromNonRT(const T& data)
{
    // get lock
    lock();

    // copy data into non-realtime buffer
    *non_realtime_data_ = data;
    new_data_available_ = true;

    // release lock
    mutex_.unlock();
}

To figure out, I tried to code the similar code like:

#include <iostream>
using namespace std;

void pt_ref(int& data) 
{
    int *ptr;        
    ptr = &data;    // ptr points to "data"
    cout << "data's addres: "<< ptr <<"\n"; // print address
}

int main() 
{
    int x = 3;
    pt_ref(x);
    cout << "x's address: " << &x;
}
\\ output:
\\ data's addres: 0x7ffe05c17c4c
\\ x's address: 0x7ffe05c17c4c

This code runs well, but it's still different to the source code.

// these 2 lines are different.
*non_realtime_data_ = data;
ptr = &data;

So I tried to change ptr = &data; to *ptr = data;, and ran again the code, the error("illegal instruction") occurred.

Hope someone can answer me, thanks a lot.

PS: I ran the code on the replit online compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2025-01-31 21:05:08

我试图更改ptr =&amp; data;到 *ptr = data;,然后再次运行代码,发生了错误(“非法指令”)。

问题是指针ptr (并且不指向任何int对象)等在左侧删除该指针(您在**ptr上写的指针)会导致不确定的行为

    int *ptr;   //pointer ptr does not point to any int object as of now    
   *ptr = data;
//-^^^^--------->undefined behavior since ptr doesn't point to any int object

要解决此问题,请确保在取消ptr之前,指针ptr指向某些int对象。

void pt_ref(int& data) 
{
    int var = 10; //int object
//-------------vvvv-------->now ptr points to "var"
    int *ptr = &var; 
//--vvvv------------------->this is fine now       
    *ptr = data;             
}

I tried to change ptr = &data; to *ptr = data;, and ran again the code, the error("illegal instruction") occurred.

The problem is that the the pointer ptr was uninitialized(and does not point to any int object) and so dereferencing that pointer(which you did when you wrote *ptr on the left hand side) leads to undefined behavior.

    int *ptr;   //pointer ptr does not point to any int object as of now    
   *ptr = data;
//-^^^^--------->undefined behavior since ptr doesn't point to any int object

To solve this make sure that before dereferencing ptr, the pointer ptr points to some int object.

void pt_ref(int& data) 
{
    int var = 10; //int object
//-------------vvvv-------->now ptr points to "var"
    int *ptr = &var; 
//--vvvv------------------->this is fine now       
    *ptr = data;             
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文