Solved

pointer and its size

Posted on 2001-06-18
19
728 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 125
Getting IP address 8 79
How to split this in C++ 4 102
Precision Problem in C++ 7 33
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

809 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