Re: Can array[]=malloc()ed?

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 10/11/03


Date: 11 Oct 2003 17:17:26 GMT

On 11 Oct 2003 00:29:03 -0700, dam_fool_2003@yahoo.com wrote:

>My god,
> Each element in the array is an int but not pointer to an int. If we
>malloc the first element it will return a chunk of memory, which is a
>pointer. Such a simple concept!
> Some times my brain does not work correctly and gives UB!!
>
> Can we allocate a array of pointer with malloc's by the following
>method?

You can allocate an array of pointer but not by the method you wrote

>int main(void)
>{
>char *a = "dam",

a is a pointer to char initialized to point to a string literal.

> *b="fool",

b is also a pointer to char initialized to point to a different string
literal.

> *c = "2003",

Ditto for c.

> *d[3]={a,b,c};

d is an array of 3 pointer to char where each element of the array is
initialized to point to a different one of the three string literals
defined above. Of course this is a syntax error since the
initialization value must be a constant and not the value currently
contained in another variable. But then, you don't test most of your
code before you post it anyway. It turns out you don't use these
values so the initialization is irrelevant.

> unsigned int i;
> for(i = 0;i<3 ;i++)
> {
> d[i] = NULL;

You also don't use this value but at least it is syntactically
correct, assuming you remembered to #include stdlib.h or one of the
other standard headers that defines NULL.

> d[i] = malloc(sizeof **d);

d is an array of pointer to char. *d is a pointer to char. **d is a
char. sizeof **d must be 1. Assuming you #include stdlib.h, d[i] now
points to an area capable of holding exactly one char. On most
systems, this will be incapable of holding anything else. Did you
mean to include some multiplier as part of the argument to allow d[i]
to point to the first element of an array?

> if(**d == NULL)

A wonderful example of using gibberish to produce both syntax and
logic errors.

d is an array of pointers. *d is equivalent to *(d+0) which is
defined to be the same as d[0], the first pointer in the array. **d
is identical to *(*d) which, by substitution, is *(d[0]) which, by the
same logic, is the same as d[0][0]. Thus **d is the first character
pointed to by the first pointer in the array d.

     At this point, since you haven't checked the return from malloc,
you don't know if this pointer points anywhere. The appearance of the
code suggests that you were attempting to test the return from malloc
but decided to get unnecessarily fancy. The return from malloc was
stored in d[i]. If you want to test that return for success, test
d[i]. Where did you come up with **d?

     Even if you had used *d, which would have been syntactically,
correct, it would have produced the correct evaluation only for i=0.
The remaining iterations of the loop would have checked d[0] each time
instead of the d[i] in which the result was stored.

On my system, NULL is defined as a pointer to void. **d is a char.
You cannot compare a pointer and a char. The compiler is required to
produce a diagnostic. Oops, sorry, you don't check your code before
posting.

> {
> printf("mem not allocated\n");

You need to #include stdio.h.

> exit(EXIT_FAILURE);
> }
> else
> printf("mem allocated\n");
> }
> return 0;
>}

One of the secrets to successful programming is to think about the
problem for a while before writing any code. Your question asked
about allocating an array of pointers. I think most of us expected
your code to attempt to allocate an array of pointers. You didn't
allocate space for any pointers. You didn't allocate space for any
arrays. You didn't address your objective at all.

What your code could be credited with attempting is to allocate
multiple areas whose addresses will be kept in an automatic (not
allocated) array of pointers. That is not the same goal.

If you look in the faq (http://www.eskimo.com/~scs/C-faq/top.html),
you will find examples of how to allocate an array of pointers and
then allocate space for each of those pointers to point to. I
deliberately omit the section reference in the hope that, while you
are there, you will read the whole thing.

<<Remove the del for email>>



Relevant Pages

  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Advice on how to return a list of values
    ... The caller passes a pointer to a previously allocated array of 64 ... My function allocates the array and returns it. ... well allocate that much space and be done with it. ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)
  • Re: Alignment requirements of LZO working memory
    ... The calculation done to compute the number of elements in the array ... same alignment question gets raised again if you allocate an ... One pointer to an allocated ... one trivial fixup and you get another pointer to an aligned ...
    (comp.compression)
  • Re: two dimensional arrays passed to functions
    ... > array and then send it down as a single dimmensional array. ... x is an array of char pointers. ... to a pointer to the first element of this array, ... need to copy the strings and not just assign pointers to them). ...
    (comp.lang.c)