The following is a project that i'm working on at the moment and as i say is work in progress but having huge problem in trying to have a dynamicly re-sized array once the array gets too big.

in the programme which will mean i'll miss out in a huge amount of marks. i'm using const at moment but don't have clue how to do dynamic array:?:
it would be a great help if some one here could show me how to do this in my programme.

thanks alot if any one does


p.s. i know there are bugs in it i'll sort them out only need help with the setting up of dynamic arrays


cheers

Recommended Answers

All 10 Replies

trying to have a dynamicly re-sized array

That's not possible, just make sure it's big enough

That is a c++ program so why don't you just use a vector of BSUPPLYER objects

vector<BSUPPLYER> supply;

The vector is an array that will increase and shrink itself as you desire. You don't have to bother about memory allocation.

The following is a project that i'm working on at the moment and as i say is work in progress but having huge problem in trying to have a dynamicly re-sized array once the array gets too big.

in the programme which will mean i'll miss out in a huge amount of marks. i'm using const at moment but don't have clue how to do dynamic array:?:
it would be a great help if some one here could show me how to do this in my programme.

thanks alot if any one does


p.s. i know there are bugs in it i'll sort them out only need help with the setting up of dynamic arrays


cheers

Since the memory is not allocated during run time for arrays, you cannot resize it. The option is to use Linked list or vectors.
(if you know the concept of smart pointers) you can create the linked list and using the smart pointers you can make your program access the data like an array. (using increment and decrement operators to point to the next/prev item...)

Regards
Murthy

Since the memory is not allocated during run time for arrays, you cannot resize it. The option is to use Linked list or vectors.
Regards
Murthy

what gave you the idea that arrays can not be reallocated? new, malloc, or realloc() can be used to do that. Linked lists and vectors are better opions, but not the only options.

// supplySize indicates the current size of the array.  When elementsUsed equals supplySize then the array must be increased
int supplySize = 25;
// elementsUsed intidates how many elements in the array have been used.  It should be increased each time a new element in the array is used
int elementsUsed = 0;

// array with initial allocation.
BSUPPLYER * supply = new BSUPPLYER[supplySize];

// now increase the array size

if( (elementsUsed+1) == supplySize)
{
   // add one new element to the array
   BSUPPLYER * temp = new  BSUPPLYER[supplySize+1];
   // copy data from old to new array
   for(int i = 0; i < arraySize; ++)
   {
        temp[i] = supply[i];
   }
   // delete old array
   delete[] supply
   // rename old to new
   supply = temp;
   // bump array counter
   ++supplySize;
}

what gave you the idea that arrays can not be reallocated? new, malloc, or realloc() can be used to do that.

Hello.

Actual arrays can't be resized, what you are talking about is data pointers being allocated enough memory to be used as arrays.

The important differentiation here being used as arrays and actual arrays.

Thank you.

The important differentiation here being used as arrays and actual arrays.

Thank you.

But they can be used as arrays. No difference except the allocation method. (and a couple other minor differences).

[edit]I reread your statement again and see the distinction you were trying to make. I believe the distinction should be between statically allocated and dynamically allocated arrays. The are both arrays, its just how they are allocated is the difference. It's a matter of getting the terminology correct. statically allocated arrays can not be reallocated. Others can. [/edit]

I read it in a thread on google group that mixing arrays and pointers interchangbly was not good so just thought I would point it out.

There is a lot of difference between something which is an array and something which is used as an array.

Thank you.

I read it in a thread on google group that mixing arrays and pointers interchangbly was not good so just thought I would point it out.
Thank you.

Well, they were wrong. There are millions of programs out there that use dynamically allocated arrays as I posted in my example.

But in c++ programs it isn't necessary because, as mentioned by both of us mentioned previously, c++ has vector class which handles all the messy allocation stuff. And I believe that is how vector class is normally implemented -- dynamically allocating C-style arrays.

There is a lot of difference between something which is an array and something which is used as an array.

Name one. Anything you can do with statically allocated arrays can be done with dynamically allocated arrays -- the reverse however is not true.

thanks alot i shall try that this evening and let you know how i get on tomorrow.

and the reason i'm not using vectors is because we are not allowed. we have to make dynamic arrays using a series of pointers layed out in a function. and call this function once the array gets full and then double size of array or multiply by 1.5 or something.

>Well, they were wrong.
It depends on what they were referring to, but I'm going to assume they were correct if it was comp.lang.c. Wrong information doesn't last long on that group.

>Name one.
I can name three:

1) Take the sizeof a dynamically allocated array.
2) Take the address of a dynamically allocated array.
3) Initialize a dynamically allocated array to a string literal.

Those are the three cases where an array is used in object context instead of value context, and because a dynamically allocated array is not an array, the effect is different.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.