LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-27-2004, 08:39 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
About allocated memory in C++


I use a lot of strings and need to make my code efficient with memory.

The first thing is how is the best way to use strings when you don't now the size of it or when you need to use pointer so you can use as return value?

The second thing. Lets say I have a function that allocates memory, fills it with a string and returns a pointer to that memory.

The calling function uses this function as argument in something like strcmp()? How can I now free that memory after I am done?
 
Old 12-27-2004, 09:12 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
What you describing is C strings. If your going to be using unknow size strings all over your code in C++ I suggest you use the string class thats in the STL.

Belows an example.

Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string str1, str2, str3, str4;
   str1 = "The First String";
   str2 = "The Second String";

   str3 = str1 + " " + str2;
   cout << "String 3:" << str3 << endl;;

   str4 = str1;
   cout << "String 4:" << str1 << endl;

   if( str1 == str4 )
      cout << "String 1 is equal to string 4" << endl;
   else
      cout << "String 1 is not equal to string 4" << endl;

   str3 = str2;
   if( str1 == str3 )
      cout << "String 1 is equal to string 3" << endl;
   else
     cout << "String 1 is not equal to string 3" << endl;
};

Last edited by leonscape; 12-27-2004 at 09:14 PM.
 
Old 12-28-2004, 02:56 AM   #3
suowei1979
LQ Newbie
 
Registered: Sep 2004
Location: BeiJing,China
Distribution: Red Hat
Posts: 25

Rep: Reputation: 16
thx
 
  


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
When is the user stack allocated? moadon Programming 1 04-11-2005 11:28 PM
allocated space of a directory Dave31836 Programming 8 10-04-2004 06:07 PM
Boot Hangs with Redhat, GRUB, Total HugeTLB memory allocated, Neochubbz Linux - General 5 08-19-2004 01:27 PM
UT2004 - Allocated Only 32M AGP! Phorem Linux - Games 0 04-16-2004 03:16 PM
how to get my ip address allocated by dhcp on dsl paledread Linux - Networking 2 03-09-2004 06:50 AM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
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