search

Home  >  Q&A  >  body text

objective-c - Did you encounter a structure assignment failure problem when using C language?

I have a class A:
defines a structure

typedef struct
{
    int sockfd;
    on_av_frame_cb av_frame_cb;
    on_av_frame_cb rtp_cb;
    on_error_cb error_cb;
    uint8_t *p_buf;
    uint8_t *v_buf;
    uint8_t *a_buf;
    int  rtp_flag;
    
}udp_client_t;

Then I call it in class B:

static udp_client_t client;
int jldv_create_client(int src_port,int port ,const char *dst_ip){
    
    
    udp_client_t *udpClient = &client;
    
    assert(udpClient != NULL);
    const char *c_ip = dst_ip;
    
    memset(udpClient, 0, sizeof(udp_client_t));
    
    int ret = create_client(src_port, port, c_ip, &udpClient);
    
    if (ret != 0) {
        
        goto err_output;
    }
    
    udpClient->av_frame_cb = (on_av_frame_cb )onVideoFrame;
    udpClient->error_cb = on_error;
    
    
    
    if (rtp_create(&udpClient) < 0) { ///问题所在之处
        goto err_output;
    }
    
    printf("rtp_client:%d \n",udpClient->rtp_flag);
    return 0;
    
err_output:
    destroy_client(udpClient);
    return -1;
    
}

In the above problem, I used the C class method:

int rtp_create(udp_client_t **data)
{
    logi("%s", __func__);
    
    udp_client_t *udpClient = *data;
    if(udpClient)
    {

        udpClient->rtp_flag = 1;
       
        
        memset(&rtp_cxt, 0, sizeof(rtp_context_t));
        int ret = init_server();
        if (ret < 0)
        {
            return -1;
        }
        rtp_cxt.nalu = alloc_nalu(MAX_FRAME_SIZE);//为结构体nalu_t及其成员buf分配空间。返回值为指向nalu_t存储空间的指针
        if (!rtp_cxt.nalu)
        {
            loge("alloc nalu failed");
            return -2;
        }
    }
    else
    {
        loge("%s: data is null", __func__);
        return -3;
    }
    
    printf("udpClient after==>%d \n",udpClient->rtp_flag);
    return 0;
}

However, the printed result is:

udpClient after==>1 
rtp_client:0 

why is that?
Am I using the wrong one? Asking the gods for help. .

天蓬老师天蓬老师2735 days ago917

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-31 10:38:42

    It’s entirely possible that the logic goes like this:

    udp_client_t *udpClient = *data;
    if (udpClient)
    {
        udpClient->rtp_flag = 1;
        int ret = init_server();
        if (ret < 0)
        {
            // 这里没执行
        }
        rtp_cxt.nalu = alloc_nalu(MAX_FRAME_SIZE);
        if (!rtp_cxt.nalu)
        {
            // 这里没执行
        }
    }
    else
    {
        // 这里没执行
    }
    // 此时 udpClient->rtp_flag => 1 且返回值为 0
    return 0;

    So output 1 and 0, is there any problem?

    reply
    0
  • Cancelreply