0

I've been having issues attempting to copy a word into a multi-dimensional array.

Here is the code I use to create the array:

char *word_buffer;
char *return_result[64];

int buffer_count = 0;
int word_start = 0;
int word_end = 0;

// Some extra, irreverent code.

for (int i = 0; i < length; i += 1) {
    if (text[i] == delim) { // Delim is a value such as '\n'
        word_end = i;
        word_buffer = (char*) malloc(sizeof(char)*64);
        strncpy(word_buffer, text + word_start, word_end - word_start); // Copy the word into word_buffer
        strcpy(*(return_result + buffer_count), word_buffer);

        word_start = i + 1;
    }
    
}

I believe my issue lies with the last line. I attempt to give strcpy a pointer to the address of the 2d array where I want the result of word_buffer to be place. However, this results in a Segmentation Fault.

The goal is to have an array of words returned. I.E.

char *result[10] = { "foo", "bar", "x", "y", "z" };

But to have this done dynamically with code. My code to split the words is working fine. Though, I don't know how to place the value into a 2d array.

Edit: User SHR recommended I try replacing the strcpy line with return_array[buffer_count]=word_buffer;. This does partially work but it crashes after a random amount of values in the array every time. I don't really see how this could be due to high memory usage. Tracking the memory usage of the binary shows nothing out of the ordinary.

  • 1
    What is the type of `return_array`? What is the type of `buffer_count`? Basically, as written, this code is nonsense. Too many missing pieces. Nobody can tell you what's going wrong. – Pete Becker Apr 27 '21 at 13:46
  • 3
    assuming `return_result` is `return_array`. why not just do: `return_array[i]=word_buffer;`? otherwise there is memory leak. – SHR Apr 27 '21 at 13:48
  • 1
    OT: `sizeof(char)` is [guaranteed to be 1](http://eel.is/c++draft/expr.sizeof#1.sentence-4). – Daniel Langr Apr 27 '21 at 13:50
  • 3
    Are you sure this is C++? It looks very C-ish. – Fred Larson Apr 27 '21 at 13:50
  • Yes, `return_result` and `return_array` are the same value. Sorry about that. I did try what you said but this seems to cause a Segmentation Fault after 1428 values (not sure if that value is significant or not). Changing the size of the array doesn't change anything either – NathanBitTheMoon Apr 27 '21 at 13:55
  • @FredLarson: No it is not correct C either: there is a cast of the result of malloc, so it is probably compiled as C++... – Serge Ballesta Apr 27 '21 at 13:55
  • 1
    @SergeBallesta: I didn't say it was *correct* C, I said it was *C-ish*. And while casting the result of `malloc()` is a [topic of some debate](https://stackoverflow.com/q/605845/10077), it is not invalid C. – Fred Larson Apr 27 '21 at 13:58
  • 2
    @NathanBitTheMoon Do you mean it crashes after trying to store 1428 strings into `return_result`? Of course it does; `return_result` only has space for 64 strings. You should really just use `std::vector` instead of all this raw array of `char*` stuff. – Miles Budnek Apr 27 '21 at 15:20

0 Answers0