Solved

pointer and its size

Posted on 2001-06-18
19
725 Views
Last Modified: 2007-11-27
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.
0
Comment
Question by:alex108
  • 8
  • 5
  • 2
  • +3
19 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6205350
what's wrong with _msize ?
0
 
LVL 2

Expert Comment

by:sateesh_babu
ID: 6205365
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
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6205384
how would you use sizeof on a void pointer that's passed into a function ?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6205559
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.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205569
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
0
 

Author Comment

by:alex108
ID: 6205575
Exactly-I need to know hoe _msize works?
Regarding sizeof?
if I do the following:
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6205576
Slightly off topic, but FYI mixing new and malloc in the same project is discouraged.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205586
_msize returns the size of a heap allocated block of memory.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6205613
> 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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:alex108
ID: 6205619
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
0
 

Author Comment

by:alex108
ID: 6205626
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
0
 

Author Comment

by:alex108
ID: 6205634
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
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6205638
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);
}
0
 

Author Comment

by:alex108
ID: 6205649
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
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6205664
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);
}
0
 

Author Comment

by:alex108
ID: 6205681
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.
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 100 total points
ID: 6205691
> it'll compile, but that's not what we need.

try the 2nd version, I think that should be OK, however a minor mistake:

size_t* buffer = (int *)::malloc(size+sizeof(int));

should be:

int* buffer = (int *)::malloc(size+sizeof(int));
0
 

Author Comment

by:alex108
ID: 6205703
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.
0
 

Author Comment

by:alex108
ID: 6205717
Well done. It works.Thank you very much.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now