• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Could someone tell me why this C code crashes

Special K

Diamond Member
I am trying to write a little program to save me time on a verilog programming project. The program takes as input an assembly language file, and outputs the raw hex bytes, 1 byte per line.

The program appears to work correctly, but always crashes at the very last line of the program. I have determined that this is caused when there are too many characters on one line of the .txt input file. For example, for any line, whatever follows a '#' character is treated as a comment. Well, some of my lines of code have pretty long comments on them. By deleting the comments, I was able to prevent the crash.

My question is, if the long lines of text are causing the program to crash, why does it wait until the very end to crash? Shouldn't it crash right when I call fgets?

Here is the code:

Also you will need a text file for input. Here is a snippet from the one I was using:

[0x00400024] 0x01294826 xor $9, $9, $9 ; 32: xor $t1, $t1, $t1
[0x00400028] 0x3408001f ori $8, $0, 31 ; 33: ori $t0, $zero, 0x1f
[0x0040002c] 0xad280000 sw $8, 0($9) ; 34: sw $t0, 0($t1)
[0x00400030] 0x3408000a ori $8, $0, 10 ; 35: ori $t0, $zero, 0x0a #


As I said above, it was the comments that were causing it to crash. Just type a bunch of garbage in after the '#' character to see what I mean. It will always crash right when it gets to the 'return 0' line.

This program was made under windows using dev-c++, a freeware program.
 
char array [100];
fgets(array,10000,fp1);

That seems like a pretty bad idea - either make the size of array larger, or limit the size of the line read to 100 also (i.e. change 10000 to 100).
Not sure if that's the reason it's crashing, but it's the first thing I noticed.
 
Originally posted by: MSCoder610
char array [100];
fgets(array,10000,fp1);

That seems like a pretty bad idea - either make the size of array larger, or limit the size of the line read to 100 also (i.e. change 10000 to 100).
Not sure if that's the reason it's crashing, but it's the first thing I noticed.

Thanks, that fixed it.

 
Back
Top