Re: How to free dynamically allocated array?
- From: "Markus" <no@xxxxxxxxx>
- Date: Wed, 31 Aug 2005 10:10:29 +0300
"Flash Gordon" <spam@xxxxxxxxxxxxxxxxxx> wrote in message
news:h86eu2xmpm.ln2@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Krishanu Debnath wrote:
>> Markus wrote:
>>
>>>Hi,
>>>
>>>I have read the FAQ 6.16 on how to dynamically allocate a
>>>two-dimensional array.
>>>
>>>I do it like this:
>>>
>>>double** A = (double **)malloc(m * sizeof(double *));
>
> This is not the preferred form and is error prone. Try
> double** A = malloc(m * sizeof *A);
>
> Reasons for this include:
> Getting rid of the cast means that all compilers (including C89
> compilers are *required* to produce a diagnostic if you forget to
> include stdlib.h
>
> By only specifying the type once you only have to get it right once
> rather than in the three places you specified type. Think of the risk
> of miscounting the *s if you did a 5D array!
>
> It is less to type.
>
> It is less to read
>
> The FAQ at http://www.eskimo.com/~scs/C-faq/q6.16.html is out of date in
> this.
>
> Also you need to check if malloc succeeded before you use the space you
> hope was allocated. After all, where will the code below write if malloc
> failed?
Thx a lot for your kind advice. Is this proper way to test for malloc
failing?
if(A == NULL)
printf("\nmalloc failed!");
You see I am not good with pointer arithmetic...
markus
.
- References:
- How to free dynamically allocated array?
- From: Markus
- Re: How to free dynamically allocated array?
- From: Krishanu Debnath
- Re: How to free dynamically allocated array?
- From: Flash Gordon
- How to free dynamically allocated array?
- Prev by Date: Re: Greater sensitivity to off-topic than in other comp.lang
- Next by Date: Re: low-level question
- Previous by thread: Re: How to free dynamically allocated array?
- Next by thread: Re: How to free dynamically allocated array?
- Index(es):
Relevant Pages
|