We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I am using calloc in a function to allocate memory to my array. This function writes to the array different number of elements each time the function is called. Here is my problem. Say for the first time function writes to all the 3 locations and next time it writes to only first 2 locations. After the second call, the 3rd memory locations still contains the value written by the first function call. I understand that is the way it works.But I want to know if there is a way to erase all the locations before calling the function again? Is there any built-in function available? Also when I print the values of array initially it doesn't print zeroes. I have read that calloc initializes the memory fields to 0's. Following is the function code.
function write_to_array(int value) { int xdata *ascii_value,i; ascii_value = calloc(3, sizeof (int)); for(i=0;value!=0;i++) { mod = value%10; c = mod+'0'; ascii_value[i] = toascii(c); value/=10; } }
How about memset()? memset (&myStruct, 0, sizeof(myStruct)); If you want the structure initialized to something other than zeroes, you might want to memcpy() from a constant. Very small structures might be more efficiently initialized with a series of assignments. calloc() is essentially malloc() followed by a call to memset() to set all bytes to zero.
Hi Davis, I am using calloc to allocate memory, but it doesn't initialize the bytes to zero as it is expected to do. And I do not want to initialize the memory locations. I just want the locations to be cleared when the function is reentered. Thanks Ramya
I'm afraid I don't understand. What is the difference between "initialize the memory locations" (to zero) and "locations to be cleared"? To me, those mean the same thing, so I'd use memset() at the beginning of the function to clear the dynamically allocated array.
I am sorry Davis. I think I din't write it clearly. I said I do not want to initialize because you were talking about initializing the memory locations to something other than zeroes. If calloc is a combination of malloc and memset, it should initialize the locations to zero without me having to use the memset function. I was wondering why my code was printing some garbage values other than zeroes when I tried printing initial array. Thanks Ramya
I think you do not present the full problem (just a code sniplet where you think the problem might be). Take a look to the source code of calloc in the folder C:\keil\c51\lib\calloc.c
I cant understand your problem. If the number of items in calloc call is fixed and it is only '3' then why cant you do it your self? some thing like:
ascii_value[0] = 0; ascii_value[1] = 0; ascii_value[2] = 0;
If the number of items in calloc call is fixed and it is only '3' then why cant you do it your self? Because calloc is supposed to return a memory block that is initialized with zeros. If it doesn't do that, something is wrong.
...Because calloc is supposed to return a memory block that is initialized with zeros. If it doesn't do that, something is wrong... ofcourse. If it returns a NULL pointer there could be a problem with accessing memory. But see his/her code. He/she is not checking for NULL pointer. I think he/she is confident that he/she has a valid memory accessing.
Have you called init_mempool somewhere in your code before the call to calloc?
If you use calloc like that in a function, you will run out of memory sooner or later... Add a free() to the mix.
If you use calloc like that in a function, you will run out of memory sooner or later... even better: If you use calloc in a '51, you will run out of memory sooner or later... and IMMEDIATELY get in trouble. once more the '51 ain't no PC Erik
You should become an evangelist Eric ;-)
I am using free() at the end of the function code. Somehow I missed to copy that line of code. And I am also manually setting the locations to '0' to make my code work for now. I was just willing to know if there is another way of doing it. But the whole point here is my 'calloc' code line not working fine. As quoted by one of you, I have not used init_mempool(). I will try including this command. Thank you Ramya
Hi all, I have just included init_mempool() in my function and it worked fine. Thanks for all your valuable replies. Ramya
You're welcome. It's probably a common mistake because the C library reference manual entry for malloc and calloc do not mention the need to call init_mempool. As others have said though, calloc really isn't suited here. Why not a simple array
int ascii_value[3] = {0,0,0};