[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

pointer and its size

Posted on 2001-06-18
19
Medium Priority
?
735 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

656 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