"Anitha Adusumilli" <a.******@gmail.com> writes:
Hi
I am getting bus error, if I include the line "free(stra)" , in the
following code:
#include <stdio.h>
char *func(int);
int main(void)
{
int i;
printf("enter the integer\n");
int num_Args=scanf("%d", &i);
if(num_Args<1)
{
printf("Not a valid number of inputs");
exit(1);
}
char *stra= func(i);
printf("\nprinting the string %s\n",temp);
if(stra!=NULL)
free(stra);
return 0;
}
I have another function "func" which returns the result as a char*. I
used malloc to assign memory to this char* in "func".
I amnt able to understand why this is happening..everything seems to be
fine..The output is also as expected.But still, I amnt able to free the
char*.
I've fixed the indentation. (groups.google.com deletes indentation;
I've complained to Google about this.)
This is not the code you compiled. I can tell you what's wrong with
what you posted, but I have no way of knowing what's wrong with your
actual code. If you post code to comp.lang.c, don't re-type it,
cut-and-paste it directly from the source file that you actually
compiled.
You don't print a newline at the end of the message "Not a valid
number of inputs". Depending on the environment in which you run the
program, this may prevent the message from being displayed properly.
exit(1) is non-portable. The only portable arguments to exit() are 0,
EXIT_SUCCESS, and EXIT_FAILURE. The EXIT_SUCCESS and EXIT_FAILURE
macros, and the exit() function itself, are declared in <stdlib.h>,
which you need to include. (This is unlikely to cause any visible
problems, but you need to fix it anyway.)
You mix statements and declarations in your main program. This is
legal in C99, but not in C90 (it's also legal in C++). You might be
using a C99 compiler, but it's more likely you're using a C++ compiler
or a C compiler that allows this as an extension. If you want your
code to be portable to C90 implementations, all declarations should
precede all statements in each block.
The (stra!=NULL) test is unnecessary but harmless. free(NULL) is well
defined (it does nothing).
You attempt to print a variable called "temp", which is not declared.
(That's how I know this isn't your actual code; you can't get a bus
error in code that will never compile in the first place.) Presumably
you meant to print stra.
The free() function is defined in <stdlib.h>, which you haven't
included. On some systems, this won't cause any visible symptoms; on
others, it can cause serious problems. (Don't worry about which
systems are which, just add the "#include <stdlib.h>".)
Finally, we have no way of guessing what happens inside func(), since
you didn't show it to us. I know you've said elsethread that you've
figured out the problem, but in the future you need to post a complete
self-contained program.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.