LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 01-01-2006, 01:04 PM   #1
con
Member
 
Registered: Jul 2005
Location: Finland
Distribution: Slackware
Posts: 213

Rep: Reputation: 30
Freeing memory allocated in a function


How do I free() *str in the code below.

Code:
#include <stdio.h>
#include <stdlib.h>

void func(char** p)
{
	*p = malloc(10*sizeof(char));
	*p = "Hello";
}

int main(void)
{
	char* str;
	
	func(&str);
	
	printf("%s World!\n", str);
	
	free(str);
	return 0;
}
If I compile and run it I get the following error
Code:
conny@fester:~$ ./test                                                      
Hello World!
*** glibc detected *** free(): invalid pointer: 0x08048584 ***
Aborted
I'm doing this on slack 10.2
 
Old 01-01-2006, 01:40 PM   #2
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
The problem here:
Code:
*p = "Hello"; /* strcpy(*p, "Hello"); ?*/
 
Old 01-01-2006, 02:03 PM   #3
con
Member
 
Registered: Jul 2005
Location: Finland
Distribution: Slackware
Posts: 213

Original Poster
Rep: Reputation: 30
Thank you! I've really got to cut down on the java....
 
Old 01-01-2006, 08:36 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,496

Rep: Reputation: 68
to make it a bit more understandable I wouldent allocate memory in a function but rather in the main program and pass the address to a function. then freeing it in the main program would make a little more sense. the way you got it is legal but a little confusing.
 
Old 01-02-2006, 04:25 AM   #5
con
Member
 
Registered: Jul 2005
Location: Finland
Distribution: Slackware
Posts: 213

Original Poster
Rep: Reputation: 30
The reason I was doing it like that was, I wanted to understand how the sqlite3_exec() functions works.

In the example in the docs it is used like this
Code:
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
  }
And I didnt understand how it could access the error message in zErrMsg when it wasnt returned from the function or anything.

But yes I would also do it like you said, which is alot less confusing. But then again, doing it like in the code in my first post should be very usefull.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Freeing Up Used Memory zoso Linux - Newbie 7 01-11-2005 10:44 PM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 03:56 AM
Boot Hangs with Redhat, GRUB, Total HugeTLB memory allocated, Neochubbz Linux - General 5 08-19-2004 02:27 PM
freeing memory in linux har5ha Linux - Software 7 07-23-2004 06:00 PM
Freeing up Memory DigiCrime Linux - General 5 09-30-2003 08:25 AM


All times are GMT -5. The time now is 07:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration