I am trying to add multiple character arrays to a function:
#define ID_LEN 5
#define MAX_NAME_LEN 25
#define FALSE 0
char **orderedIds, buffer[5], idString[ID_LEN + 1], inputName[MAX_NAME_LEN + 1], inputDrinkType;
char inputDescription[240];
int i, listSize = 0, uniqueID, validated = FALSE;
This will run fine. However if I add one more byte to inputDescription it will give me an abort trap error:
char inputDescription[241];
开发者_C百科
Also if I add new string arrays I get the same error:
char inputDescription[240], newStringArray[10];
It is a big project with multiple files and functions, adding all the code to give the bigger picture might be a bit hard.... Does anybody know what is happening here???
Resolved:
It all boiled down to me not null terminating a string I was building, before using it in strcat
.
Yes, but only in general
C is not a memory safe language. It's possible to alias an object with an other accidentally; this is sometimes called a wild pointer or a dangling pointer.
The problem isn't really with whether inputDescription has 240 or 241 bytes, rather, the problem is in which object is aliasing which other one and whether that causes a fatal error or a smaller problem. Small changes to your program change the memory layout and change the symptoms of the failure, but the cause is a bug in the program's source code.
If you clean up the program you will probably find the problem.
- Have a prototype for everything
- Turn on some or all of the compiler warnings and fix the identified problems
- Incorporate a memory debugger
- Use a general-purpose debugger and get a traceback
精彩评论