简体   繁体   English

更改值但打印时没有分段错误

[英]Segmentation fault when changing value but not when printing

In the following code, I initialize a struct array that is inside another struct.在下面的代码中,我初始化了另一个结构内的结构数组。 After initializing the array, I can print all the values without problem in loop #1.初始化数组后,我可以在循环 #1 中毫无问题地打印所有值。

However, if I try to change the values, as in Loop #2, I get a segmentation fault when when trying to access the element exactly at the middle of the array (ie when j==points/2, or 50 in this particular case).但是,如果我尝试更改值,如循环 #2 中那样,当尝试访问恰好位于数组中间的元素时(即当 j==points/2 或此特定的 50 时)时,我会遇到分段错误案子)。

Why is it that accessing the element to print it works but trying to change it causes a segmentation fault?为什么访问元素以打印它可以工作但试图更改它会导致分段错误?

#include <stdio.h>
#include <stdlib.h>

struct flux{
  double *intense;
  double *tau;
};

struct radius_struct{
  int id;
  struct flux *flux;
};

int main(int argc, const char * argv[]) {
    
    int i,j, ichan, points,nchan;
    points = 100;
    nchan = 20;
    
    struct radius_struct radius_struct;
    radius_struct.id = 99;
    radius_struct.flux = malloc(sizeof(double) * points);
    
    
    for(i = 0; i < points; i++){
      radius_struct.flux[i].intense= malloc(sizeof(double) * nchan);
      radius_struct.flux[i].tau= malloc(sizeof(double) * nchan);

      for(ichan=0;ichan<nchan;ichan++){
        radius_struct.flux[i].intense[ichan] = ichan;
        radius_struct.flux[i].tau[ichan] =ichan;
      }
    }
    
    //Loop #1
    for(j = 0; j < points; j++){
      for(ichan=0; ichan<nchan; ichan++){
         printf("%f %f\n", radius_struct.flux[j].intense[ichan],  radius_struct.flux[j].tau[ichan]);
      }
    }
    
    //Loop #2
    for(j = 0; j < points; j++){
      for(ichan=0; ichan<nchan; ichan++){
          radius_struct.flux[j].intense[ichan] = 123.456;
          radius_struct.flux[j].tau[ichan] = 123.456;
      }
    }

    return 0;
}

Avoid allocation sizing errors.避免分配大小错误。

Allocate to the size of the referenced object.分配给引用的 object 的大小。 Easier to code right, review and maintain.更容易正确编码、审查和维护。

//                             v------------v wrong size 
// radius_struct.flux = malloc(sizeof(double) * points);
radius_struct.flux = malloc(sizeof *(radius_struct.flux) * points);
//                          ^--------------------------^ right size, even without looking up .flux type.

why doesn't it fail when accessing them in the initialization or when printing them before Loop #2?为什么在初始化中访问它们或在循环 #2 之前打印它们时它不会失败?

radius_struct.flux[i].intense and all following code is suspect and subject to undefined behavior as the prior allocation was not certainly sufficient. radius_struct.flux[i].intense和所有以下代码都是可疑的,并且会受到未定义行为的影响,因为先前的分配肯定不够。 Some things might work , others might not.有些事情可能会奏效,有些可能不会。 See also @CiaPan .另请参阅@CiaPan


Robust code would also check the return pointer for allocation failure with a check against NULL .健壮的代码还将通过检查NULL来检查分配失败的返回指针。

as pointed out in the comment, radius_struct.flux is of type "struct flux".正如评论中所指出的,radius_struct.flux 的类型为“struct Flux”。 so all you have to do is replace所以你所要做的就是更换

radius_struct.flux = malloc(sizeof(double) * points);

with

radius_struct.flux = malloc(sizeof(struct flux) * points);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM