calloc / free

O

Oodini

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));
script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
setlinewidth\n0 setgray\n";
fprintf(image_file,script);
fclose(image_file);
// free(script);

image_file is defined elsewhere, and there isn't any problem with it.

Note: I read the FAQ at
http://www.eskimo.com/~scs/C-faq/top.html

Thanks for help.

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

Morris Dovey

Oodini said:
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));
script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
setlinewidth\n0 setgray\n";
fprintf(image_file,script);
fclose(image_file);
// free(script);

image_file is defined elsewhere, and there isn't any problem with it.

Oodini...

Note that when you assigned a pointer to the string "/cm... " to
script, you destroyed the pointer returned from calloc(). You
aren't, of course, entitled to free the space occupied by that
string literal.

<OT> Did you really mean "0.014 cm setlinewidth"? 0.014 pt might
as well be zero.
</OT>
 
N

Neil Cerutti

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));

What is scalloc?

I'm going to pretend you said malloc.
script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
setlinewidth\n0 setgray\n";

You have overwritten your pointer from malloc with a pointer to
a literal string. When you pass this pointer to free, worlds
collide.
 
O

Oodini

char *script;
What is scalloc?

A "safe calloc" function (it checks if calloc receives NULL).
You have overwritten your pointer from malloc with a pointer to
a literal string. When you pass this pointer to free, worlds
collide.

Actually, my goal was to put the string in the pointer.
I didn't thought it wil use an other memory space.

What do you suggest ?
Should I use only a string, and wait for its destruction at the end of
the function, or copy the string to the pojnter in an other way ?

Thanks.
 
O

Oodini

char *script;
Note that when you assigned a pointer to the string "/cm... " to script,
you destroyed the pointer returned from calloc(). You aren't, of course,
entitled to free the space occupied by that string literal.

That was the point. :)
Thanks.
 
O

Oodini

char *script;
assuming calloc(), you have initialized this pointer with the value returned
by calloc() which is the address of a freshly allocated block. All bits are
set to zero. Note that sizeof(char) is 1 by-definition. How did you specify
71? Isn't it a better way of doing it? I mean automagically (strlen, sizeof
etc.)

Please don't laugh: I counted them. :)
To use strlen, I have to create a string.
But my goal was to create a pointer to free it later.
Not really needed (a string would be OK).
But may I free a string ?? I don't think so...
The value passed to free() must be exactly the one you got from malloc() (or
one of its brothers). You have changed it, creating a memory leak, hence the
undefined behaviour.

OK. i got it now. Thanks.
 
N

Neil Cerutti

A "safe calloc" function (it checks if calloc receives NULL).


Actually, my goal was to put the string in the pointer. I
didn't thought it wil use an other memory space.

You can only store the address of things in pointers. You can't
store anything in pointers except pointers.
What do you suggest ?

Copy the string into the storage pointed to by script using
strcpy.

strcpy(script, "/cm {72 mul 2.54 div}...");
 
B

bd

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));

What is this 'scalloc'?
script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014

Strings must be on one line. I'll assume your newsreader broke this line.
setlinewidth\n0 setgray\n";

You don't copy strings like that. You just lost the memory you allocated
(literally!)

You meant:

strcpy(script, "...");

Also, you need to allocate one extra character, for the terminating null.
Here's how I'd do it:
const char *orig = "/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014 "
"setlinewidth\n0 setgray\n";
char *script;
script = malloc(strlen(orig) + 1);
if(!script){
fprintf(stderr, "Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
strcpy(script, orig);
fprintf(image_file,script);
fclose(image_file);
// free(script);

Why'd you copy it? Just use:
fprintf(image_file, "...");
and get rid of the script variable.
 
B

bd

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.
But my goal was to create a pointer to free it later.
Not really needed (a string would be OK).
But may I free a string ?? I don't think so...

Nope, but there's no reason to. Your memory usage increases if you
manually allocate somewhere to put a copy - string literals are never
freed.
 
B

bd

A "safe calloc" function (it checks if calloc receives NULL).


Actually, my goal was to put the string in the pointer.
I didn't thought it wil use an other memory space.

What do you suggest ?
Should I use only a string, and wait for its destruction at the end of
the function, or copy the string to the pojnter in an other way ?

The correct way to copy a string is using strcpy:
strcpy(destination, source);

There must be room for strlen(source) + 1 chars in destination.
 
M

Micah Cowan

Oodini said:
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:

memcpy(script,THE_STRING_LITERAL,sizeof THE_STRING_LITERAL);

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.

-Micah
 
M

Micah Cowan

bd said:
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.

-Micah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top