473,399 Members | 3,038 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,399 software developers and data experts.

free() is useless... (I WAS WRONG)

It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems. Perhaps this is not the correct group to ask, but from what
I understand pthread_join causes the main to 'sleep' until the pthread
returns. That's kinda pointless for me since I want to run _many_
pthreads at once. I looked into pthread_detach, but could not get it
to work properly. Should I be using pthread_join?

Matt

----------

Well, it's not useless, it just doesn't seem to be working for me.

Basically, when I'm returning a pointer from a function, free does not
seem to be freeing the memory for me. Here is an example of what I am
trying to do:

char *blah (void) {
char *string;

string = (char *) malloc(sizeof("foobar")*sizeof(char));
strcpy(string, "foobar");

return string;
}

int main (void) {
char *string;

string = blah();
printf("%s\n", string);
free(string);

return 0;
}
-----

I'm not quite sure how to properly diagnose this... as I haven't
touched C in quite some time. Although, the problem I am having now
is evident b/c the software is a high volume socket server and I can
see the process size growing with `top`.

Any help would be very much appretiated.
Thanks,
Matt.
Nov 14 '05 #1
6 1207
"Matt Gostick" <ma**@crazylogic.net> wrote in message
news:c4*************************@posting.google.co m...
It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems.
Please reply in the thread. (I see you posted through Google, which might
explain things going by some other posts.)
Perhaps this is not the correct group to ask, but from what
I understand pthread_join [...]


No, it's not the right group to ask. Try comp.unix.programmer instead.

HTH,
Alex
Nov 14 '05 #2
ma**@crazylogic.net (Matt Gostick) writes:
It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems. Perhaps this is not the correct group to ask, but from what
I understand pthread_join causes the main to 'sleep' until the pthread
returns. That's kinda pointless for me since I want to run _many_
pthreads at once. I looked into pthread_detach, but could not get it
to work properly. Should I be using pthread_join?


You're right; this isn't the correct group to ask. (Standard C has no
concept of threads.) Try comp.programming.threads.

--
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 #3
Alex Fraser wrote:
"Matt Gostick" <ma**@crazylogic.net> wrote in message
news:c4*************************@posting.google.co m...
It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems.


Please reply in the thread. (I see you posted through Google, which
might explain things going by some other posts.)


The braindead google newsreader starts a new thread whenever the
subject is changed. There's really no good reason to ever use that as a
news service.

For those without news service from their ISP, the free service at
http://news.individual.net works fine.

Brian Rodenborn


Brian Rodenborn
Nov 14 '05 #4
In article <ln************@nuthaus.mib.org>, ks***@mib.org says...
ma**@crazylogic.net (Matt Gostick) writes:
It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems. Perhaps this is not the correct group to ask, but from what
I understand pthread_join causes the main to 'sleep' until the pthread
returns. That's kinda pointless for me since I want to run _many_
pthreads at once. I looked into pthread_detach, but could not get it
to work properly. Should I be using pthread_join?


You're right; this isn't the correct group to ask. (Standard C has no
concept of threads.) Try comp.programming.threads.


Let me second this and explain why comp.programming.threads is a better
option than the often recommended comp.unix.programmer for this. Pthread
code (POSIX compliant) can be written on a fairly wide number of OS platforms
and compiler combinations, including Linux, Windows (with add-on library),
Mac OS X, Novell NetWare, and a slew of others. Also, several of the
"standard pedants" for pthreads similar to the C standard wordmeisters here
hang out there, although they tend to focus more on technical rather than
wordsmithing issues. Either way, although there is overlap, the answers
in c.p.t seem to be more generic whereas the answers in c.u.p often lean
towards platform-specific answers.

--
Randy Howard
To reply, remove FOOBAR.
Nov 14 '05 #5
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:MP************************@news.verizon.net.. .
In article <ln************@nuthaus.mib.org>, ks***@mib.org says...

[snip]
You're right; this isn't the correct group to ask. (Standard C has no
concept of threads.) Try comp.programming.threads.


Let me second this [...]


I'll third it. It didn't occur to me earlier, but is almost certainly a
better choice than c.u.p for pthread stuff.

Alex
Nov 14 '05 #6
[snips]

On Thu, 02 Sep 2004 00:19:44 -0700, Matt Gostick wrote:
It was my pthreads that were leaking.. not my string ops. doh. string = (char *) malloc(sizeof("foobar")*sizeof(char));
strcpy(string, "foobar");


All other issues aside, why are you casting malloc, in direct violation of
about 78,000 posts in clc all saying don't do this, it's a bad idea?
Nov 14 '05 #7

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

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
46
by: Matt Gostick | last post by:
Well, it's not useless, it just doesn't seem to be working for me. Basically, when I'm returning a pointer from a function, free does not seem to be freeing the memory for me. Here is an example...
17
by: kj | last post by:
How can one test if a pointer has been "freed" (i.e. with free())? My naive assumption was that such a pointer would equal NULL, but not so. Thanks, kj -- NOTE: In my address everything...
8
by: Campano, Troy | last post by:
Hello, I'm having a problem with timestamps in postgresql. I run the following query to pull dates in this format: WEEK/MONTH/YEAR However the data says that April 28th 2004 was in week 4 of...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
41
by: SkyBlue | last post by:
Hi, can someone explain why the following simple C code segfaulted? I've stared it for 30mins but couldn't find the problem. Thx in advance #include <stdio.h> int main() { char *one; char...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
76
by: dbansal | last post by:
Hi group, I have a question to ask you all. I have allocated some chunk of memory using ptr=(int*)malloc(). now I am trying to free that memory using free((void*)ptr). My question is does free()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.