problem with free.

  • Thread starter Anitha Adusumilli
  • Start date
A

Anitha Adusumilli

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*.

Am I missing any pt?

Regards
Anitha
 
J

Joona I Palaste

Anitha Adusumilli said:
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);

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*.
Am I missing any pt?

Showing the code to func() might help. For all we know, it could
include zillions of bugs on every line.
 
S

Shan

free(stra);

For every free() there should be a malloc where you allocate memory.
Only when you allocate memory, you can free it. Post the of func()
also.

Cheers
Shan
 
A

Anitha Adusumilli

Shan said:
For every free() there should be a malloc where you allocate memory.
Only when you allocate memory, you can free it. Post the of func()
also.

Cheers
Shan

I cud solve the problem. Sorry for the post. I was confused because I
am able to see the result properly but still cudnt free the memory.
Anyways, thanks.
 
M

Mike Wahler

Anitha Adusumilli said:
Hi

I am getting bus error, if I include the line "free(stra)" , in the
following code:


#include <stdio.h>

#include <stdlib.h>

(if you don't include this header, which provides
the prototype for 'malloc()', a C89 compiler will
assume it returns type 'int', very likely causing
problems.)
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*.

Am I missing any pt?

SHow us the *complete* program, or we can't know for sure
(i.e. also include your 'func()' function).

-Mike
 
K

Keith Thompson

Anitha Adusumilli said:
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.
 
K

Keith Thompson

Keith Thompson said:
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>".)

Sorry, I meant to say that the free() function is *declared*, not
defined, in <stdlib.h>.
 
P

Peter Shaggy Haywood

Groovy hepcat Mike Wahler was jivin' on Fri, 07 Jan 2005 16:07:01 GMT
in comp.lang.c.
Re: problem with free.'s a cool scene! Dig it!
#include <stdlib.h>

(if you don't include this header, which provides
the prototype for 'malloc()', a C89 compiler will
assume it returns type 'int', very likely causing
problems.)

Right, but his code was not C89/90 compliant. It was C99 compliant,
though, notwithstanding the missing header. In it he was declaring a
variable after executable statements. So, without a declaration for
malloc() the compiler should have rejected the code.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
K

Keith Thompson

Groovy hepcat Mike Wahler was jivin' on Fri, 07 Jan 2005 16:07:01 GMT
in comp.lang.c. [...]
#include <stdlib.h>

(if you don't include this header, which provides
the prototype for 'malloc()', a C89 compiler will
assume it returns type 'int', very likely causing
problems.)

Right, but his code was not C89/90 compliant. It was C99 compliant,
though, notwithstanding the missing header. In it he was declaring a
variable after executable statements. So, without a declaration for
malloc() the compiler should have rejected the code.

Yes, either a C90 compliant compiler or a C99 compliant compiler
*should* have rejected the code. Apparently the OP didn't compile the
posted code with either.

Possibly the OP used a compiler that allows intermixed declarations
and statements *and* allows calls to undeclared functions.

Note that the posted code almost certainly isn't what the OP actually
compiled. As I pointed out elsewhere in this thread, it refers to an
undeclared variable "temp".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top