Re: [Dev-C++] Correct use of pointers II
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-03-15 23:03:00
|
Yes, it's technically correct, but normally when allocating memory you
should check that the allocattion succeeded, i.e. that p wasn't set to
NULL.
On the other hand, malloc/free are extremely expensive, and I can't really
se why anyone would need to allocate a single integer. Since the integer
often have the same size as the pointer, while the memory allocation
library will need to keep some book-keeping information for each
allocation, it's a very strange thing to do.
Since you need one pointer for every int* you allocate, you might use
integers directly instead, and save a huge amount of time and memory.
If you don't need how many elements you need to sture until the program is
run, it's still no use to allocate single integers. Allocate an array of
integers instead.
/Per W
On Sat, 15 Mar 2003, Andr=E9 Mac=E1rio Barros wrote:
> Gentlemen,
>
> 1. In a generic point of view, can I use this code? (Y/N)
> 2. Would it be technically correct? (Y/N)
>
> #include <stdlib.h>
>
> int main()
> {
> int a=3D3, b=3D2, *p;
>
> p=3D(int *)malloc(1*sizeof(int));
> *p =3D a + b;
>
> /* .... */
>
> free(p);
> }
>
> Thanks
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by:Crypto Challenge is now open!
> Get cracking and register here for some mind boggling fun and
> the chance of winning an Apple iPod:
> http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|