Thursday, 3 April 2014

Memory errors in C

I am minute (first time have mercy) some formula in c for a propagandize
project. Basically we am minute a integrate of functions to manipulate
strings but controlling a strings library.I am going to concentration on
one of these duty (ml - multiplies a cut several times in place) since it
throws a memory allocation/ giveaway blunder when controlling it several
times. Other functions tend to pitch identical errors. What is rare to me
is that on their possess all functions work morally though when in a
method of some arrange pitch errors. MAIN SNIPET: int main(){char
*str;int size;char cmd[2];printf("Enter your
string:\n");fflush(stdout);scanf ("%d",&size);str = calloc
(size+1,sizeof(char));checkAlloc(str);scanf ("%s",str);while (!compare
(cmd, "ex")){ ... ... ... else if (compare (cmd, "ml")){
/* <--- ml */ int d; scanf ("%d", &d); ml
(str, d); /* in place change of str */ printf ("Current twine is
%s\n", str);. ML FUNCTION: (other functions combined in identical way,
i refrain from posting a whole formula since it is long) vacant
ml (char* str, int d){ /* O(dN) = O(N) (d is a constant) */char
*concat,*result;char *p;char *ps; /* use to camber str */int len =
ln(str);int i,j;concat = calloc (len +1,
sizeof(char));checkAlloc(concat);/* emanate a duplicate of str to concat
to str many times */for (i = 0; i < len; i ++){ concat[i] = str
[i];}concat[len] = '\0';result = calloc (len*d+1,
sizeof(char));checkAlloc(result);ps = result;for (i = 0; i < d; i++){ p
= concat; /*reset concat pointer, duplicate form start */ for (j = 0; j
< len ; j++) *ps++ = *p++;}*ps = '\0';free (concat);len =
ln(result);for (i = 0; i < len; i ++){ /* register outcome into str for an
"in-place" change */ str[i] = outcome [i];}str[len] = '\0';free
(result);}when controlling ml 3 (multiply twine by 3, abc -> abcabcabc) it
works glorious , when doing ml 3, ml 2 it works fine. when doing ml 0 it
works fine. though when doing ml 3, ml 2, ml 0 it throws an * glibc
detected /home/david/workspace2/soft2/Strings: unfair fastbin opening
(free): 0x0893a028 **error.So could anyone broadcast some light on what
I'm doing wrong? AM i atonement things that shouldn't be freed, maybe
allocation in correctly? Ones again, this is all new to me so greatfully
bear with.

No comments:

Post a Comment