Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

calloc / free

 
 
Oodini
Guest
Posts: n/a
 
      06-27-2003
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...

 
Reply With Quote
 
 
 
 
Morris Dovey
Guest
Posts: n/a
 
      06-27-2003
Oodini wrote:
> 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>

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      06-27-2003
In article <>, Oodini wrote:
> 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.

--
Neil Cerutti
 
Reply With Quote
 
Oodini
Guest
Posts: n/a
 
      06-27-2003
>>char *script;
>>script = scalloc(71,sizeof(char));

>
> What is scalloc?


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

>>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.


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.

 
Reply With Quote
 
Oodini
Guest
Posts: n/a
 
      06-27-2003
>> char *script;
>> script = scalloc(71,sizeof(char));
>> script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014


> 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.

 
Reply With Quote
 
Oodini
Guest
Posts: n/a
 
      06-27-2003
>>char *script;
>>script = scalloc(71,sizeof(char));



> 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.

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      06-27-2003
In article <>, Oodini wrote:
>>>char *script;
>>>script = scalloc(71,sizeof(char));

>>
>> What is scalloc?

>
> A "safe calloc" function (it checks if calloc receives NULL).
>
>>>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.

>
> 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}...");

--
Neil Cerutti
 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      06-27-2003
On Fri, 27 Jun 2003 17:25:46 +0200, Oodini wrote:

> 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.

--
Freenet distribution not available
Teutonic:
Not enough gin.

 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      06-27-2003
On Fri, 27 Jun 2003 19:30:34 +0200, Oodini wrote:

>>>char *script;
>>>script = scalloc(71,sizeof(char));

>
>
>> 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.


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.

>
>> 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.


--
Freenet distribution not available
There's no saint like a reformed sinner.

 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      06-27-2003
On Fri, 27 Jun 2003 19:26:29 +0200, Oodini wrote:

>>>char *script;
>>>script = scalloc(71,sizeof(char));

>>
>> What is scalloc?

>
> A "safe calloc" function (it checks if calloc receives NULL).
>
>>>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.

>
> 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.

--
Freenet distribution not available
As he had feared, his orders had been forgotten and everyone had brought
the potato salad.

 
Reply With Quote
 
 
 
Reply

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



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57