LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2003, 09:25 AM   #1
registering
Member
 
Registered: Jun 2003
Location: Florida, USA
Distribution: Drake 10.1 Download
Posts: 182

Rep: Reputation: 30
Is my malloc/free thinking correct?


Hi all,
I had a bug in my application that I THINK I fixed but I want to propose
my theory and see if it's correct.

What I was doing was this:

1) allocate memory via malloc() for var1 (char*)
2) allocate memory via malloc() for var2 (char*)
3) var1 points to a string "house"
4) var2 = strstr(var1, "s") so now var2 points to the "s" contained in var1
5) free (var1)
6) free (var2)

Step 6 was creating a SIGABRT from free().

My hypothesis:
Since var2 is pointing to var1 I never need to free var2 as long as I free var1,
since it is not a copy, but rather a pointer to the same memory allocated for var1.
Thus, when I free var1, var2's memory is also freed, since they're essentially the
same.

This means I also never need to malloc() for var2, since it will only point to
somewhere within var1, or NULL.

Assuming the above, my app seems to work fine, but I was hoping someone could
verify my thinking or correct it since there might be a bug lurking.

Thanks.
 
Old 06-18-2003, 10:17 AM   #2
tda
Member
 
Registered: Mar 2002
Location: Ukraine, Kiev
Distribution: Mandrake 7.2
Posts: 37

Rep: Reputation: 15
Thumbs up

all right
 
Old 06-18-2003, 10:21 AM   #3
registering
Member
 
Registered: Jun 2003
Location: Florida, USA
Distribution: Drake 10.1 Download
Posts: 182

Original Poster
Rep: Reputation: 30
Talking

Cool beans, thanks!
 
Old 06-18-2003, 05:35 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
3) var1 points to a string "house"
What do you mean exactly by this?
If "house" is a constant string (i.e. you are not going to change it), say, something like this:
Code:
char *var1;
var1 = "house";
var2 = strstr(var1, "s");
puts(var2)
...then, that's it!. In this case you don't need any malloc, since the string "house" will already allocate 6 char's in the executable itself.
 
Old 06-18-2003, 05:50 PM   #5
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Or else you could do this and be able to free the memory:

char * var1 = malloc (6);
strcpy ( var1, "house");
char * var2 = strstr(var1,"s");
free(var1);
var2 = 0; //NULL

Last edited by GtkUser; 06-18-2003 at 05:52 PM.
 
Old 06-18-2003, 11:09 PM   #6
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
You r right. strstr() only returns the pointer to the location where 's' is present within the string. It does not copy anything to var2.

The scenario where u were allocating memory to var2 and then using var2 to store return value from strstr() will cause memory leaks because actual pointer to the memory allocated to the var2 will be lost.
 
Old 06-18-2003, 11:35 PM   #7
jamestwok
LQ Newbie
 
Registered: Oct 2002
Location: New Zealand
Distribution: Redhat 9.0
Posts: 14

Rep: Reputation: 0
#include <stdio.h>

main()
{
char *v1, *v2;

v1=(char*)malloc((strlen("house")+1)*sizeof(char));

v2=strstr(v1,"s");

free(v1);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
*** glibc detected *** malloc() / free()/ double RohanShrivastav Programming 12 10-01-2012 10:08 AM
thinking of converting from linux to Free BSD berrance General 9 12-13-2004 08:28 AM
how does malloc() and free() functions work? kuna Programming 8 12-04-2004 03:59 AM
malloc/free in C h/w Programming 12 02-26-2004 01:13 PM
malloc/free and segfault - advanced question iTux Programming 3 12-10-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration