Reputation: 35
So I got this function which is able to put a space infront of a "/" C if there is no space. And it cuts up the string fine but I get an error, possibly a memory violation when I try to concat the string together. Please give me a hand.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* substr(const char *pstr, int start, int numchars) {
char* pnew = malloc(numchars + 1);
strncpy(pnew, pstr + start, numchars);
pnew[numchars] = '\0';
return pnew;
}
char* fixString(char str[]) {
char* position;
char* newString = "";
char* finalString;
int oldPosition = 0;
printf("Original str: %s\n", str);
printf("Original length: %d\n\n", strlen(str));
position = strchr(str, '/');
while (position != NULL) {
int charPosition = position - str;
printf("String position: %d->%d\n", oldPosition, charPosition);
newString = substr(str, oldPosition, charPosition - oldPosition);
oldPosition = charPosition;
if (charPosition > 0 && str[charPosition - 1] != ' ') {
printf("Previous char: %c\n", str[charPosition - 1]);
newString = strcat(newString, " ");
}
printf("String: |%s|\n", newString);
if (strlen(newString) > 0) {
finalString[0] = strcat(finalString, newString);
}
printf("------------\n");
position = strchr(position + 1, '/');
}
char* lastString = substr(str, oldPosition, strlen(str));
finalString = strcat(finalString, lastString);
printf("lastString: %s\n\n", lastString);
return finalString;
}
int main() {
char* testString = "/Filter /FlateDecode/Length 7108/Subtype /Type1C";
printf("%s", fixString(testString));
return 0;
}
Upvotes: 0
Views: 1103
Reputation: 186078
You never allocate a target buffer. The finalString
variable isn't initialised to anything.
That's not the only problem with your code, either. You seem to be treating the char *
as some kind of smart string type, but it is nothing more than a pointer into a memory location. For instance, this:
newString = strcat(newString, " ");
doesn't concatenate two strings and return the concatenated result. It appends a space onto the char buffer newString points to and returns the same buffer. Assigning to newString is harmless, but misleading.
// It is the callers responsibility to free the returned string.
char *fixString(char *str) {
int len;
char *s;
char *dest;
int after_space;
// First pass, figure out the size of the output.
len = 0;
after_space = 0;
for (s = str; *s; s++) {
len += 1 + (!after_space && *s == '/');
after_space = *s == ' ';
}
dest = malloc(len + 1);
s = dest;
after_space = 0;
while(*str) {
if (!after_space && *str == '/') *s++ = ' ';
after_space = (*s++ = *str++) == ' ';
}
return dest;
}
Upvotes: 2
Reputation: 856
You might also have to check for the size after malloc() and realloc() if necessary. Welcome to the beautiful world of memory management in C :)
Upvotes: 0
Reputation: 80031
You never malloc
memory for your newString
and finalString
pointers. strcat
expects that the destination pointer you give it has enough space to store the strings.
Upvotes: 0
Reputation: 72299
You need to allocate some memory for finalString
.
Try doing it like this:
#define BUFFER_SIZE 1024
char* finalString = malloc(BUFFER_SIZE);
// ...
strncat(finalString, "something", 1024);
Remember to call free()
on the pointer when you don't need it anymore (not in your function - after the function returns, somewhere in client code, whenever the result's not needed).
Upvotes: 0