To: | |
---|---|
Subject: | Re: [ts-7000] malloc() |
From: | "Don W. Carr" <> |
Date: | Wed, 25 Oct 2006 09:54:19 -0500 |
No, all pointers are treated differently. With a void pointer, you can only copy bytes to or from that location using memcpy, or other functions. If you know what type you are going to store after calling malloc, you should cast it to that type. If you need to manipulate the memory and walk through, it is typical to cast it to a character pointer. With a void pointer, you can not add to it or increment it, since that the compiler does not know the size of what you are pointing to. An increment, will always increment by the size of what you are pointing to, as shown in the code below, ptr++ will increment the pointer by 4 bytes to point at the next floating point number that was allocated.
float *ptr; ptr = (float *) malloc(10 * sizeof(float)); ptr++; If you are going to use pointers, it is important that you take care with what you are doing, and you understand very well how they work. Also, try to isolate the code that does pointer manipulation so that you do not have pointer manipulation mixed throughout your code. Don. On 10/25/06, Jason Stahls <> wrote:
|
<Prev in Thread] | Current Thread | [Next in Thread> |
---|---|---|
|
Previous by Date: | Re: [ts-7000] malloc(), Jason Stahls |
---|---|
Next by Date: | [ts-7000] Re: malloc(), Phil |
Previous by Thread: | Re: [ts-7000] malloc(), Jason Stahls |
Next by Thread: | [ts-7000] Re: malloc(), Phil |
Indexes: | [Date] [Thread] [Top] [All Lists] |
Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU