473,378 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

freeing string literals?

Can you / are you supposed to free() string literals which are no longer
needed?

In my case I've menu construction code that looks like this:

menu_items = list_new();
list_add(menu_items, "Item 1");
list_add(menu_items, "Item 2");
list_add(menu_items, "Item 3");
menu_create(menu_items, false)

The boolean argument of menu_create() determines whether or not the entries
of the menu_items list (a generic linked-list) are free()ed on destruction.

AFAIK string literals decay into char pointers but aren't supposed to be
free()ed, however I'm not sure.
IIRC string literals remain in memory until program termination.
In my current project I've lots of string literals which are only
temporarily needed and I'm afraid of wasting lots of RAM...
Any ideas?


Nov 26 '05 #1
6 2695
copx wrote:
Can you / are you supposed to free() string literals which are no longer
needed?
No, and no.
In my case I've menu construction code that looks like this:

menu_items = list_new();
list_add(menu_items, "Item 1");
list_add(menu_items, "Item 2");
list_add(menu_items, "Item 3");
menu_create(menu_items, false)

The boolean argument of menu_create() determines whether or not the entries
of the menu_items list (a generic linked-list) are free()ed on destruction.

AFAIK string literals decay into char pointers but aren't supposed to be
free()ed, however I'm not sure.
Right. The general rule is: never free() memory that wasn't malloc()ed
or calloc()ed. Some functions may implicitly allocate memory for you,
but they're rare. String literals are never "allocated" in this way.
IIRC string literals remain in memory until program termination.
In my current project I've lots of string literals which are only
temporarily needed and I'm afraid of wasting lots of RAM...
Any ideas?

Well, that's obvious, innit? Allocate them dynamically and free them
when you don't need them anymore, or use a more advanced string library
with garbage collection.

Before you do this, however, check that you're actually right. Through
the miracle of virtual memory, string literals are often stored as data
that can be paged in on demand from the executable file itself as
necessary and automagically discarded from main memory when space gets
tight. This is unlikely to be a huge drain on system resources, though
it may make for some awkward loading times and a big virtual footprint
(but no increase in actually allocated virtual memory, if the on-demand
paging of readonly data works).

Allocating strings dynamically has fringe benefits, though. Dynamically
loading them has the advantage of allowing internationalization --
something that's hardly feasible with hard-coded string literals. Many
toolkits/frameworks have some form of resource management for this.

Moreover, the "menu creation" scheme you're employing is often handled
by resource files as well, because having to change the code when you
change the menu layout is a maintenance hurt.

S.
Nov 26 '05 #2
"copx" <in*****@invalid.com> wrote

AFAIK string literals decay into char pointers but aren't supposed to be
free()ed, however I'm not sure.
string literals are really a little convenience put into the C language.
Declaring one is the same as declaring an array of chars with a terminating
NUL.
You don't need to free arrays created in automatic or static memory, only
arrays created dynamically (with a call to malloc and family).
IIRC string literals remain in memory until program termination.
In my current project I've lots of string literals which are only
temporarily needed and I'm afraid of wasting lots of RAM...
Any ideas?

Somehow you need to get the character information into the computer.
However you could store all your strings in a file and read them in using
fgets(), discarding the buffer when you have done with the string.
Or you could devise some compression scheme where the strings are stored as
an array of unsigned chars (compressed data) and decompressed on the fly.
However add up your memory first. 1 MB is over 100,000 words of English,
about the same as an average novel.
Nov 26 '05 #3
copx wrote:
Can you / are you supposed to free() string literals which are no longer
needed?

In my case I've menu construction code that looks like this:

menu_items = list_new();
list_add(menu_items, "Item 1");
list_add(menu_items, "Item 2");
list_add(menu_items, "Item 3");
menu_create(menu_items, false)

The boolean argument of menu_create() determines whether or not the entries
of the menu_items list (a generic linked-list) are free()ed on destruction.

AFAIK string literals decay into char pointers but aren't supposed to be
free()ed, however I'm not sure.
IIRC string literals remain in memory until program termination.
In my current project I've lots of string literals which are only
temporarily needed and I'm afraid of wasting lots of RAM...
Any ideas?

copx. You obviously write better than you read. Go get your C book and
look up 'free()'.

Yes, string literals are part of your executable and remain until
program termination. However much memory these strings take, C doesn't
promise you can do anything but read them. Whether you can write them
are implementation specific and C is out of the picture. You definitely
cannot 'free()' them.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 26 '05 #4
Skarmander <in*****@dontmailme.com> writes:
[...]
Right. The general rule is: never free() memory that wasn't malloc()ed
or calloc()ed.


<QUIBBLE>Or realloc()ed.</QUIBBLE>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 26 '05 #5
Keith Thompson wrote:
Skarmander <in*****@dontmailme.com> writes:
[...]
Right. The general rule is: never free() memory that wasn't malloc()ed
or calloc()ed.

<QUIBBLE>Or realloc()ed.</QUIBBLE>

Right, right... never free() memory that wasn't (m|c|re)alloc()ed... but
it's just not quite as snappy.

S.
Nov 27 '05 #6
On 2005-11-27, Skarmander <in*****@dontmailme.com> wrote:
Keith Thompson wrote:
Skarmander <in*****@dontmailme.com> writes:
[...]
Right. The general rule is: never free() memory that wasn't malloc()ed
or calloc()ed.

<QUIBBLE>Or realloc()ed.</QUIBBLE>

Right, right... never free() memory that wasn't (m|c|re)alloc()ed... but
it's just not quite as snappy.


Or, for that matter, never free() memory that _was_ successfully
realloc()ed, for varying methods of verbification.
S.

Nov 28 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
8
by: junky_fellow | last post by:
what would be the output for the following piece of code ? if ( "hello" == "hello" ) printf("True\n"); else printf("False\n"); What is the reason for that ?
5
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
8
by: arnuld | last post by:
i tried to output these 2 to the std. output: const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; const std::string exclam = "!"; const std::string...
4
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello all, I'm learning C and I still am struggling to understand some basic concepts. For example, I read in the standard that with functions such as strcpy, 'If copying takes place between...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
7
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.