Making an array dynamic

This is a discussion on Making an array dynamic within the C Programming forums, part of the General Programming Boards category; Hey there everyone, I'm doing a project for school and I have it completely working right now. There's 1 part ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    68

    Making an array dynamic

    Hey there everyone, I'm doing a project for school and I have it completely working right now. There's 1 part of the project that I don't know how to do...Make the array that's inside of a struct, dynamic.

    If anybody could help me, I'd greatly appreciate it.

    Code:
    typedef struct
    {
    	VectorItemT items[DEFAULT_CAPACITY];
    	int size;
    	int capacity;
    }VectorT;
    Code:
    VectorT *newVector()
    {
    	VectorT *ptr_malloc = (VectorT*)malloc(sizeof(VectorT));
    	ptr_malloc->size = 0;
    	ptr_malloc->capacity = 5;
    	
    	return ptr_malloc;
    }
    These are in separate files.... Anybody got a clue where to start?
    I've read several tutorials & other posts, it just doesn't click when I read them.
    What else do you need to know?

    Thanks ahead of time.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you do not need to cast malloc in C

    2.
    Code:
    typedef struct
    {
    	VectorItemT* items;
    	size_t allocated_capacity;
    	int size;
    	int capacity;
    }VectorT;
    
    
    VectorT *newVector()
    {
    	VectorT *ptr_malloc = malloc(sizeof(VectorT));
    	if(!ptr_malloc) return NULL;
    
    	ptr_alloc->items = malloc(DEFAULT_CAPACITY * sizeof *ptr_alloc->items );
    	if(!ptr_malloc->items)
    		ptr_malloc-> allocated_capacity = 0;
    	else
    		ptr_malloc-> allocated_capacity = DEFAULT_CAPACITY;
    
    	ptr_malloc->size = 0;
    	ptr_malloc->capacity = 5;
    	return ptr_malloc;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    Thanks so much!

    Would you mind going over this step-by-step so I know what's going on?

    and did you just miss type ptr_malloc a couple times?

    Code:
    VectorT *newVector()
    {
    	VectorT *ptr_malloc = malloc(sizeof(VectorT));
    	if(!ptr_malloc) return NULL;
    /////   \/ ptr_malloc?                                        \/ptr_malloc?
    	ptr_alloc->items = malloc(DEFAULT_CAPACITY * sizeof *ptr_alloc->items );
    	if(!ptr_malloc->items)
    		ptr_malloc-> allocated_capacity = 0;
    	else
    		ptr_malloc-> allocated_capacity = DEFAULT_CAPACITY;
    
    	ptr_malloc->size = 0;
    	ptr_malloc->capacity = 5;
    	return ptr_malloc;
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by madmax2006 View Post
    Thanks so much!

    Would you mind going over this step-by-step so I know what's going on?

    and did you just miss type ptr_malloc a couple times?
    m key is too lazy on my keyboard

    Code:
    VectorT *newVector()
    {
    	VectorT *ptr_malloc = malloc(sizeof(VectorT));
    	if(!ptr_malloc) return NULL; /* you need to check the return value of malloc */
    	ptr_malloc->items = malloc(DEFAULT_CAPACITY * sizeof *ptr_malloc->items ); /* allocate DEFAULT_CAPACITY items 
    		* size of each is sizeof *ptr_malloc->items - 
    		* if you change the type of the variable you do not need to change the sizeof operator as in your malloc */
    	if(!ptr_malloc->items)
    		ptr_malloc-> allocated_capacity = 0; /* failed to allocate array */
    	else
    		ptr_malloc-> allocated_capacity = DEFAULT_CAPACITY; /* store the array size to prevent buffer overrun, 
    		* if we will need more than this - 
    		* use realloc to increase size and update this var accordingly */
    
    	ptr_malloc->size = 0;
    	ptr_malloc->capacity = 5;
    	return ptr_malloc;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably shouldn't do:
    Code:
    	ptr_malloc->capacity = 5;
    if the malloc() failed!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    1

    Variable-length array

    Code:
    typedef struct
    {
    	int size;
    	int capacity;
    	VectorItemT items[0]; /*This must be the last member! 
                                                  And if your compiler complains, 
                                                  try changing it to "VectorItemT items[];"*/
    }VectorT;
    Code:
    VectorT *newVector(int capacity)
    {
    	VectorT *ptr_malloc = (VectorT*)malloc(sizeof(VectorT) + capacity * sizeof(VectorItemT));
            if (NULL != ptr_malloc) {
    	    ptr_malloc->size = 0;
    	    ptr_malloc->capacity =  capacity;
    	}
    	return ptr_malloc;
    }
    Last edited by bel5f5; 02-10-2009 at 04:05 AM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    Just noticed you changed something inside of the struct, would it be possible to make the array dynamic without making changes inside the struct, just the 2nd part of he code?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    Quote Originally Posted by madmax2006
    Just noticed you changed something inside of the struct, would it be possible to make the array dynamic without making changes inside the struct, just the 2nd part of he code?
    vart introduced an allocated_capacity member variable, but in my opinion that is redundant: generally, you only need capacity (the number of elements for which space has been allocated) and size (the number of elements in use).

    bel5f5's example is wrong: the C standard does not allow fixed size arrays of zero length, so at the very least it requires a compiler extension that is wholly unnecessary in this case. It should be replaced by a pointer instead, as in vart's example. (In this sense you do need to change your original struct definition to change the array to a pointer.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-20-2005, 11:39 PM
  2. dynamic array
    By linuxdude in forum C Programming
    Replies: 5
    Last Post: 07-13-2004, 02:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

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