【问题标题】:segmentation fault while reading a FITS file with healpix使用 healpix 读取 FITS 文件时出现分段错误
【发布时间】:2021-06-04 16:59:56
【问题描述】:

我正在尝试使用 C 代码通过 healpix 打开一个 FITS 文件:

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "chealpix.h"
#include "fitsio.h"
#include <string.h>
#include "fitsio2.h"

int main(int argc, char **argv){

        char *coordsys="G";
        char *order="RING";
        long nside;
        long *p_nside=&nside;
        nside=512.;
        float *map;
        map=read_healpix_map("file.fits",p_nside,coordsys,order);
         
    return(0);
}

这个代码在使用gdb时返回一个segmentation fault (core dumped),我明白了

Program received signal SIGSEGV, Segmentation fault.
ffgkys (status=0x7fffffffdaf4, comm=<optimized out>, value=0x55555555a004 "G", keyname=<optimized out>, 
    fptr=<optimized out>) at getkey.c:808
808     value[0] = '\0';

这是我第一次使用 healpix,我不知道是什么导致了问题。有谁知道可能出了什么问题?

【问题讨论】:

    标签: c linux fits


    【解决方案1】:

    根据read_healpix_map()的文档:

    orderingcoordsysoutput 参数 - 它们是指向函数将写入结果的空间的指针。您已将指针传递给内存中的字符串常量,这些常量通常会被标记为只读,并且在任何情况下都可能不足以接收结果。

    nside 也是一个输出参数,将其初始化为 512 没有任何意义。你不需要p_nside;你可以简单地传递&amp;nside

    它不是最清晰的接口;具体而言,尚不清楚coordsys 是字符串还是单个char。该文档使用单引号,因此您可能会假设 char 但它也会为 ordering 这样做,这显然是一个字符串。假设coordsys 是一个字符串是最安全的——它不会造成任何伤害。

        char  coordsys[] = "G";    // This will be overwritten
        char  order[] = "NESTED" ; // This will be overwritten, 
                                      initialise to largest expected string
                                      to ensure sufficient space is allocated
        long nside = 0 ;
        float* map = read_healpix_map( "file.fits", &nside, coordsys, order ) ;
    
        printf( "nside:%ld, coordsys:%s, order:%s", nside, coordsys, order ) ;
    

    后置脚本:查看source codecoordsysordering 都传递给fits_read_key(),因此coordsys 被视为字符串。

    【讨论】:

    • 我尝试了您的代码,但收到警告“警告:在文件 file.fits 中找不到 COORDSYS 关键字”。这对你有什么作用?
    • @pulsar_hh 我不知道,TBH 我什至不知道 HEALPix 或 FITS 文件是什么——我只是从 C 代码的角度阅读和解释文档。听起来它与 file.fits 的内容有关,而不是与代码有关。
    • @pulsar_hh 我猜它会默认 - 通话后coordsys 包含什么?如果是“G”,请将初始化更改为 "-" 以查看它是默认设置还是保持不变。
    • @pulsar_hh 使用来自fits.gsfc.nasa.gov/fits_samples.html 的样本之一进行测试?标题部分是 ASCII - 我看了一对既不包含COORDSYS。警告在哪里/如何显示?是调用的副作用输出,还是危险地写入coordsys?如果是后者,则需要使缓冲区更大,并且文档很烂。
    • 堆栈粉碎可能有所不同,或者函数向缓冲区写入的内容比文档建议的要多 - 对于 C 代码来说,这是一个非常不安全的接口。使用调试器来确定发生了什么。使缓冲区任意大;例如char order[1024] = "" ;
    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多