size of dynamic array

This is a discussion on size of dynamic array within the C Programming forums, part of the General Programming Boards category; Hi, i have the following program: Code: #include <stdio.h> #include <stdlib.h> int main(){ int *test, i; int test2[5] = {1,2,3,4,5}; ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    Question size of dynamic array

    Hi, i have the following program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
    	int *test, i;
    	int test2[5] = {1,2,3,4,5};
    
    	test = malloc(5*sizeof(int));
    
            //populate dynamic array with arbitary data
    	for(i=0; i<5; i++){
    		
    		test[i] = i+1;
    		printf("test[%d] = %d\n", i, test[i]);
    		
    	}
    
    //OUTPUT 1
    	printf("sizeof test  = %d\n", sizeof(test));
    
    //OUTPUT 2
           printf("sizeof test2 = %d\n", sizeof(test2));
    
    }
    The OUTPUT 1 returns the size of test (the dynamically allocated array) to be 4 and OUTPUT 2 returns test2 as 20. Surely they should both be the same (ie 20) as they both hold 5 elements each. Or is there a different way for establiching the size of a dynamic array?

    thanks i advance.

    tg

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by tommy_gunn
    Or is there a different way for establiching the size of a dynamic array?
    You know the size when you ask for it -- keep track of it if you want it elsewhere.
    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.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    test2 is an array and sizeof() returns the real size of it.

    test1 is a pointer, and that's what sizeof() returns: the size of a pointer on your platform.

    There is no way to get the size of the allocated memory from the pointer. That's why functions like gets() are so dangerous. You pass a pointer to a buffer and gets() has no way of knowing how big that buffer is.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    2
    Quote Originally Posted by Dave_Sinkula
    You know the size when you ask for it -- keep track of it if you want it elsewhere.
    Kool - thought it mite be sumtin like that. thanks for clearing it up for me.

    tg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 03:04 AM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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