473,324 Members | 2,511 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,324 software developers and data experts.

free() an empty string ?

Hi there,

I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}

What's exactly going on in main memory here?

Thanks,
Cornel
Nov 13 '05 #1
6 16162
c.*****@bluewin.ch wrote:
cpStr = "";
free(cpStr);


Free() should only be used to free memory allocated with malloc(),
realloc(), or calloc(). Or a function which uses them of course, like
the common strdup() function.

malloc() & co are for the _programmer_ to allocate memory at
run-time, so the programmer must clean up afterwards with free().

Other kinds of memory (variables, string literals like "" or "foo")
are managed implicitly by the run-time system.

--
Hallvard
Nov 13 '05 #2
In article <16**************************@posting.google.com >,
c.*****@bluewin.ch wrote:
I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}

What's exactly going on in main memory here?


Noone can say what's going on in main memory, but you're
freeing memory that was never allocated.

Don't do that.

Every call to malloc() should be accompanied by a call to free()
somewhere else in the code, and vice versa. I.e., free what you
allocate, no more, no less.

--
Andreas Kähäri
Nov 13 '05 #3
On 15 Sep 2003 06:52:46 -0700
c.*****@bluewin.ch (c.*****@bluewin.ch) wrote:
Hi there,

I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}


You're free()ing a string literal. That invokes nasal demons.
--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #4
c.*****@bluewin.ch (c.*****@bluewin.ch) wrote:
I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}


Well, and so it should. You're not allocating any memory for cpStr. You
point it at a string literal, but you can't free string literals. You
can only call free() with a pointer to memory you have previously
received from malloc(), calloc() or realloc().

Richard
Nov 13 '05 #5
On Mon, 15 Sep 2003 06:52:46 -0700, c.*****@bluewin.ch wrote:
int main(int argc, char *argv[])
{
char *cpStr;

cpStr = ""; Here is your problem. free(cpStr);
return 0;

Make sure you return an integer for main.
}

What's exactly going on in main memory here?


No one knows what is going into to memory. Safe guess would be nothing at
all because your pointer points to unallocated memory. You need to
allocate some memory by using malloc.

free() frees the memory space pointed to by ptr, which must have been
returned by a previous call to malloc(), calloc() or realloc(). Other-
wise, or if free(ptr) has already been called before, undefined
behaviour occurs. If ptr is NULL, no operation is performed.

- Jake

Nov 13 '05 #6
On Mon, 15 Sep 2003 14:05:34 GMT, in comp.lang.c , Jake Roersma
<ja***@copiosus.net> wrote:
char *cpStr;

cpStr = "";

What's exactly going on in main memory here?
No one knows what is going into to memory. Safe guess would be nothing at
all because your pointer points to unallocated memory.


er, no it doesn't - he pointed it at a string literal containing one
char, the null character.
You need to allocate some memory by using malloc.


this is certainly true, if you plan to free() it.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #7

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

Similar topics

2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
8
by: Chris Potter | last post by:
Hello everyone. I have two questions both of which regard a homework assignment for my "Intro to C" class. The First question that i have is that my program segfaults when i free() memory that i...
11
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
14
by: cj | last post by:
What is string.empty used for? I can't say: if string.empty then I have to use: if string = "" then which is ok, I just want to know what .empty is for.
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
5
by: sriramsreenivasan | last post by:
I am a beginner in c i have written a linked list program and it shows a error after two successions please help me to find the error and to solveit #include<stdio.h> #include<stdlib.h>...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.