Due to popular criticism, I removed the asserts
I'm having a seg fault that I just don't understand...
Code:
//Compile: gcc -Wall -Werror -O -o text textbuffer.c tests.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "textbuffer.h"
void initialize (void);
int main (int argc, char *argv[]) {
initialize ();
return EXIT_SUCCESS;
}
void initialize (void) {
{
TB TestSubject;
printf("\nTest 1: Initialize with text \"abcd\"\n");
TestSubject = newTB("abcd");
printf("Print text -\n%s\n", dumpTB(TestSubject));
printf("Free text buffer\n");
releaseTB(TestSubject);
printf("Passed\n\n");
}
{
TB TestSubject;
printf("Test 2: Text = \"u\\nda\\nsexay \\nno homo\"\n");
TestSubject = newTB("u\nda\nsexay \nno homo");
printf("Print text - \n%s\n", dumpTB(TestSubject));
printf("Number of lines = %d\n", linesTB(TestSubject));
printf("Free text buffer\n");
releaseTB(TestSubject);
//printf("Attempt to find numLines should result in error:\n");
//printf("%d\n", linesTB(TestSubject));
printf("Passed\n\n");
}
return;
}
Code:
//Date created: 09-Dec-13
//Date modified: 09-Dec-13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "textbuffer.h"
typedef struct line *Line;
struct line {
char *text;
int lineNum;
Line next;
};
typedef struct textbuffer *TB;
struct textbuffer {
Line first;
int numLines;
};
static Line newLines (char text[]);
static void memoryError (void);
/* Allocate a new textbuffer whose contents is initialised with the text given
* in the array.
*/
TB newTB (char text[]) {
TB Buff;
Line curr;
int count = 0;
if ((Buff = malloc(sizeof(struct textbuffer))) == NULL)
memoryError();
Buff->first = newLines(text);
for (curr = Buff->first; curr != NULL; curr = curr->next)
count++;
Buff->numLines = count;
return Buff;
}
static Line newLines (char text[]) {
Line list = NULL, curr = list;
int lineCount = 1;
int length = strlen(text);
int subLength;
int i=0, prevPos = i;
if (*text == '\0')
return NULL;
if ((list = malloc(sizeof(struct line))) == NULL)
memoryError();
assert (list != NULL);
list->text = NULL;
list->lineNum = lineCount;
for (; i<=length; i++) {
if (text[i] == '\n') {
subLength = i-prevPos+1;
//line text:
if ((curr->text = malloc(sizeof(char)*(subLength+1)))
== NULL)
memoryError();
assert (curr->text != NULL);
strncpy(curr->text, &text[prevPos], subLength);
curr->text[subLength] = '\0';
//line number:
curr->lineNum = lineCount;
//next line:
if ((curr->next = malloc(sizeof(struct line))) == NULL)
memoryError();
curr = curr->next;
prevPos = i+1;
lineCount++;
}
}
subLength = length-prevPos+1;
if ((curr->text = malloc(sizeof(char)*(subLength+1))) == NULL)
memoryError();
strncpy(curr->text, &text[prevPos], subLength);
curr->next = NULL;
return list;
}
/* Free the memory occupied by the given textbuffer. It is an error to access
* the buffer afterwards.
*/
void releaseTB (TB tb) {
Line curr = tb->first, temp;
for (; curr != NULL; curr = curr->next) {
temp = curr->next;
free (curr->text);
free (curr);
curr = temp;
}
free(tb->first);
free(tb);
tb = NULL;
}
/* Allocate and return an array containing the text in the given textbuffer.
*/
char *dumpTB (TB tb) {
Line curr;
for (curr = tb->first; curr != NULL; curr = curr->next)
printf("%s", curr->text);
return NULL;
}
/* Return the number of lines of the given textbuffer.
*/
int linesTB (TB tb) {
return tb->numLines;
}
static void memoryError (void) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
I get an error when I try to access a node's string, and I have no idea why.