total size of dynamic memory allocated array

This is a discussion on total size of dynamic memory allocated array within the C Programming forums, part of the General Programming Boards category; hi, this is my first post and my question is... When using the sizeof operator on an array name it ...

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    Question total size of dynamic memory allocated array

    hi, this is my first post and my question is...

    When using the sizeof operator on an array name it returns
    the size that the whole array allocates,eg

    Code:
    int array[ 20 ] = { 0 };
    printf( "%u ", sizeof( array ) ); /* returns 80 */
    is there a way to do the same on a dynamic array? eg

    Code:
    int *array = ( int * ) malloc( 20 * sizeof( int ) );
    thanks
    Last edited by trekker; 03-08-2002 at 09:54 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Of course there is, you can dynamically allocate an array with only a little bit more work than a static array. But if you try to use the sizeof operator on the entire dynamic array then the value will most likely be 4, which is the size of a pointer. sizeof is determined at compile time and the size of a dynamic array is determined at runtime, so sizeof will only return the size of the pointer and not the memory allocated.
    Code:
    /* pseudocode */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i, errors = 0;
      char **array = malloc ( 20 * sizeof ( char * ) );
      if ( array != NULL ) {
        for ( i = 0; i < 20; i++ ) {
          array[i] = malloc ( 10 * sizeof ( char ) );
          if ( array[i] == NULL ) errors++;
        }
        for ( i = 0; i < 20; i++ )
          free ( array[i] );
        free ( array );
      }
      printf ( "Code exited with %d errors\n", errors );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    well, i might be stupid but i can't see how your code answers my question. So, i'll try to be more specific.

    After i allocate memory for my pointer i pass him as an arg
    to an other function. I want that function to be able to get the size of memory allocation...

    i hope i made myself clear,
    thanks again

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but i can't see how your code answers my question
    I thought you were asking both how to create a dynamic array and how to get the size of it. The code doesn't answer the latter, but the message does:
    But if you try to use the sizeof operator on the entire dynamic array then the value will most likely be 4, which is the size of a pointer. sizeof is determined at compile time and the size of a dynamic array is determined at runtime, so sizeof will only return the size of the pointer and not the memory allocated.
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    thanks Prelude...
    here's what i want to do ( kind of... )

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void f( int *ptr )
    {
    	int *innerPtr, size = 0;
    
    	/* here i want to count the allocated memory of ptr 
    	and assign it in variable size */
    	
    	innerPtr = ( int * ) malloc( size * sizeof( int ) );
    
    	/* rest of the code */
    
    	return;
    }
    
    int main( void )
    {
    	int *array;
    
    	array = ( int * ) malloc( 20 * sizeof( int ) );
    
    	f( array );
    
    	return 0;
    }
    thanks for any additional input

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Set the size variable in main and pass it to the function. That way tends to be less buggy than trying to acquire the size of a dynamic array.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static void f( int *ptr, int size )
    {
      int *innerPtr;	
      innerPtr = malloc( size * sizeof *innerPtr );
      /* rest of the code */
      return;
    }
    
    int main( void )
    {
      int *array, size = 20;
      array = malloc( size * sizeof *array );
      f( array, size );
      return 0;
    }
    /* Don't forget to free the pointers :) */
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    isn't there a getallocsize function or something like that?
    i've been searching all afternoon...

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >isn't there a getallocsize function or something like that?
    Nothing that's standard

    -Prelude
    My best code is written with the delete key.

  9. #9
    Sayeh
    Guest
    > After i allocate memory for my pointer i pass him
    > as an arg to an other function. I want that function
    > to be able to get the size of memory allocation...

    No, there is no such "standard" function. You, however, can write one if you like--

    All that is required is that you learn enough about Window's or DOS's memory management so that you can break apart the block header for the dynamically allocated RAMblock in questions.

    Normally, an allocated block has about 12 bytes extra prepended to it. This additional space (that you normally aren't aware of), describes information about the block (lockstate, size, offset-size, etc.).

    enjoy.

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Or as an alternative you could wrap your pointer in a struct, along with a variable containing the size.

  11. #11
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    well i'm interested in low-level programming and i would like
    to know if there is any kind of book that could help me do so.
    The unusual thing about me is that in uni i use visual studio
    on pc and back home i program on a macintosh with
    codewarrior. However info on windows memory management
    won't hurt

    tia

    Originally posted by Sayeh
    > After i allocate memory for my pointer i pass him
    > as an arg to an other function. I want that function
    > to be able to get the size of memory allocation...

    No, there is no such "standard" function. You, however, can write one if you like--

    All that is required is that you learn enough about Window's or DOS's memory management so that you can break apart the block header for the dynamically allocated RAMblock in questions.

    Normally, an allocated block has about 12 bytes extra prepended to it. This additional space (that you normally aren't aware of), describes information about the block (lockstate, size, offset-size, etc.).

    enjoy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 08:45 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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