Click to See Complete Forum and Search --> : Dynamic Arrays
kuhns_m
June 21st, 2002, 02:48 PM
This is an easy one. I have a dynamic array of a certain size. I want to resize my array. Do I lose all the array members of the original array? I am looking for something similar to VB's redim preserve for c++ or do I have to save my original array create a new one then copy the original into the new one with the new data member.
Thanks in advance,
Matt
Paul McKenzie
June 21st, 2002, 02:55 PM
You would have to allocate the new memory, copy the data to the new memory, and delete the old memory.
However, why not use std::vector<>? It does all of this work for you.
#include <vector>
std::vector<int> IA; // A dynamic array of int's
//...
// Add
IA.push_back(20);
//...
//... Redimension
IA.resize(100); // Array now has 100 elements, keeps old elements
Regards,
Paul McKenzie
JMS
June 21st, 2002, 03:17 PM
int MyArray[10] = {0,1,2,3,4,5,6,7,8,9 };
int *pDynamicArray = NULL;
// allocate a new array
pDynamicArray = new int[100];
// Initialize array to -1.
memset( pDynamicArray, -1, sizeof(int) *100 );
// copy in your old array
memcpy( pDynamicArray, &MyArray, sizeof(MyArray));
// Verify the copy worked
for ( int i = 0; i < 100; i++ )
{
if ( pDynamicArray[i] != -1 )
printf("%d\n", pDynamicArray[i]);
else
break;
}
jfaust
June 21st, 2002, 03:35 PM
Please use the STL vector class as Paul mentioned.
However, if you really do want redimension a C array, and I'm almost embarassed to mention this, look up realloc().
Jeff
Graham
June 21st, 2002, 04:22 PM
Ditto.
Use std::vector.
mce
July 23rd, 2002, 01:38 AM
almost all guru are suggesting using std::vector as dynamic array.. i do love std::vector... even since Paul Mckenzie suggested me a few years back in codeguru when i was asking some array question.. it is so easy and clean.
But now i am facing a problem towards using std::vector, i want to put std::vector in a shared memory. Can somebody tell me how to do this, and if this is not possible, what is the alternative?
Am i facing a dead end.. now..? I want to enjoy the dynamic array as std::vector, but shared.
If i can use C array, i need to use new delete to have dynamic array, well, if i can use new delete here, why can't i just declare a std::vector pointer in my shared section and thus have my vector shared?
OR may be someone is going to tell me that i can do shared momory on fixed C style array only...:mad: :mad:
Alexey B
July 23rd, 2002, 01:54 AM
The shared memory sector has a fixed size. Make it the largest size you will need it to be and put a statically allocated vector into it.
mce
July 23rd, 2002, 02:04 AM
do you mind showing me some code for your suggestion? Thanks...
Alexey B
July 23rd, 2002, 02:12 AM
Honostly, I have never done this, but the following should work:#pragma comment(linker, "/SECTION:.Shared,RWS)
#define SHARED_SIZE 512
#pragma data_seg("Shared")
BSTR array[SHARED_SIZE];
#pragma data_seg()In my opinion, shared sections do not suit your needs. Try using an alternative method of interprocess communication, such as memory mapped files.
mce
July 23rd, 2002, 08:35 AM
i thought of using memory mapped files before..
but the problem is.. say that i am using stl::vector,
how would you suggest to write stl object to a file?
by copying stl vector to a buffer first?
if so how am i going to write something like below?
memcpy( buf, &stlObj, sizeof( stlObj) );......??
let's say i am using stl::vector<CComBSTR> m_vector
Also, how i know how many bytes in the vector?
Or more straight to the point, could you show me some codes how you write functions like CreateFileMapping(.....).. ? And how to access the data in the memory block pointed by MapViewOfFile??
One more point, what is the safe maximum size of the memory map file i can use declare or use? so that i can use dynamic stl object sizing smoothly?
Alexey B
July 23rd, 2002, 01:34 PM
Here is how to share data between two processes A and B using memory mapped files: Process A creates a named file map, passing 0xFFFFFFFF as the hFile parameter. This tells the system that the storage for the map will reside in a paging file - CreateFileMapping.
Process B opens the named file map - OpenFileMapping.
A process maps a view of the map to write to it or read from it, and closes it when it is done - MapViewOfFile, UnmapViewOfFile.
When the processes are done sharing data they close the file map handles - CloseHanlde.Win32 guarantees coherence between views of a file mapping object. Be sure to study each of the functions you are going to call. It is very important that you check for failure after calling each function.
Do not share the STL vector. As well, you can not share an array of strings. You have to copy whole strings into the view, not just pointers to them.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.