Link to home
Start Free TrialLog in
Avatar of alex108
alex108

asked on

pointer and its size

Hi!
If I have a pointer void* p with memory allocated through
new, malloc, alloc.... How can I know the size of the memory block allocated for it without using functions like
_msize? It must be more platform independent.
Thanks.
Avatar of djbusychild
djbusychild

what's wrong with _msize ?
If you are assigning memory for an object, then you can get its size by knowing the size of each of its member variables.
   You can always use the sizeof function. And its platform independent.

Babu
how would you use sizeof on a void pointer that's passed into a function ?
You cannot, in general, know the size of a block of memory via a pointer to that block.  There is no mechanism in C++ for discovering this information.

The only standard mechanism is to pass around the size of the block.

> You can always use the sizeof function. And its platform independent.

This would of course only return the size of the pointer.
Avatar of Paul Maker
NO NO, sizeof will return the size of the pointer to the memory block. i dont know of a platform independant way of getting the size of a heap allocated block. i use _msize. you could always make you own struct that has

struct _ptr
{
  void *ptr;
  int size;
};

then use these instead of just void*

struct _ptr ptr;

ptr.size = 10;
ptr.ptr = malloc(ptr.size);

maybe
Avatar of alex108

ASKER

Exactly-I need to know hoe _msize works?
Regarding sizeof?
if I do the following:
Slightly off topic, but FYI mixing new and malloc in the same project is discouraged.
_msize returns the size of a heap allocated block of memory.
> Exactly-I need to know hoe _msize works?

It relies on system dependant behaviour.  Windows does keep track of the size of each block it allocates, and a function like this can query that information.

There is no standard mechanism for achieving this.  Even a function like _msize is not as useful as you would think.  It only works with the first element of a heap allocated array, so if you tried something like _msize(buffer+4) it would fail.
Avatar of alex108

ASKER

I can tell you something:
In one company I was interviewed (I am looking for job now
C++ programmer).So they told if you can solve the following problem then we can talk with you futher, otherwise go home.
////////////////////////////////////////

In some OS there is 2 functions:
void* malloc(int size);
free( void* p, int size);

Out of this 2 functions I have to make my own malloc and free, but free maust accept only void* p as asingle parameter.They say the solution is simple and few lines long. They didn't rebeal me. They said it's a secret. Up to now I don't know it. May be you know.
Seems to me it has to do something with memory allocation rules... Maybe...
For example how _msize works?
Thanks
Avatar of alex108

ASKER

I can tell you something:
In one company I was interviewed (I am looking for job now
C++ programmer).So they told if you can solve the following problem then we can talk with you futher, otherwise go home.
////////////////////////////////////////

In some OS there is 2 functions:
void* malloc(int size);
free( void* p, int size);

Out of this 2 functions I have to make my own malloc and free, but free maust accept only void* p as asingle parameter.They say the solution is simple and few lines long. They didn't rebeal me. They said it's a secret. Up to now I don't know it. May be you know.
Seems to me it has to do something with memory allocation rules... Maybe...
For example how _msize works?
Thanks
Avatar of alex108

ASKER

I can tell you something:
In one company I was interviewed (I am looking for job now
C++ programmer).So they told if you can solve the following problem then we can talk with you futher, otherwise go home.
////////////////////////////////////////

In some OS there is 2 functions:
void* malloc(int size);
free( void* p, int size);

Out of this 2 functions I have to make my own malloc and free, but free maust accept only void* p as asingle parameter.They say the solution is simple and few lines long. They didn't rebeal me. They said it's a secret. Up to now I don't know it. May be you know.
Seems to me it has to do something with memory allocation rules... Maybe...
For example how _msize works?
Thanks
Given your own malloc and free, it is possible, you could do something like:

void* malloc(int size)
{
   void* buffer = ::malloc(size+1*sizeof(void*));
   buffer[0] = (void *) size;
   return buffer + 1;
}

voif free(void* buffer)
{
  size_t size = *(buffer-1);
  ::free(buffer-1,size);
}
Avatar of alex108

ASKER

I can tell you something:
In one company I was interviewed (I am looking for job now
C++ programmer).So they told if you can solve the following problem then we can talk with you futher, otherwise go home.
////////////////////////////////////////

In some OS there is 2 functions:
void* malloc(int size);
free( void* p, int size);

Out of this 2 functions I have to make my own malloc and free, but free maust accept only void* p as asingle parameter.They say the solution is simple and few lines long. They didn't rebeal me. They said it's a secret. Up to now I don't know it. May be you know.
Seems to me it has to do something with memory allocation rules... Maybe...
For example how _msize works?
Thanks
Here is a tidied up version of that example:

#include <stdlib.h>

void* mymalloc(int size)
{
  size_t* buffer = (int *)::malloc(size+sizeof(int));
  buffer[0] = size;
  return (void *)(buffer + 1);
}

void myfree(void* mem)
{
 int* buffer = ((int *) mem) - 1;
 int size = buffer[0];
 ::free(buffer,size);
}
Avatar of alex108

ASKER

Ot,s hot, but not that. As soon as you write:
return buffer+1;
you get an error:
error C2036: 'void *' : unknown size
since size of buffer+1 unknown. If you do return buffer, it'll compile, but that's not what we need.
ASKER CERTIFIED SOLUTION
Avatar of jasonclarke
jasonclarke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alex108

ASKER

Ot,s hot, but not that. As soon as you write:
return buffer+1;
you get an error:
error C2036: 'void *' : unknown size
since size of buffer+1 unknown. If you do return buffer, it'll compile, but that's not what we need.
Avatar of alex108

ASKER

Well done. It works.Thank you very much.