Segmentation fault calling a function
-
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; }
================================================================================================
-
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; }
================================================================================================
@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? :)
-
Your code you posted does not crash, rebuild your executable and post the real code.
btw: What's the relation to Qt programming here?