Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是分段错误吗?程序冻结。(c)_C_Struct_Segmentation Fault - Fatal编程技术网

这是分段错误吗?程序冻结。(c)

这是分段错误吗?程序冻结。(c),c,struct,segmentation-fault,C,Struct,Segmentation Fault,当我运行它时,它会打印出正确的内容,但随后会冻结?这是分段错误吗 int main(int argc, char** argv) { int NumElements = 2; /* the number of elements in the string array */ String* string = (String*) malloc((sizeof (String*)) * NumElements); /* allocate some memory for the app to u

当我运行它时,它会打印出正确的内容,但随后会冻结?这是分段错误吗

int main(int argc, char** argv) {
  int NumElements = 2; /* the number of elements in the string array */
  String* string = (String*) malloc((sizeof (String*)) * NumElements); /* allocate some memory for the app to use */
  unsigned int BytesReserved = ((sizeof (String*)) * NumElements); /* the amount of bytes reserved */

  /* debug */
  printf("Number of elements: %d\nAmount of bytes reserved: %u\n", NumElements, BytesReserved);
  string[0] = String_Set("Hello, ");
  string[1] = String_Set("world!");

  /* loop through the array of strings and print each one */
  int i;
  for (i = 0; i < NumElements; i++) {
    printf("%s\n", String_CString(string[i]));
  }

  free(string); /* deallocate the allocated memory we used earlier */
  return 0;
}
int main(int argc,char**argv){
int NumElements=2;/*字符串数组中的元素数*/
String*String=(String*)malloc((sizeof(String*))*NumElements);/*分配一些内存供应用程序使用*/
unsigned int bytes reserved=((sizeof(String*))*NumElements);/*保留的字节数*/
/*调试*/
printf(“元素数:%d\n保留的字节数:%u\n”,NumElements,bytesreperved);
字符串[0]=字符串集(“你好,”);
字符串[1]=字符串集(“世界!”);
/*循环遍历字符串数组并打印每个字符串*/
int i;
对于(i=0;i
如果您需要一个字符串数组,那么您为“字符串”分配的空间似乎太少了 分配数组,然后为数组中的每个字符串分配

String** string = malloc((sizeof (String*)) * NumElements);  // notice the extra *
现在,对于“string”中的每个字符串,您需要分配可以包含要存储的整个字符串的空间:

string[0] = malloc( strlen( "Hello, " ) + 1 ) // (including trailing \0)
strcpy( string[0], "Hello, " );

不知道你在字符串集中做了什么,也许你正在做,我没有看到任何分割错误…你能试试吗?注释掉
free(string)
部分,然后再试一次……如果您使用的是linux,请尝试“谢谢”,因为它清除了很多内容,而且string\u集几乎是strdup