Re: pointer/allocation question
From: Jack Klein (jackklein_at_spamcop.net)
Date: 11/24/03
- Next message: Robert Stankowic: "Re: pointer/allocation question"
- Previous message: Russell Hanneken: "Re: pointer/allocation question"
- In reply to: Jules: "pointer/allocation question"
- Next in thread: Robert Stankowic: "Re: pointer/allocation question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Robert Stankowic: "Re: pointer/allocation question"
- Previous message: Russell Hanneken: "Re: pointer/allocation question"
- In reply to: Jules: "pointer/allocation question"
- Next in thread: Robert Stankowic: "Re: pointer/allocation question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|