How to check the size of dinamyc array ?

This is a discussion on How to check the size of dinamyc array ? within the C Programming forums, part of the General Programming Boards category; I have a 2 dimentional array where i need to check if row and colum number is the same, and ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    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
    Last edited by Stobor; 05-14-2011 at 09:55 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,747
    > 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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    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.
    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. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Stobor View Post
    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.
    Last edited by CommonTater; 05-14-2011 at 10:55 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,747
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    4
    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. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Stobor View Post
    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
    Quote Originally Posted by Stobor View Post
    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.
    Last edited by quzah; 05-15-2011 at 06:24 AM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    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. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    IMO n == m is much more straightforward. and No fear of overflow.
    and I think you meant ( !(n-m) )

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ansi C - check file size in bytes.
    By Ironic in forum C Programming
    Replies: 9
    Last Post: 04-02-2009, 12:54 PM
  2. Replies: 12
    Last Post: 01-08-2009, 12:15 PM
  3. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM
  4. How do I check memory size after malloc and realloc?
    By electrolove in forum C Programming
    Replies: 3
    Last Post: 02-06-2003, 10:12 AM
  5. can i dynamicaly check the size of an array?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21