how to free this

C

copx

What is the correct way to free arrays allocated like this (from C FAQ):int
**array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;
 
E

Eric Sosman

copx said:
What is the correct way to free arrays allocated like this (from C FAQ):int
**array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;


I'm surprised to see those unnecessary casts in a FAQ
answer. <checks> Strange: they're not there in the FAQ.
Where do you suppose they came from?

Anyhow:

free(array2[0]);
free(array2);

The general rule is "One malloc, one free" (with suitable
alterations if you use calloc and/or realloc).
 
C

copx

Eric Sosman said:
copx said:
What is the correct way to free arrays allocated like this (from C
FAQ):int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;


I'm surprised to see those unnecessary casts in a FAQ
answer. <checks> Strange: they're not there in the FAQ.
Where do you suppose they came from?


They were in the version of the FAQ I read. The first google hit for CLC FAQ
is:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/

See here:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/q6.16.html

Anyhow:

free(array2[0]);
free(array2);

The general rule is "One malloc, one free" (with suitable
alterations if you use calloc and/or realloc).

I know, but the pointer arithmetic (which I almost never use myself) scared
me, so I asked just to be sure.
 
A

Al Balmer

Eric Sosman said:
copx said:
What is the correct way to free arrays allocated like this (from C
FAQ):int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;


I'm surprised to see those unnecessary casts in a FAQ
answer. <checks> Strange: they're not there in the FAQ.
Where do you suppose they came from?


They were in the version of the FAQ I read. The first google hit for CLC FAQ
is:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/

See here:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/q6.16.html

Dunno what this is. I suggest you use the official one:
http://c-faq.com/
 
V

vippstar

Eric Sosman said:
copx wrote:
What is the correct way to free arrays allocated like this (from C
FAQ):int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;
I'm surprised to see those unnecessary casts in a FAQ
answer. <checks> Strange: they're not there in the FAQ.
Where do you suppose they came from?

They were in the version of the FAQ I read. The first google hit for CLC FAQ
is:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/

Dunno what this is. I suggest you use the official one:http://c-faq.com/

It is mr Summits personal home page (or, at least it appears to be
so), he clearly states that this very version is out of date, and
suggests to use <http://c-faq.com/> instead.
 
K

Keith Thompson

copx said:
They were in the version of the FAQ I read. The first google hit for CLC FAQ
is:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/
[...]

That's surprising. A first hit for "comp.lang.c FAQ" is the correct
site, <http://c-faq.com/> (also accessible with the usual "www."
prefix if you prefer).

<http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/>
purports to be "Steve Summit's home page", but it appears to be just a
copy of it, and quite probably an unauthorized one. His actual home
page is <http://www.eskimo.com/~scs/> (in which context reference to
"The ISP hosting this page" makes a lot more sense). I'll drop Steve
a line and let him know about this.
 
R

Richard Heathfield

(e-mail address removed) said:

....which just demonstrates that you shouldn't always take the first hit as
gospel truth.
It is mr Summits personal home page (or, at least it appears to be
so),

Steve's home page is certainly not mandragor.org; it is
http://www.eskimo.com/~scs/ - and has been since at least 1999 and
probably long before.
 
F

Flash Gordon

Keith Thompson wrote, On 08/01/08 07:56:

page is <http://www.eskimo.com/~scs/> (in which context reference to
"The ISP hosting this page" makes a lot more sense). I'll drop Steve
a line and let him know about this.

We went round this buoy April last year, something you might want to
remind Steve of.
http://groups.google.com/group/comp...2/0d8bbc02a261d372?lnk=st&q=#0d8bbc02a261d372

My opinion is that the owner of mandragor.org does not care about being
either honest or accurate.
 
F

Flash Gordon

Keith Thompson wrote, On 08/01/08 07:56:

<http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/>
purports to be "Steve Summit's home page", but it appears to be just a
copy of it, and quite probably an unauthorized one. His actual home
page is <http://www.eskimo.com/~scs/> (in which context reference to
"The ISP hosting this page" makes a lot more sense). I'll drop Steve
a line and let him know about this.

I managed to contact the owner of mandragor.org and he has taken Steve's
home page and the FAQ down now, so we should have a bit less confusion.
I don't know if it was abuse@, postmaster@, the contact address he gave
or the technical contacts his whois records provided that reached him,
but something did.
 
B

Barry Schwarz

What is the correct way to free arrays allocated like this (from C FAQ):int
**array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;


Only two pointers contain values provided by malloc. Those are the
only two values you can legally pass to free. The order in which you
free them should be obvious.

You should use www.c-faq.com for the current version.


Remove del for email
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top