473,387 Members | 1,572 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,387 software developers and data experts.

how to free this

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[i] = array2[0] + i * ncolumns;
Jan 7 '08 #1
8 1700
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[i] = 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).

--
Er*********@sun.com
Jan 7 '08 #2

"Eric Sosman" <Er*********@sun.comschrieb im Newsbeitrag
news:1199742719.546282@news1nwk...
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[i] = 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/Prog.../C/clc_faq_en/

See here:
http://docs.mandragor.org/files/Prog...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.

Jan 7 '08 #3
On Tue, 8 Jan 2008 00:36:22 +0100, "copx" <co**@gazeta.plwrote:
>
"Eric Sosman" <Er*********@sun.comschrieb im Newsbeitrag
news:1199742719.546282@news1nwk...
>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[i] = 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/Prog.../C/clc_faq_en/

See here:
http://docs.mandragor.org/files/Prog...faq/q6.16.html
Dunno what this is. I suggest you use the official one:
http://c-faq.com/
--
Al Balmer
Sun City, AZ
Jan 8 '08 #4
On Jan 8, 2:05 am, Al Balmer <albal...@att.netwrote:
On Tue, 8 Jan 2008 00:36:22 +0100, "copx" <c...@gazeta.plwrote:
"Eric Sosman" <Eric.Sos...@sun.comschrieb im Newsbeitrag
news:1199742719.546282@news1nwk...
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[i] = 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/Prog.../C/clc_faq_en/
See here:
http://docs.mandragor.org/files/Prog.../clc_faq_en/C-...

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.
Jan 8 '08 #5
"copx" <co**@gazeta.plwrites:
[...]
They were in the version of the FAQ I read. The first google hit for CLC FAQ
is:
http://docs.mandragor.org/files/Prog.../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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 8 '08 #6
vi******@gmail.com said:
On Jan 8, 2:05 am, Al Balmer <albal...@att.netwrote:
>On Tue, 8 Jan 2008 00:36:22 +0100, "copx" <c...@gazeta.plwrote:
<snip>
>The first google hit for CLC FAQ is:
http://docs.mandragor.org/files/Prog.../C/clc_faq_en/
....which just demonstrates that you shouldn't always take the first hit as
gospel truth.
>>
>See here:
http://docs.mandragor.org/files/Prog.../clc_faq_en/C-...
>>
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),
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 8 '08 #7
Keith Thompson wrote, On 08/01/08 07:56:

<snip mandragor.org>
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....8bbc02a261d372

My opinion is that the owner of mandragor.org does not care about being
either honest or accurate.
--
Flash Gordon
Jan 8 '08 #8
Keith Thompson wrote, On 08/01/08 07:56:

<snip>
<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.
--
Flash Gordon
Jan 8 '08 #9

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

Similar topics

9
by: zerro | last post by:
Hello, I try to understand heap overflows (under Linux), but can not understand one thing with freeing memory allocated with malloc(). Here it comes: I have a program called 1.c: main() {...
16
by: Peter Ammon | last post by:
Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
46
by: Matt Gostick | last post by:
Well, it's not useless, it just doesn't seem to be working for me. Basically, when I'm returning a pointer from a function, free does not seem to be freeing the memory for me. Here is an example...
11
by: vjay | last post by:
Can someone help me understanding how free call works.how the free call will know that how many bytes has to free when i send pointer to that call?
13
by: santosh | last post by:
Hi, If I call free() with a uninitialised pointer, will the program state become undefined, or will free() return harmlessly? Incidentally, is there a way in Standard C to determine weather a...
13
by: lovecreatesbeauty | last post by:
/* How do free() know how many elements should be freed in a dynamic array? When free a single variable, the amount of byte of memory can be retrieved from the type of variable itself. ...
7
by: Jack | last post by:
how does free know how much of memory to release if I say free(ptr)? Thanks.
5
by: Michael | last post by:
Hi, if I have declared: typedef struct something_big_struct Something_Big; struct something_big_struct{ <lots of stuff> Something_Big *a_big_thing; };
13
by: Chad | last post by:
Excercise 6-5 Write a functon undef that will remove a name and defintion from the table maintained by lookup and install. Code: unsigned hash(char *s); void undef(char *s)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.