LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-01-2004, 03:01 AM   #1
warney_out
LQ Newbie
 
Registered: Sep 2004
Posts: 10

Rep: Reputation: 0
freeing variables


i have the following variable
char text1[30]=" ";
char text2[30]=":";
then after putting some values in text1 and text2 i do
strcat(text1,text2);
then i need to clear text1 and text2 i tried the free command but it gave me a segmentation error....what is wrong??
 
Old 10-01-2004, 04:34 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Only use free() when you have assinged memory to a pointer by calling malloc() or calloc(). And that's not what you are doing in your example. So free() won't work in your case and will generate segmentation faults.

A segmentation fault occurs when a program accesses (or free's) memory it should access (or free).

What do you mean by "clearing" text1 and text2:
  • Just make the strings empty?
    Then just do:
    Code:
    text[0] = '\0';
    text[0] = '\0';
  • Make them disappear from memory? Just don't:
    If they are global variables (declared outside any function) you just cannot. If they are declared inside a function, they will be free when that function returns.

Last edited by Hko; 10-01-2004 at 04:36 AM.
 
Old 10-01-2004, 07:34 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
The variables used in your program are allocated on the stack (program memory) and not on the heap (dynamic data memory). Therefore there is no need to free the memory as they are not technically on the 'allocated' memory or heap.
 
  


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 09:44 PM
Freeing swap LinFreak! Slackware 2 12-13-2003 08:48 AM
Freeing up Space stroudt Linux - General 2 08-28-2003 03:14 PM
Freeing up Space stroudt Red Hat 6 08-28-2003 02:45 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

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

All times are GMT -5. The time now is 07:51 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