Address 0x0 is not stack'd, malloc'd or (recently) free'd

回眸只為那壹抹淺笑 提交于 2019-12-05 09:18:55

In these two lines

            result = (char *) malloc (sizeof(char) * 128);
            result = str_replace(line, rep, with);

you first allocate space for result that you then loose immediately after by overwriting it with the return of str_replace. That function probably returns 0, so your fputs fails.

BTW, don't cast the return of malloc, in C this is superfluous and may hide the fact that you forgot to include the prototype.

Edit: Your str_replace function is completely wrong in its memory handling. Never return the pointer to a local variable, the space isn't valid after you have left the function.

If NUM is 0, then result is uninitialized and may be 0 by chance.

You aren't checking the results of your calls to malloc(), so a failure can mean you're trying to write to a NULL pointer.

Where is mychars declared?

You don't show how mychars is declared, but this line:

rep = (char *) malloc (sizeof(mychars[i][0]))

looks like it probably only allocates one byte. Also you're allocating a lot of memory and never freeing it. And here:

result = (char *) malloc (sizeof(char) * 128);
result = str_replace(line, rep, with);

You allocate memory using malloc and then completely discard the pointer to that memory by assigning the return value of another function over the top of it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!