# Thread: How to check the size of dinamyc array ?

1. ## How to check the size of dinamyc array ?

I have a 2 dimentional array where i need to check if row and colum number is the same, and i need to do it using arrays, not the usual n==m
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int **p,i,j,n,m;
srand(time(NULL));

n=rand()%10;
m=rand()%10;
p = (int **)malloc(n*sizeof(int));
for(i=0;i<m;i++);{
p[i]= ((int*)malloc(m*sizeof(int)));}

for(i=0;i<n;i++){
for(j=0;j<m;j++){
*(p+i+j)=rand() % 10; } }```

2. I don't think what you are asking makes sense. You cannot recover the size of a malloc'ed piece of memory just from the pointer, so the only information you have about the size of the array is n and m.

3. Well i need to check p[n][m] if (n == m) ? using pointers is it even possible ? p[n][m] is a matrix and i need to check if rows == collums using pointers

4. Do you mean you only want to walk through the diagonal entries? Or do you just want to know if the original is square? If the latter, then you have to compare the original sizes, because that's the only information about the sizes that exists.

5. > p = (int **)malloc(n*sizeof(int));
1. Don't cast the result of malloc in a C program.
Cprogramming.com FAQ > Casting malloc

2. Your sizeof is wrong, it should be sizeof int*, not sizeof int (for the outer block).
The idiom
Code:
`p = malloc ( n * sizeof *p );`
is easier to remember, and you don't have to keep figuring out what the type is (let the compiler do it for you)

6. Do you mean you only want to walk through the diagonal entries? Or do you just want to know if the original is square? If the latter, then you have to compare the original sizes, because that's the only information about the sizes that exists.
so basicly the only thing i can do is to compare m == n , theres no other option to check if its a square ?
Salem
Well it works , and what works cantbe wrong , cant it ?

7. Originally Posted by Stobor
I have a 2 dimentional array where i need to check if row and colum number is the same, and i need to do it using arrays, not the usual n==m
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int **p,i,j,n,m;
srand(time(NULL));

n=rand()%10;
m=rand()%10;
p = (int **)malloc(n*sizeof(int));
for(i=0;i<m;i++);{
p[i]= ((int*)malloc(m*sizeof(int)));}

for(i=0;i<n;i++){
for(j=0;j<m;j++){
*(p+i+j)=rand() % 10; } }```
First of all, creating arrays from random numbers is just plain stupid. (Sorry, but it is!)
If you want to play that game, create the array at your maximum sizes and limit your indexes by random numbers less than the maximum size.

On the Windows platform you can try this...
Code:
```int GetArraySize(void *ptr)
{ HANDLE hHeap;
int PtrSize = -1;

hHeap = GetProcessHeap();
if (! hHeap)
return -1;

if (HeapValidate(hHeap,0,MyPointer))
PtrSize = HeapSize(hHeap,0,MyPointer));

CloseHandle (hHeap);
return PtrSize; }```
This will fail if A) your pointer does not point to the first byte in a block created with malloc() or B) your compiler does not use the default heap for memory allocation.

8. > Well it works , and what works cantbe wrong , cant it ?
Of course it can be wrong.
It's only working for you at the moment down to pure dumb luck and that your current machine has the same size for pointers and integers.

If you tried (by implication)
char **p = malloc( 10 * sizeof(char) );
you'd get yourself into all sorts of difficulty almost immediately.

9. Code:
```if ((ptr+n+0)==(ptr+m+0))

printf("square");
else printf("No");```
Thank you all great minds for not helping with this solution

10. Originally Posted by Stobor
Code:
`if ((ptr+n+0)==(ptr+m+0))`
Why exactly do you think that is different than if( n == m )? You do know that if something appears on both sides of a equation, you can get rid of it from both sides and have the same thing, so if you get rid of ptr and you get rid of 0 (which is absolutely doing nothing here), then all you end up with is n == m
Originally Posted by Stobor
Thank you all great minds for not helping with this solution
Yeah...

Or you could have actually remembered how math works, and done:
Code:
`if( n-m == 0 )`

Quzah.

11. Code:
`if ((ptr+n+0)==(ptr+m+0))`
Even kids know that you're just cheating yourself.
It's just a stupid way to write n == m. isn't it?

12. Originally Posted by Bayint Naung
Code:
`if ((ptr+n+0)==(ptr+m+0))`
Even kids know that you're just cheating yourself.
It's just a stupid way to write n == m. isn't it?
Even simpler... if (! n-m)

13. IMO n == m is much more straightforward. and No fear of overflow.
and I think you meant ( !(n-m) )

14. Originally Posted by Bayint Naung
IMO n == m is much more straightforward. and No fear of overflow.
and I think you meant ( !(n-m) )
Actually... now that you mention it