您当前的位置: 首页 >  ar

鱼香ROS

暂无认证

  • 0浏览

    0关注

    498博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ESP8266中关于C语言静态指针赋值问题(const char *)

鱼香ROS 发布时间:2019-07-18 12:12:44 ,浏览量:0

一、介绍

之所以写这个博客是想记录下自己的愚蠢,还有就是基础知识的不牢固。当然还有在嵌入式系统中使用C的注意事项。先描述以下我所遇到的问题,和结构体有关系,在看esp8266_rtos_sdk的http_client这段例程的时候遇到的,我想动态的改变请求的地址,其中要先初始化一个结构体esp_http_client_config_t,它的声明是这样的:

/**
 * @brief HTTP configuration
 */
typedef struct {
    const char                  *url;                /*!< HTTP URL, the information on the URL is most important, it overrides the other fields below, if any */
    const char                  *host;               /*!< Domain or IP as string */
    int                         port;                /*!< Port to connect, default depend on esp_http_client_transport_t (80 or 443) */
    const char                  *username;           /*!< Using for Http authentication */
    const char                  *password;           /*!< Using for Http authentication */
    esp_http_client_auth_type_t auth_type;           /*!< Http authentication type, see `esp_http_client_auth_type_t` */
    const char                  *path;               /*!< HTTP Path, if not set, default is `/` */
    const char                  *query;              /*!< HTTP query */
    const char                  *cert_pem;           /*!< SSL Certification, PEM format as string, if the client requires to verify server */
    esp_http_client_method_t    method;                   /*!< HTTP Method */
    int                         timeout_ms;               /*!< Network timeout in milliseconds */
    bool                        disable_auto_redirect;    /*!< Disable HTTP automatic redirects */
    int                         max_redirection_count;    /*!< Max redirection number, using default value if zero*/
    http_event_handle_cb        event_handler;             /*!< HTTP Event Handle */
    esp_http_client_transport_t transport_type;           /*!< HTTP transport type, see `esp_http_client_transport_t` */
    int                         buffer_size;              /*!< HTTP buffer size (both send and receive) */
    void                        *user_data;               /*!< HTTP user_data context */
    bool                        is_async;                 /*!< Set asynchronous mode, only supported with HTTPS for now */
} esp_http_client_config_t;

我们要看的是url,它被声明成了静态的char类型的指针。当然在初始化之前,这个指针的地址和这个指针所指向的地址都是不存在的。我所遇到的bug就在这里,在没有分配指针地址的时候,就给了其一个指针指向的地址,也就是这个指针的值。下面在详细的讲一下。

二、错误代码

错误代码的错误原因就是在没有初始化的情况下赋值了,一开始以为自己是赋值方法错误,就换了好多种方法。甚至有些编译都通过了,最后把代码下载进去的时候,一运行就会导致模块重启。来看以下错误的尝试:

	/**
	*	以为定义了就初始化了,其实不然,定义了只是定义了,有一些空间还是不存在的,这个时候我应该打印下config .url所在的地址看一下,是否为空。
	*/
    char * url2="http://jxaummd.com:8080/test/getMqttInfo";
    esp_http_client_config_t config ;	
    config.event_handler = _http_event_handler;
    config.url=url2;
	/**
	*	maclloc 方法,忘记能不能编译通过了,反正最后还是错的,虽然config.url初始化成功了,但是config没有初始化成功 ,而且 config.event_handler这个操作,依然是在给一个空的空间放东西,编译不会错,运行肯定挂掉。
	*/
    char * url2="http://jxaummd.com:8080/test/getMqttInfo";
    esp_http_client_config_t config ;	
    config.event_handler = _http_event_handler;
    config.url= malloc(strlen(url2));
    strcpy(url,url2);
三、正确代码

既然是未初始化的问题,那么解决方案就出来了,先进行初始化再赋值操作呗,我们来看一下:

    char * url2="http://jxaummd.com:8080/test/getMqttInfo";
    esp_http_client_config_t config = {
        .url = "http://jxaummd.com:8080/test/getMqttInfo",
        .event_handler = _http_event_handler,
    };
    config.url=url2;

完美解决问题~

四、总结

这个问题折磨了我将近2个小时,最终还是解决掉了,回顾下原因基础薄弱是一,发生了内存泄漏不回去看一下原因也是很狗的,遇到错误就不断是实验这种精神固然是正确的,但是查查资料,看看原理不来的更加快速准确嘛~

关注
打赏
1666092100
查看更多评论
立即登录/注册

微信扫码登录

0.0368s