[英]Qt Segmentation Fault strtod
每當我嘗試從C字符串(char *)解析一個數字(即float,double等)時,都會遇到分段錯誤,並且它非常一致。
轉到實際代碼:
//aLocation is a C++ object with two variables double Latitude & double Longitude.
char** tokens; //Gets used in a string split function.
char* pEnd;
aLocation.setLatitude(strtod(tokens[0],&pEnd)); //tokens[0] = "26.379165"
aLocation.setLongitude(strtod(tokens[1],&pEnd)); //tokens[1] = "-80.101639"
應該提到的是,我嘗試了替代函數和數據類型(即atoi,atof,strtol,strtof和strtol)。
我已經檢查了字符串拆分功能,它按預期工作。 我可以使用“ qDebug(” Location:%s,%s“,tokens [0],tokens 1 );”打印標記值。 這兩個stregd()函數在兩個seg錯誤(共同或單獨)之上調用。 當我從應用程序中刪除上述兩行代碼時,根本沒有任何seq錯誤。
運行環境是我使用Buildroot和較舊的Linux內核(3.4.39)制作的自定義Linux映像。 該應用程序是使用Qt 4.8.4 SDK用C ++編寫的。 我在其上運行應用程序的設備是alix3d主板,帶有一些自定義IC附加功能(通過USB)。 我也在使用ld-uClibc.so.0庫。
Location.h:
#ifndef LOCATION_H
#define LOCATION_H
class Location
{
public:
explicit Location();
void setLatitude(double aLat);
double getLatitude();
void setLongitude(double aLong);
double getLongitude();
private:
double latitude;
double longitude;
};
#endif // LOCATION_H
Location.cpp:
#include "location.h"
Location::Location()
{
}
void Location::setLatitude(double aLat){latitude=aLat;}
double Location::getLatitude(){return latitude;}
void Location::setLongitude(double aLong){longitude = aLong;}
double Location::getLongitude(){return longitude;}
更新
Case 1: this->aLocation.setLatitude(1); ------> works (No additional code above or below)
Case 2:double aLat = (double)strtod(tokens[0], &pEnd); ---> works (No additional code below this)
Case 3: Below Code Seg faults
double aLat = (double)strtod(tokens[0], &pEnd);
this->rsuLocation.setLatitude(aLat);
我做了一個非常笨拙的舉動,忘了調用父對象的構造函數。
另外,我也更改了include語句(即#include),也更改了QtCore庫目錄(即#include)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.