0

Currently I'm writing a rather extensive homework assignment that - among other things - reads a file, builds a binary search tree and outputs it.

Somewhere inside all that I've written a recursive method to output the values of the binary search tree in order.

void output(node* n)
{
if(n->leftChild != NULL)
    output(n->leftChild);
cout << n->keyAndValue << " || ";
outputString += n->keyAndValue << '|';
if(n->rightChild != NULL)
    output(n->rightChild);
}

No problem with that, but you'll notice the line outputString += n->keyAndValue << '|';, because I also want to have all the values inside a char array (I am not allowed to use strings or other more current features of C++) that I can use later on in a different method (e.g. Main method).

The Char-Array is declared as follows:

char *outputString;

This being just one of the ways I've tried. I also tried using the const keyword and just regularly building an array char outputString[]. With the version I've shown you I encounter an error when - later on in the program in a different method - calling the following code:

cout << outputString;

I get the following error:

Unhandled exception at 0x008c2c2a in BST.exe: 0xC00000005: Access Violation reading location 0x5000000000.

Any clue as to how I'd be able to build a dynamic char array, assign values to it numerous times using += and outputting it without triggering an access violation? I am sorry for asking a rather basic question but I am entirely new to C++.

Thanks and Regards,

Dennis

Mike
  • 47,263
  • 29
  • 113
  • 177
Dennis Röttger
  • 1,975
  • 6
  • 32
  • 51
  • 10
    Whoever is stopping you from using `std::string` should be taken outside and shot. Every day for a week. – David Heffernan Jan 16 '13 at 13:57
  • 1
    Are you conclusively proving to the SO community that you are unable to pick up a C++ book and read it? – Tony The Lion Jan 16 '13 at 13:57
  • @DavidHeffernan It's a homework assignment. – Mysticial Jan 16 '13 at 13:57
  • You cannot append to char pointers like that, not to mention that `outputString += n->keyAndValue << '|'` would not work as written even on an `std::string`. If you are constrained to `char*` it's going to be needlessly hard, but look into creating a "very large" buffer with `malloc` and then appending to it with `strcat`. – Jon Jan 16 '13 at 13:58
  • as this is homework I would only point you in the general direction of `malloc` and leave it at that. of course, if it is allowed, you should be using `std::string` instead of `char*`. – Oren Jan 16 '13 at 13:58
  • i think you should tag either C or C++, not both as they differ quite a lot. – AndersK Jan 16 '13 at 14:04
  • What is it that they are teaching you, C or C++ ? Please remove the other tag, then. – Jens Gustedt Jan 16 '13 at 14:05
  • You seem to be guessing what `char*` is and how it can be used. Don't guess. Learn it from decent books. Hint: it's not a char array. – sellibitze Jan 16 '13 at 14:24
  • Is it even necessary to build a string? can't you just directly output the stuff on the console? – sellibitze Jan 16 '13 at 14:30

2 Answers2

3

I'm guessing that since you can't use std::string, you also can't use new[].

You can concatenate strings with a function like this:

char *concat(const char *s1, const char *s2)
{
    size_t len = strlen(s1) + strlen(s2);
    char *result = (char*)malloc(len+1);
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

This can be done more efficiently, but that probably doesn't matter for homework. And you need to check for errors, etc. etc.

You also need to decide who is going to call free on s1 and s2.

For what it is worth, the efficient version looks like this:

char *concat(const char *s1, const char *s2)
{
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);
    char *result = (char*)malloc(len1+len2+1);
    memcpy(result, s1, len1);
    memcpy(result+len1, s2, len2);
    result[len1+len2] = '\0';
    return result;
}

It's more efficient because it only walks the input strings once.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

+= on pointers does pointer arithmetic, not string concatenation. Eventually you get way beyond your array that outputString was pointing to, and trying to print it leads to a segfault.

Since you can't use std::string, you need to use strcat along with new[] and delete[] and make sure you allocated your original array with new[].

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Thanks for the quick response, unfortunately, as I stated, I am not allowed to use strings for the sake of doing my homework within the means of the knowledge I should've acquired during the lectures. – Dennis Röttger Jan 16 '13 at 13:57
  • 1
    @DennisRöttger sorry, bad reading. You need to just reallocate the array and copy into it whenever you need to concatenate. – Seth Carnegie Jan 16 '13 at 13:58
  • 1
    It's also worth mentioning that plain C strings need to be `\0`-terminated before being output somewhere via `cout` or `FILE`. – Alexey Frunze Jan 16 '13 at 14:02