1

All we know malloc return the start address of allocated memory block but I would like how do I count end address of this block

  int *p, *q;
  p=malloc(4*sizeof(int));
  q=p+4;

Now q will be pointing to end address block but I would like to have some other approach to get end address of memory block allocated by malloc.

Edit

One more thing I would like know here is there way to make sure that we are at end of allocated block

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • Actually you don't. The `4*sizeof(int)` will on almost any modern system result in 16. So +8 won't do the trick. – halfdan Nov 20 '11 at 20:07
  • `p` points far after the end of the allocated memory. You need to add `4`, in most cases (even thought I think it's implementation defined). And why would you need other approaches about that? – Kiril Kirov Nov 20 '11 at 20:08
  • 3
    @halfdan: you're sort of right, but seemingly for the wrong reason. The +8 will advance the pointer by 8*sizeof(int) bytes. – John Zwinck Nov 20 '11 at 20:08
  • 1
    Since you know you already allocated `4` `int`s worth of memory, you could do `p += 4`. But it's best to copy `p` so you can `free` it later. – wkl Nov 20 '11 at 20:08
  • @halfdan i am taking int as 2 byte long – Amit Singh Tomar Nov 20 '11 at 20:09
  • @AmitSinghTomar - read more about pointer arithmetic. In short - `pointer + number` is `address_pointed_by_the_pointer + sizeof( the_pointed_type)`. So, it doesn't matter what's the size of an `int`. Adding `1` will move the pointer to the next element in the array. – Kiril Kirov Nov 20 '11 at 20:11
  • @AmitSinghTomar: the 4 was correct; a `sizeof(int)` factor is implicit in the `p+4`. – Fred Foo Nov 20 '11 at 20:11
  • @Amit - your platform ints are 2 bytes wide? `pointer + n` would result in moving the `pointer` by `n` steps dependent on what the pointer's underlying type is. If pointer were an `int *`, advancing or going backward by 1 would mean moving by `sizeof(int)`. – wkl Nov 20 '11 at 20:12
  • Calculating `p + 8` is [Undefined Behaviour](http://c2.com/cgi/wiki?UndefinedBehavior). During that calculation anything can happen: your computer may explode; your computer may transfer money to my bank account, ..., ... – pmg Nov 20 '11 at 20:12
  • I assumed the `8` was an honest mistake and changed it to a `4`. Apprently the `8` was the intended value ... and is an error! – pmg Nov 20 '11 at 20:15
  • @pmg Surely it's dereferencing `p+8` that is UB. Just calculating it is fine. – David Heffernan Nov 20 '11 at 20:15
  • @David: no, calculating is UB. See 6.5.6/8 in the [C99 Standard](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf). – pmg Nov 20 '11 at 20:16
  • @pmg i got the point it should be 4 not 8 in this case – Amit Singh Tomar Nov 20 '11 at 20:18
  • @pmg That would make it very inconvenient to test if a pointer was contained in a given block of memory. – David Heffernan Nov 20 '11 at 20:20
  • @pmg I expect you are right and have edited my answer so that it (hopefully) does not come under attack from standards lawyers! ;-) – David Heffernan Nov 20 '11 at 20:23
  • 1
    @pmg OK, I read that. `p+4` is fine, but `p+5` is UB. So the standard specifically allows you to point to 1 past the end. That is very sensible. – David Heffernan Nov 20 '11 at 20:25
  • @Amit Twice now I have converted i into I in your question. For future reference, please take a little more time and use the capital I. – David Heffernan Nov 20 '11 at 20:29
  • @Amit No probs at all, just trying to help you out for the future!! – David Heffernan Nov 20 '11 at 21:06
  • Please consider taking extended discussion to [chat] as it keeps questions/answers/comments leaner and cleaner, void of lengthy dialogue. – Sampson Nov 20 '11 at 23:26

4 Answers4

3

You can't, using standard techniques, using just a pointer as input, determine where the end of the block lies. In order to do that. you would have to instrument your allocator or use an allocator that provided such functionality.

When you allocate memory with malloc the responsibility is entirely down to you the programmer to keep track of how much memory you allocated. You cannot ask the system, at some later point in time, to tell you any information about the memory block.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

The code you wrote (p=p+8) will have p pointing to beyond the end, since you allocated space for 4 ints but shifted the pointer by 8 (ints, not bytes).

One way to do it is trivial:

size_t const SIZE = 4;
int *p;
p=malloc(SIZE*sizeof(int));
p=p+SIZE;

Now, no bug, and no likely future one either.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Slightly late answer, but I figure this needs a mention: For Visual Studio, you can use _msize() to calculate the number supplied to malloc() (or number of bytes allocated by new x, if you're using C++). It only works for pointers allocated on the heap though, so _malloca(), alloca(), char x[2]; won't work.

Phi
  • 49
  • 7
1

If you just want to know how much space you asked for, you are out of luck, since this is not part of the contract of malloc(3)'. Some particular implementations of malloc support an API for this, but it is not portably.

So, you cannot unless you write your own malloc.

If you really want to know the total amount of memory allocated, you are even further out of luck. If malloc returns value p for size q, then the storage can start at any p-X and extend to p-X+q+Y, for any values of X and Y that the implementors of malloc found convenient for their own overhead.

The right way to detect errors in pointer usage is to use a tool like valgrind.

bmargulies
  • 97,814
  • 39
  • 186
  • 310