Solved

pointer and its size

Posted on 2001-06-18
19
729 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

756 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