Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > calloc / free

Thread Tools

calloc / free

Micah Cowan
Posts: n/a
Oodini <(E-Mail Removed)> writes:

> Hello,
> I create a pointer, get some memory with calloc, but when I want to
> free it, I get a run-time error.
> Here is the code:
> char *script;
> script = scalloc(71,sizeof(char));

is that meant to be calloc()? Also, sizeof(char) is 1, by definition.

> script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
> setlinewidth\n0 setgray\n";

Was that line break in the code, or just wrapping from your
newsreader? Line breaks within string literals are not permitted.

At this point, whatever memory you allocated with calloc() is lost
forever: you are not changing the memory allocated, but setting script
to point at an entirely different region of memory: that occupied by
the string literal.

If you want to set the memory you allocated, use a standard C function
such as strcpy(), strncpy() or memcpy() to copy the contents of the
string literal into your newly allocated space. Something like:


where THE_STRING_LITERAL was #defined to be the literal you used.

When doing this, though, make sure you actually allocated enough space
to store the string literal (use sizeof THE_STRING_LITERAL again for
this). The 71 characters you currently have allocated is too short to
hold the all-important terminating null character ('\0').

> fprintf(image_file,script);
> fclose(image_file);
> // free(script);

an attempt to free script now will fail, because script no longer
points at dynamically allocated memory, but at a string literal.

> And if my questions does irritate you, please don't respond...

Most people here aren't irritated by questions; many of us are
irritated by failure to follow standard USENET procedure: read the
FAQ, and lurk a while before posting.

Reply With Quote
Micah Cowan
Posts: n/a
"bd" <(E-Mail Removed)> writes:

> On Fri, 27 Jun 2003 19:30:34 +0200, Oodini wrote:
> > Please don't laugh: I counted them.
> > To use strlen, I have to create a string.

> strlen("I am a string literal. I can be used in strlen") works.
> Also,
> const char *foo = "I am a string literal.";
> strlen(foo);
> works.

Provided you remember to add one for the null character.

Reply With Quote

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc/calloc free with double pointers luca C++ 5 03-02-2004 06:01 PM
Re: Override malloc,calloc,realloc and free? Dan Pop C Programming 2 06-27-2003 04:10 PM
Re: Override malloc,calloc,realloc and free? Douglas A. Gwyn C Programming 0 06-26-2003 06:19 PM
Re: Override malloc,calloc,realloc and free? Dan Pop C Programming 0 06-26-2003 04:52 PM
Re: Override malloc,calloc,realloc and free? Jun Woong C Programming 0 06-26-2003 03:49 PM