I’m trying to make some sort of simple console text editor program to get better with C. I’m having trouble with what I thought would be a somewhat simple task:

How do I get each line from my char* buffer, which contains all of my text, so I can output each line, including empty lines, with the correct line number in front of it.

I tried several different ways already, but none have stuck. I tried strtok(), which is what is currently pushed to my repo, and it ignores whitespace. I tried strchr() but did not have the slightest idea how that function worked and got an infinite loop. I tried doing my own function to create an array of lines but that lead to a segmentation fault which was not fixed by mallocing the array. I am at a loss here, I’m not sure what I can do.

Here is the repo: https://codeberg.org/Mister_Bones/txt-ed

Here is the offending code:

// Print contents of file
int print_file(char* buffer) {
	// Print a new line
	printf("\n");

	// Print each line with line number
	// Set first line
	int line_num = 1;

	// Get individual line from buffer
	char* line = strtok(buffer, "\n");

	// Loop through and print lines
	// TODO: Don't ignore whitespace
	while (line != NULL) {
		printf("%4d\t%s\n", line_num, line);
		line = strtok(NULL, "\n");
		line_num++;
	}

	return 0;
}
  • oantby@lemmy.today
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    3 days ago

    So there’s a couple errors I can see in there: one which causes your issue here, and one that will cause another painful issue, unless you’re lucky.

    1. malloc(strlen(buffer + 1)); is not the same as malloc(strlen(buffer) + 1);. Your allocation is actually one byte shorter than your input string. This means your strcpy will copy a byte past the allocated buffer. Only bad things can come out of that, and they may not be noticeable at first.
    2. The issue you’re actually describing here requires maybe a bit better of an understanding of pointers. malloc is giving you back an address - call it 0x0001 - and promises to have at least the available size you requested. You promise to at some point free that address. That address is stored into line. strchr returns a different address - one later in the string, specifically. Your loop continuously advances line to that new address plus one. So, at the end, you’ve been given an address, 0x0001, and you’re trying to free some other address - e.g. 0x10de. That’s why you can’t free; you’re freeing something other than what you malloc’d. You need to hold onto the actual address returned by malloc in another variable, and free that address specifically.

    For #1, I’d personally recommend taking a look at strdup instead of malloc + strcpy. It’s identical functionally, so I invite you to wholly understand all three functions and understand when/why they’re used, but when copying a string exactly, strdup is the way to go - and it removes the possibility of an errant + 1. As part of the understanding, understand that the result of strdup does still need to be given to free eventually.

    As a general note, to provide maybe some clarity on “is it fine to not free strings” - strings don’t really exist in C. That is, there’s not some special thing that the system sees as a string. They’re by convention - a string is a blob of non-'\0' bytes that ends with '\0'. malloc and free do not know or care what you did with the bytes you received, and whenever you see a man page say that it does something with a string (copy, search, etc.), you can confidently substitute string with “blob of non-nul bytes ending with a nul byte”. “string” just sounds nicer, and by convention, that’s what we know a “C string” to be.