0

When doing my project I've came across to a problem. I It seems like realloc is not copying all data from original array. This is what I'm dealing with (code still missing error checking, I know) :

char ** dictionary = (char**) malloc(512  * sizeof(char*));

for (int i = 0; i < 256; i++)
{
    dictionary[i] = (char*) malloc(2 * sizeof(char));
    sprintf(dictionary[i], "%c", i);
}

char * character = dictionary[129];

char ** new_dictionary = realloc(dictionary, 1024);
dictionary = new_dictionary;

char * character2 = dictionary[129];

I first create a array of pointers to char pointers. Then I create pointer to array of chars for each byte and assign byte value. Values of character and character2 differ. Why? I've checked for lower and higer indexes (few samples) and the values remains the same. Does this have something to do with that I'm trying to realloc char pointers not just pure data?

enuliver
  • 33
  • 4
  • 1
    Not all values in the range `0` to `255` will be printable characters. Also note that ASCII is only a 7-bit encoding, with values up to (and including) `127`. Anything above that will be implementation-specific. – Some programmer dude Oct 10 '22 at 16:11
  • Also, in your "real" code, do you have error checking? Both `malloc` and `realloc` can fail without warning. – Some programmer dude Oct 10 '22 at 16:13
  • And you really [should not cast the result of `malloc` (or `realloc` or any other function returning `void *`)](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858). – Some programmer dude Oct 10 '22 at 16:14
  • 1
    What does "not all data" mean? How much is available? BTW: `realloc` will probably not copy anything as you are shrinking the memory. Inititally you allocate space for 512 pointers and then resize to 1024 bytes. On a 64bit system that is only sufficient for 128 pointers and `dictionary[129]` will be out of bounds then. – Gerhardh Oct 10 '22 at 16:14
  • By the way, how do you check if `character` and `character2` (which are not good names by the way, as they aren't really characters)? Please [edit] your question to show us a [mre], or if you use a debugger then tell us that in the question itself. – Some programmer dude Oct 10 '22 at 16:16
  • @Gerhardh That out of bounds condition is probably the culprit. Assuming the OP really wanted to *increase* the memory, perhaps you should post an answer with the details from the comment and show how to increase properly? – Some programmer dude Oct 10 '22 at 16:18
  • @Someprogrammerdude Initially I wasn't sure if that would qualify as typo. But now it's an answer. ;) – Gerhardh Oct 10 '22 at 16:22
  • @Gerhardh Yeah, my god how did I do such a simple mistake. I guess it just was not adding up since every higher index I tried did work. Imma close this question. Thanks. – enuliver Oct 10 '22 at 16:22

1 Answers1

3

It is not entirely clear what you are trying to do. I assume you want to increase the memory and expect realloc to copy the content of the initial memory block.

This will not happen in your code:

char ** dictionary = (char**) malloc(512  * sizeof(char*));
...
char ** new_dictionary = realloc(dictionary, 1024);
dictionary = new_dictionary;

char * character2 = dictionary[129];

You are actually shrinking your memory size. Initialliy you reserve memory for 512 pointers. Then you resize to 1024 bytes. On a 64 bit system that will only be sufficient for 128 pointers. As a result, dictionary[129] is an out of bounds access.

You probably wanted to resize this way:

realloc(dictionary, 1024 * sizeof *dictionary)
Gerhardh
  • 11,688
  • 4
  • 17
  • 39