I'm currently writing a small application to re-familiarise myself with C (it's been a while since I last wrote any) and like most people do, I've run into a memory allocation issue which I cannot figure out.
The code revolves around the setting up of different panels, windows and the associated titles. Precisely it's the allocation of memory for the title string I'm having trouble with.
The base is a structure which contains:
struct TFB_PANEL
{
WINDOW *window;
char *title;
};
This is typedef'd in the header file as:
typedef struct TFB_PANEL TfbPanel;
In the related C file I have the following method which initialises an array TFB of a fixed size.
int tfb_init()
{
if (!_initialised) {
return -1;
}
int i;
for (i = 0; i < TYPE_MAX; i++) {
TFB[i] = malloc(sizeof(TfbPanel*));
TFB[i]->title = calloc((strlen(TYPES[i]) + 18), sizeof(char*));
switch(i)
{
case A:
sprintf(TFB[i]->title, " %s | r | h | s | t ", TYPES[i]);
break;
case B:
sprintf(TFB[i]->title, " f | %s | h | s | t ", TYPES[i]);
break;
case C:
sprintf(TFB[i]->title, " f | r | %s | s | t ", TYPES[i]);
break;
case D:
sprintf(TFB[i]->title, " f | r | h | %s | t ", TYPES[i]);
break;
case E:
sprintf(TFB[i]->title, " f | r | h | s | %s ", TYPES[i]);
break;
}
TFB[i]->window = tfb_create_window(i);
}
return 0;
}
Now the error occurs when C is initialised. A and B get set correctly to a length of 25 characters. C on the other hand should contain 24 characters after initialisation but instead (from within tfb_create_window) I recieve
Program received signal EXC_BAD_ACCESS, Could not access memory
Reason: 13 at address 0x0000000000000000
0x00007fff930b9390 in strcmp()
Examining the stack shows TFB[C] being initialised correctly but the title element is not. This contains a null element as if I had never made the call to calloc.
Please can someone explain where I am going wrong with this or why A and B get initialised correctly but C kills the app. Everything was going beautifully until about 6am this morning, it's been down hill since then.
If it helps, tfb_create_window is defined with a call to _create_window and a call to tfb_get_title as follows:
char *tfb_get_title(int type)
{
if (type >= TFB_MAX) {
return (char*)NULL;
}
return TFB[type]->title;
}
WINDOW *tfb_create_window(int type)
{
int height = ((LINES - WIN_OFFSET_Y) / 3);
char *title = tfb_get_title(type);
return _create_window(height, WIN_SIDEBAR_X, WIN_OFFSET_Y, 0, COLOUR_MAIN, title);
}
WINDOW *_create_window(int height, int width, int starty, int startx, int color, const char *title)
{
WINDOW *window;
window = newwin(height, width, starty, startx);
box(window, 0, 0);
mvwprintw(window, 0, 2, title);
wbkgd(window, COLOR_PAIR(color));
return window;
}
calloc((strlen(TYPES[i]) + 18), sizeof(char*)). This allocates space for(strlen(TYPES[i]) + 18char pointers, which is probably 4 or 8 times as much memory as you really need.strlen(TYPES[i]) + 18but I added the multiplier whilst trying to fix it. I've also tried just usingmalloc(strlen(TYPES[i]) + 18)but to no avail.malloc(sizeof(TfbPanel*))instead ofmalloc(sizeof(TfbPanel))? Also, what isTFB[i]?TFB[i]is a pointer to a valid block of memory which previously was working fine until I added the dynamic title in. TheTFBarray is defined as:TfbPanel *TFB[TYPE_MAX]TfbPanelobject?