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; } }
Why don't you follow your own advice and stick to what we are discussing, instead of trying to divert the thread onto a topic you feel more comfortable with?
I do.
what we are discussing is what is most importanr in C51 "sticking to the standard" or "efficiency"
I vote for 1) maintainability (clarity of code and comments - which has NOTHING to do with "knowledge of the standard") 2)efficiency and 3) size.
That my knowledge of the "C standard" is not as complete as yours, is TOTALLY IRRELEVANT, what is relevant is "quality of code". If i achieve that by banning 'char' so friggin' what. If the code is clear and understandable, efficient and do not use excessive memory I am happy and so will "Those who are unfortunate enough to work on your stuff after you" be. If they want to add 'char' that is their prerogative, but the fact that I have used 'unsigned char' hardly makes "those" "unfortunate"
Erik
No. That is what you are trying to change the discussion to.
I vote for 1) maintainability (clarity of code and comments - which has NOTHING to do with "knowledge of the standard")
What do you think the purpose of standardisation is, then?
That my knowledge of the "C standard" is not as complete as yours, is TOTALLY IRRELEVANT
It is highly relevant to this discussion.
what is relevant is "quality of code".
You are giving the impression that you think that code conforming to the 'C' standard is of lower quality that non-conforming code. Do you wish to correct this impression or is that really the way you feel?
If i achieve that by banning 'char' so friggin' what.
You were given a most clear explanation of char, signed char and unsigned char by another contributor to this thread. Were you unable to understand it?