PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > how to determine the size of memory pointed by a int pointer?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
how to determine the size of memory pointed by a int pointer?

Réponse
 
LinkBack Outils de la discussion
Vieux 29/11/2007, 04h15   #1
Paul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to determine the size of memory pointed by a int pointer?

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
  Réponse avec citation
Vieux 29/11/2007, 04h26   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to determine the size of memory pointed by a int pointer?

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.
  Réponse avec citation
Vieux 29/11/2007, 05h55   #3
cr88192
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to determine the size of memory pointed by a int pointer?


"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



  Réponse avec citation
Vieux 29/11/2007, 07h23   #4
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to determine the size of memory pointed by a int pointer?

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.

  Réponse avec citation
Vieux 29/11/2007, 08h41   #5
Remo D.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to determine the size of memory pointed by a int pointer?

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

  Réponse avec citation
Vieux 29/11/2007, 15h34   #6
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to determine the size of memory pointed by a int pointer?

"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>

  Réponse avec citation
Vieux 29/11/2007, 21h55   #7
Remo D.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to determine the size of memory pointed by a int pointer?

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15087 seconds with 15 queries