473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

problem with free.

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

Nov 14 '05 #1
9 1427
Anitha Adusumilli <a.******@gmail.com> scribbled the following:
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?


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Show me a good mouser and I'll show you a cat with bad breath."
- Garfield
Nov 14 '05 #2
>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

Nov 14 '05 #3

Shan wrote:
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


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.

Nov 14 '05 #4

"Anitha Adusumilli" <a.******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Nov 14 '05 #5
"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.
Nov 14 '05 #6
Keith Thompson <ks***@mib.org> writes:
[snip]
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>.

--
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.
Nov 14 '05 #7

Keith Thompson wrote:
Keith Thompson <ks***@mib.org> writes:
[snip]
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>.

--
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.
Thanks for your suggestions reg improving the above code.

Nov 14 '05 #8
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!
"Anitha Adusumilli" <a.******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googleg roups.com...
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.)


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"?
Nov 14 '05 #9
ph******@alphalink.com.au.NO.SPAM (Peter "Shaggy" Haywood) writes:
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".

--
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.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
14
by: placid | last post by:
Hi all, i have this struct for a binary tree node typedef struct treenode { char *word; struct treenode *right; struct treenode *left; }TreeNode;
3
by: ferbar | last post by:
Hi all, I'm working on a program that tests a random number generator -rand()- in this case. Runs and Chi square test have to be applied to see if the numbers generated are random.. anyway, I'm...
3
by: Sean Shanny | last post by:
To all, We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3 server, 8GB mem, attached to a fully configured 3.5TB XRaid box via fibre channel. I think we have run into this...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
9
by: david | last post by:
I will past only two segments from the code it should be enough to see what I did wrong, I think I know there I made a mistake, but how to fix it I can not tell. This why I need help from you all....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.