Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Segmentation fault calling a function
Forum Updated to NodeBB v4.3 + New Features

Segmentation fault calling a function

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 380 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oddball
    wrote on last edited by Chris Kawa
    #1

    Hi,
    I have to parse a string and fill a structure with data. But when i try to step into function parse_mp_info in debugging mode i receive a segmentation fault. The weired thing is that if i don`t initialize pch pointer with NULL no error occures. How can it be this way and how to fix it?
    (Use Qt Creator 7.0.1 on Ubuntu 20.04. Plain C project)

    ===============================example==========================================================

    #include <stdio.h>
    
    typedef struct MarketInfo{
        unsigned long serverTime;
        char   symbol[8];
        size_t request_1m_limit;
        size_t orders_10sec_limit;
        size_t orders_1D_limit;
        double min_price;
        double max_price;
        double lot;
    } MPInfo;
    
    
    int parse_mp_info(MPInfo* stData,const char* pData){
        char *pch=NULL;
        //char *pch; //no error in this case		
        return 0;
    
    }
    
    int main()
    {
        const char *pData     = "{\"timezone\":\"UTC\",\"serverTime\":1657195557242......}";
        MPInfo stParsed       = {0};
        int res               = parse_mp_info(&stParsed,pData);
        if(res!=0)
            printf("Error parsing data!\n");
    
        return 0;
    }
    

    ================================================================================================

    JonBJ 1 Reply Last reply
    0
    • O Oddball

      Hi,
      I have to parse a string and fill a structure with data. But when i try to step into function parse_mp_info in debugging mode i receive a segmentation fault. The weired thing is that if i don`t initialize pch pointer with NULL no error occures. How can it be this way and how to fix it?
      (Use Qt Creator 7.0.1 on Ubuntu 20.04. Plain C project)

      ===============================example==========================================================

      #include <stdio.h>
      
      typedef struct MarketInfo{
          unsigned long serverTime;
          char   symbol[8];
          size_t request_1m_limit;
          size_t orders_10sec_limit;
          size_t orders_1D_limit;
          double min_price;
          double max_price;
          double lot;
      } MPInfo;
      
      
      int parse_mp_info(MPInfo* stData,const char* pData){
          char *pch=NULL;
          //char *pch; //no error in this case		
          return 0;
      
      }
      
      int main()
      {
          const char *pData     = "{\"timezone\":\"UTC\",\"serverTime\":1657195557242......}";
          MPInfo stParsed       = {0};
          int res               = parse_mp_info(&stParsed,pData);
          if(res!=0)
              printf("Error parsing data!\n");
      
          return 0;
      }
      

      ================================================================================================

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Oddball said in Segmentation fault calling a function:

      How can it be this way

      Maybe because pch = NULL tries to write to the stack variable, and you have a stack issue?

      Shown code should not seg fault. Examine stack trace and exact cause/statement/assembly where you say it faults. Rebuild from scratch. Check whether anything happens outside debugger with release and/or debug compiled code. Remove nearly all members in struct MarketInfo. Move it off stack.

      None of this should be necessary from what I can see of your code. Try rebuilding tomorrow? :)

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #3

        Your code you posted does not crash, rebuild your executable and post the real code.
        btw: What's the relation to Qt programming here?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          The code posted certainly does not do anything untoward on my Ubuntu 20.04, GCC 9.4.0, GDB 9.2 combination.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved