Thread: Making an array dynamic

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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 05: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
    28,413
    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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-21-2005, 12:39 AM
  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