|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
hi, there,
I was asked such a question: how to determine the size of memory of the int pointer pointed? for example int GetTheSizeofMemory(int *buffer) { int size; //enter your code here, to calculate the memory size of buffer point to. return size; } we can not use sizeof(buffer) to get the value, how should we do? thanks. paul |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Paul wrote:
> hi, there, > > I was asked such a question: how to determine the size of memory of > the int > pointer pointed? for example > > int GetTheSizeofMemory(int *buffer) > { > int size; > //enter your code here, to calculate the memory size of buffer > point to. > return size; > } > we can not use sizeof(buffer) to get the value, how should we do? > thanks. > By remembering the size when the buffer is allocated. There is no other portable way. -- Ian Collins. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"Paul" <paul.pettus@gmail.com> wrote in message news:d741bbdf-a93c-436b-b6e6-afd8bdbf4889@w34g2000hsg.googlegroups.com... > hi, there, > > I was asked such a question: how to determine the size of memory of > the int > pointer pointed? for example > > int GetTheSizeofMemory(int *buffer) > { > int size; > //enter your code here, to calculate the memory size of buffer > point to. > return size; > } > we can not use sizeof(buffer) to get the value, how should we do? > thanks. > you know or you don't know. within the standard, nothing is provided for this. however, if you implement your own heap (or something similar), you can also provide your own means of determining object sizes... > paul |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Paul wrote:
> hi, there, > > I was asked such a question: how to determine the size of memory of > the int pointer pointed? By storing the size you specified when initialising the pointer to a block of memory. > for example > > int GetTheSizeofMemory(int *buffer) > { > int size; > //enter your code here, to calculate the memory size of buffer > point to. > return size; > } > we can not use sizeof(buffer) to get the value, how should we do? > thanks. Some platforms have special routines for this purpose but from a Standard C point of view there is no other way than to manually remember the size. You can also implement a wrapper on top of *alloc() that maintains a table of pointer values and their associated size requests and provide a querying function to retrieve the data associated with a valid pointer value. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Paul ha scritto:
> I was asked such a question: how to determine the size of memory of > the int pointer pointed? for example > > int GetTheSizeofMemory(int *buffer) > ... > we can not use sizeof(buffer) to get the value, how should we do? As others have already answered, you can't and one option is to write your own malloc/free functions. I've never really done it, but I've always thought that, in case I need them, I would have implemented something like this: void *mymalloc(size_t sz) { size_t *p; p = malloc(sz + sizeof(size_t)); if (p) { *p = sz; return p + 1; } return NULL; } void myfree(void *p) { free(((size_t *)p)-1); } size_t myblocksize(void *p) { return ((size_t *)p)[-1]; } I can imagine all weird things happening if pointers obtained with malloc get passed to these functions, but avoiding to pass invalid pointer to functions is among the first things a C programmer learns. I understood that some malloc() implementation behaves this way, storing the relevant information just before the pointer returned. What I've never investigated is if there are issues with alignment, portability or any other drawback I might have missed. Remo.D |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"Remo D." wrote:
[... keep track of malloc'ed size ...] > I've never really done it, but I've always thought that, in case I need > them, I would have implemented something like this: > > void *mymalloc(size_t sz) > { > size_t *p; > > p = malloc(sz + sizeof(size_t)); > if (p) { > *p = sz; > return p + 1; > } > return NULL; > } [...] > What I've never investigated is if there are issues with alignment, > portability or any other drawback I might have missed. Anything that has a stricter alignment requirement than size_t will cause this to fail. Consider a not-too-unlikely scenario where size_t has 4-byte alignment and double has 8-byte alignment. The following will do "bad things"[tm]: double *mypt = mymalloc(10 * sizeof(*mypt)); if ( mypt != NULL ) mypt[0] = 1.23; You need to know the strictest alignment requirement, and use that in your wrapper. And there is no portable way to know what is the strictest alignment requirement. The closest you could do would be create a union containing every type that you might malloc, and hope you never call it for something stricter. Or, you could make a #define in a header file and document "make sure you set this properly, or else." -- +-------------------------+--------------------+-----------------------+ | Kenneth J. Brody | www.hvcomputer.com | #include | | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> | +-------------------------+--------------------+-----------------------+ Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com> |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Kenneth Brody ha scritto:
> "Remo D." wrote: > [... keep track of malloc'ed size ...] >> I've never really done it, but I've always thought that, in case I need >> them, I would have implemented something like this: >> > [...] >> What I've never investigated is if there are issues with alignment, >> portability or any other drawback I might have missed. > > Anything that has a stricter alignment requirement than size_t > will cause this to fail. Consider a not-too-unlikely scenario > where size_t has 4-byte alignment and double has 8-byte alignment. I see! Thanks, I'll keep it in mind should I use somthing similar. Remo.D |
|
![]() |
| Outils de la discussion | |
|
|