Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-27-2004, 09:39 PM   #1
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
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, 10:12 PM   #2
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.

#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;
      cout << "String 1 is not equal to string 4" << endl;

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

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

Rep: Reputation: 16


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

All times are GMT -5. The time now is 10:57 AM.

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