Re: pointer/allocation question

From: Jack Klein (jackklein_at_spamcop.net)
Date: 11/24/03


Date: Mon, 24 Nov 2003 04:18:19 GMT

On Mon, 24 Nov 2003 04:00:59 GMT, Jules <jules@nospam.com> wrote in
alt.comp.lang.learn.c-c++:

> The answer will be obvious to an experienced C/C++ developer, but it's
> got me stumped.
>
> I would expect that after calling allocate(), 'list' would point to an
> allocated array of 5000 bytes.
>
> Why doesn't this work as I want it to?
>
>
>
> void allocate(int *array, int length)
                     ^^^^^

Remember, C and C++ pass BY VALUE. This function receives a copy of
the value of the pointer list in main(). Since array in main() has
not been initialized, it is actually undefined behavior to do anything
at all with its value, even pass it to a function, although this
usually does not cause problems on most implementations unless you
actually try to dereference the pointer.

> {
> array=(int *)malloc(length);

Always check the value returned by malloc() to see if the allocation
succeeded. If it did not, you have a null pointer, and using that is
undefined behavior, and of a type that does cause problems on most
implementations.

In any case, even if the malloc() succeeded, you have put the returned
pointer in the function's local COPY of array. Due to pass by value,
you have not changed the pointer named "array" in main() at all.

>
> return;
> }
>
> int main(void)
> {
> int *list;
>
> allocate(list,5000);
>
> return 0;
> }
>
>
>
> - Thanks

You need:

#include <stdlib.h>

...and then either:

void allocate(int **array, int length)
{
         *array = malloc(length);

/* don't cast the return value of malloc() in C */
/* and if your programming in C++, use new[], */
/* not malloc */

         return;
}

...called like this from main():

   allocate(&list, 5000);

...or:

int *allocate(int length)
{
    int *temp_ptr = malloc(length);
    return temp_ptr;
}

...and call like this from main():

   list = allocate(5000);

Other notes: C and C++ provide a perfectly good type specifically
designed to be used for the sizes of objects, size_t. It would be
preferable to use that instead of int.

main() needs to check the pointer for NULL after the allocation, and
not do anything with it if it is. main() should also free() the
memory before the program ends.

Finally, if you fix the program it will allocate 5000 bytes of memory,
but on most systems that is not enough for 5000 ints. On most systems
5000 bytes of memory is only enough space to hold 2500 or 12500 ints.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


Relevant Pages

  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: object creation and naming at runtime?
    ... >>replaced with memory references by the compiler. ... I don't know where the computer will allocate space in ... memory for my int so I'll use a name for the address and let ... In the source code the pointer object (which will ...
    (alt.comp.lang.learn.c-cpp)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)

Loading