Chris
Chris

Reputation: 8432

Abort trap when trying to add char arrays

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];

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.

Upvotes: 4

Views: 6544

Answers (1)

DigitalRoss
DigitalRoss

Reputation: 146093

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.

  1. Have a prototype for everything
  2. Turn on some or all of the compiler warnings and fix the identified problems
  3. Incorporate a memory debugger
  4. Use a general-purpose debugger and get a traceback

Upvotes: 4

Related Questions