简体   繁体   中英

Program Compiles Without Error, but Segfaults During Execution

The following code compiles just fine but when I run it, it gets to the while loop and then SegFaults . The input file is a CSV file. What this program is supposed to take data from a CSV file and parse it into a structure, which is then sent to a function that prints it in a particular format to stdout.

Your airport pointer is null, so when you try to set elements of it, you dereference a null pointer. To fix, either give it automatic storage like so:

airPdata airport;

and pass it to printData like so:

printData(&airport)

Or allocate it with malloc:

airPdata *airport = malloc(sizeof(*airport));

But then make sure to free it when you are finished with it.

Also, just because your C program compiles does not mean it is correct, as you have seen. C is really unforgiving, especially with regards to memory, so you'll definitely need to pay close attention to how you handle memory, and maybe consider unit testing your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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