[SOLVED] Is there anything analog to what would recalloc be?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there anything analog to what would recalloc be?
Hi there guys,
I was writing some code for my research work, and I noticed that every time I reallocate my memory, i.e. every time I invoke remalloc, it actually does it work, but the reallocated array is not initialized.
I goggled for something as recalloc, but I do not see it out there.
Would it make sense for me to try to create it? Is there anything like it already out there?
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
From the manual page for malloc():
Quote:
The malloc() function allocates sizebytes and returns a pointer to the allocated memory. The memory is not initialized.
The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes.
If you want to clear the memory, use the memset() function:
Code:
(void) memset ((void *) ptr, '\0', sizeof (ptr));
Where ptr is the pointer to allocated memory and '\0' (null) is set in every position (you can set whatever you'd like).
tronayne: Thanks for your reply, which makes sense.
Nonetheless, my original concerns stands: If you had already filled your array with say, 16 elements, and you are interested in reallocating it with extra 16 elements... would it make sense to use realloc and then memset? I do NOT think so, because memset would initialize the previously assigned values, wouldn't it? Unless, memset is somehow instructed to initialize only the "new" space.
Is there a way of doing that, considering the arguments memset takes?
The stated purpose of realloc() would be defeated by setting the reallocated area to binary zeros.
There is a very-efficient library subroutine which will set an arbitrarily-large block of memory to known-zero. (This is actually how "calloc()" does it.) It may actually use a system call which invalidates the page-table entries for the specified area so that they become flagged as zero-pages without paging them in as a "memset()" type of call might do. Paged-out pages remain paged-out, but when and if demand-paged back in, they will be all-zeros.
Last edited by sundialsvcs; 09-21-2012 at 10:42 AM.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Quote:
Originally Posted by ejspeiro
Is there a way of doing that, considering the arguments memset takes?
Sure. memset() takes a pointer to where you want to start, an initialize character (like '\0', NULL) and a length. You can do a little quick arithmetic, even fancy-schmancy arithmetic that's dynamic to figure out where to point and how far to go.
So, if you've got an area of memory that's 16 bytes long, and you know the beginning address, and you realloc() to add 16 bytes... eh?
If you want to clear the memory, use the memset() function:
Code:
(void) memset ((void *) ptr, '\0', sizeof (ptr));
You probably don't want to use sizeof(ptr) there.
Quote:
Originally Posted by ejspeiro
If you had already filled your array with say, 16 elements, and you are interested in reallocating it with extra 16 elements... would it make sense to use realloc and then memset? I do NOT think so, because memset would initialize the previously assigned values, wouldn't it? Unless, memset is somehow instructed to initialize only the "new" space.
Is there a way of doing that, considering the arguments memset takes?
Hi guys... well this is the implementation that I have tried:
Code:
double *array;
int amount_of_elements;
int max_amount_of_elements;
int requested_memory;
int allocated_memory;
int ii;
array = NULL;
amount_of_elements = 4;
max_amount_of_elements = 16;
allocated_memory = 0;
while (amount_of_elements < max_amount_of_elements) {
requested_memory = amount_of_elements*sizeof(double);
// Reallocate the array AND initialize the new attained memory if the allocation is successful:
array = (double*) realloc(array, requested_memory);
if ((array != NULL) && (requested_memory > allocated_memory)) {
memset(array + allocated_memory, 0, requested_memory - allocated_memory);
} else {
printf("Error.\n");
}
// If the reallocation worked, then update the amount of allocated memory:
allocated_memory = requested_memory;
// Print the array to make sure that the new memory interval has been indeed initialized:
for (ii = 0; ii < amount_of_elements; ii++) {
printf("%.3g ", array[ii]);
}
putchar('\n');
// Fill the array to make sure that the new memory interval can be used:
for (ii = 0; ii < amount_of_elements; ii++) {
array[ii] = ii;
}
// Print the fill the array to make sure that the new memory interval can be used:
for (ii = 0; ii < amount_of_elements; ii++) {
printf("%.3g ", array[ii]);
}
putchar('\n');
// Try with more elements:
amount_of_elements = amount_of_elements + amount_of_elements;
}
free(array);
What am I doing wrong in terms of resetting? Because, this is the output that I get:
As you can see... the first time, I realloc and successfully initialize, but for the second time... I have problem with the initialization to 0s, as it can be noticed by the presence of 'nan' and weird non-zero numbers as 2.37e-320.
Any hints?
I tried to use the bzero function, but since it is kind of an analog to memset, in the sense that you still have to specify the correct limits, I have the same issue.
Yes. Read carefully the description of memset() and realloc() functions in man pages:
Quote:
void *memset(void *s, int c, size_t n);
The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
Quote:
void *realloc(void *ptr, size_t size);
The realloc() function changes the size of the memory block pointed to by ptr to size bytes.
You see what I mean? Sizes are in bytes. Only char variable occupies 1 byte of memory. Each double variable occupies few bytes. So, when you need a place in memory for, say N doubles, you should specify N*sizeof(double) (bytes).
thanks for your reply. But I really believe that the size of a double, i.e., 8 B, is already being considered, given the definition of requested_memory:
Code:
requested_memory = amount_of_elements*sizeof(double);
// Reallocate the array AND initialize the new attained memory if the allocation is successful:
array = (double*) realloc(array, requested_memory);
Furthermore, when I state the amount of data in memset... am I failing on doing it? Because, and please correct me if I am wrong, both values should have the info on the size of the data type within their definitions:
When you write array+allocated_memory, because array is (double*), that is equivalent to &array[allocated_memory], which is definitely not what you wanted (so, array is too smart, it knows, that it consists of doubles). To overcome this difficulty you may write
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.