trying to accomplish a growing string i tried this example
https://en.cppreference.com/w/c/memory/realloc
but i dont get the correct result (given as possible result)
Code: Select all
New location: 0x144c010. Size: 8 ints (32 bytes).
Old location: 0x144c010. Size: 10 ints (40 bytes).
New location: 0x144c450. Size: 12 ints (48 bytes).
Old location: 0x144c450. Size: 512 ints (2048 bytes).
Old location: 0x144c450. Size: 32768 ints (131072 bytes).
New location: 0x7f490c5bd010. Size: 65536 ints (262144 bytes).
Old location: 0x7f490c5bd010. Size: 32768 ints (131072 bytes).
Code: Select all
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_storage_info(const int* next, const int* prev, int ints)
{
if (next)
printf("%s location: %p. Size: %d ints (%ld bytes).\n",
(next != prev ? "New" : "Old"), (void*)next, ints, ints * sizeof(int));
else
printf("Allocation failed.\n");
}
int main(void)
{
const int pattern[] = {1, 2, 3, 4, 5, 6, 7, 8};
const int pattern_size = sizeof pattern / sizeof(int);
int *next = NULL, *prev = NULL;
if ((next = (int*)malloc(pattern_size * sizeof *next))) // allocates an array
{
memcpy(next, pattern, sizeof pattern); // fills the array
print_storage_info(next, prev, pattern_size);
}
else
return EXIT_FAILURE;
// Reallocate in cycle using the following values as a new storage size.
const int realloc_size[] = {10, 12, 512, 32768, 65536, 32768};
for (int i = 0; i != sizeof realloc_size / sizeof(int); ++i)
{
if ((next = (int*)realloc(prev = next, realloc_size[i] * sizeof(int))))
{
print_storage_info(next, prev, realloc_size[i]);
assert(!memcmp(next, pattern, sizeof pattern)); // is pattern held
}
else // if realloc failed, the original pointer needs to be freed
{
free(prev);
return EXIT_FAILURE;
}
}
free(next); // finally, frees the storage
return EXIT_SUCCESS;
}
addaptions help a little since the example is 32 or 64 bits result
const int pattern[] = {1, 2, 3 , 4};
const int realloc_size[] = {10, 12, 512};
the sizes created by zcc are enourmous and obviously wrong
what library do i need for a correct handeling?
my aim is *char, but thats for later
the original gives 'new' AND 'old' remarks
the zcc version only the 'new' remarks , in both zcc exapmples the size of eg '10' is equal