【发布时间】:2011-11-27 05:09:11
【问题描述】:
当我运行它时,它会打印出正确的东西,但随后会冻结?这是分段错误吗?
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;
}
【问题讨论】:
-
我没有看到任何分段错误...您可以尝试一下吗?注释掉
free(string)部分,然后重试... -
如果你在linux上,试试valgrind
标签: c struct segmentation-fault