Thread: Determining size of block of memory

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    38

    Determining size of block of memory

    Say i have a function that is being passed two pointers to two separate arrays of doubles and that these arrays occupy the same ammount of memory. how can I:

    Initialize a third pointer to the size of the other two (I don't know the number of elements in the array)

    Step through the arrays without overstepping the bounds and crashing my program?

  2. #2
    old man
    Join Date
    Dec 2005
    Posts
    90
    number of elements in array = sizeof array / sizeof *array

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    love simple answers.....i'm oblivious sometimes!

    Thanks a bunch!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    number of elements in array = sizeof array / sizeof *array
    That wont work. sizeof(array) is going to equal the size of a pointer (4 on most systems) when that's all you have. There is no way to figure out the size of an array which is passed to a function. That is why you always see functions that pass a pointer to an array and the size of the array.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The array name needs to be in scope to do that, so you'll need to pass the number of elements as a parameter to your function. This disclaimer was to prevent you from doing this:
    Code:
    void foo(double *a, double *b)
    {
       int size = sizeof a / sizeof *a;
       /* ... */
    }
    And instead to this.
    Code:
    void bar(double *a, double *b, size_t size)
    {
       /* ... */
    }
    
    void baz(void)
    {
       double x[5], y[5];
       bar(x, y, sizeof x / sizeof *x);
    }
    Last edited by Dave_Sinkula; 01-12-2006 at 11:43 AM. Reason: Holy pokiness, Batman. Two interceding posts?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The sizeof only works on arrays which are in scope.
    If all you have is a pointer to an array, because that's your parameter, then it doesn't work.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    I have some flexibility with my choice of parameters, If i pass the array directly, than i can use sizeof() to determine the number of elements
    Last edited by ExxNuker; 01-12-2006 at 11:49 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    "when applied to the name of an array"
    Read carefully.
    If you have an array, then it works.
    If all you have is a pointer, because someone passed an array to you, then it doesn't.

  9. #9
    old man
    Join Date
    Dec 2005
    Posts
    90
    I just came back to qualify my post, but others have been here already :-)

    It won't work when array has decayed to a pointer, so it depends on what you're doing. I didn't read your post carefully enough the first time (not enough coffee), so it's my bad.

    You can still use sizeof when array is in scope (to avoid hardcoding the array size) and then pass that count to the function.
    Last edited by eerok; 01-12-2006 at 11:55 AM.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Angry

    What is so hard about allocating SizeOfControlledSequence*sizeof(data_type)?

    As long as data_type is a known this will work. Strange how it looks like C++ new eh?

    Code:
    DWORD *m_pMap=new DWORD[size];
    DWORD *m_pMap=malloc(size*sizeof(DWORD));
    Code:
    struct TestStruct
    {
      int a,b,c,d,e,f,g,h;
      DWORD i,j,k,l,m,n,o,p,q;
      BYTE array[400];
    };
    
    TestStruct Temp;
    
    //Assign values to members
    ...
    ...
    //Write to disk
    int handle=_open("Test.dat",_O_BINARY | _O_CREAT);
    
    if (handle!=-1)
    {
      _write(handle,&temp,sizeof(TestStruct));
    
      close(handle);
    }
    Same idea. _write writes bytes at a time so just passing the number of data members to it won't work. It needs to know the total size of the struct to compute the total size in bytes.

    When you are allocating an array, the array can be of any data type. But you can use sizeof() for the default C recognized data types like int, long, short, float, double, etc.

    When you move on to C++ and use new you only need to allocate the size of the sequence. For 100 int's you simply do:

    Code:
    int *pArray=new int[100];
    But this must allocate 100*sizeof(int) because each integer in 32-bit protected mode is 32-bits or 4 bytes. So it actually allocates 400 bytes, not 100.

    A simple test proves this:

    Code:
    for (int i=0;i<100;i++)
    {
      pArray[i]=i;
    
      printf("%d\n",pArray[i]);
    }
    If the memory allocation was not working this would not print 100 items. It would print 25 indexes and then crash because you can only store 25 32-bit signed/unsigned integers in 100 bytes. So it would overrun the bounds of the array. So new allocates 100 items of size (data_type), not 100 bytes. Malloc allocates bytes so you must pre-compute the size and then pass that size to malloc.

    On the 26th attempt pArray[i]=i would cause an access violation if the exception was unhandled inside of Windows 2K/XP, a possible general protection fault or GPF on Windows 95/98, and a definite unrecoverable crash in DOS.

    Say i have a function that is being passed two pointers to two separate arrays of doubles and that these arrays occupy the same ammount of memory. how can I:

    Initialize a third pointer to the size of the other two (I don't know the number of elements in the array)
    You cannot do this without knowing the size in BYTES of the array.
    Knowing how many indexes or elements there are won't help on a pointer as has been said.
    Last edited by VirtualAce; 01-13-2006 at 12:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. My memory management solution
    By cboard_member in forum Game Programming
    Replies: 20
    Last Post: 08-23-2006, 09:07 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. Memory size
    By a_learner in forum C Programming
    Replies: 4
    Last Post: 10-08-2001, 08:10 PM