|
-
September 3rd, 2009, 07:50 AM
#16
Re: Inserting element in array
 Originally Posted by rahulnair87
The code u wrote will replace every fourth element in the array with a zero, but i want to INSERT a zero in every fourth index.
which also means that on each insertion u got to shift the array by one.
Now , pls put in your valuable suggestions and the array size is about 1300000.
regards
Rahul
Please use code tags and complete, real words.
Arrays don't really support insertion, so it's going to be time consuming. Can you use a list?
Obviously you're going to have to create a new array with a larger size. I'd look at figuring out how big the new array needs to be then making one allocation with the new size. memset the whole thing to zero, then use memcpy to copy four elements at a time from the old array to the new one skipping every fourth element.
This is completely untested, but may point you in the right direction.
Code:
int newsize = (oldsize * 125) / 100;
char* newArray = new char[newsize];
memset(newArray, 0, newsize);
int nSkip = 0;
for(int i = 0; i < oldsize; i += 4)
{
memcpy(newArray + i + nSkip, oldArray + i, 4);
nSkip++;
}
-
September 3rd, 2009, 07:58 AM
#17
Re: Inserting element in array
Code:
void makeitwork ( unsigned char* input, unsigned char* output, unsigned inputlength )
{
// index of the new array
unsigned int tempindex = 0;
// alloc new array with the new bytes
unsigned char* temp = new unsigned char[inputlength + (inputlength / 4) + 1];
ZeroMemory(temp, inputlength + (inputlength / 4)+1);
for(unsigned int i = 0; i < inputlength; ++i)
{
temp[tempindex++] = input[i]; // keep copying the input data to the temp buffer
if((i+1) % 4 == 0) // insert here?
{
temp[tempindex++] = '-'; // = 0;
}
}
// copy to the output
memcpy(output, temp, inputlength + (inputlength / 4) + 1);
delete[] temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
char* teste = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
char* teste2 = new char[strlen(teste) + (strlen(teste) / 4) + 1];
makeitwork((unsigned char*)teste, (unsigned char*)teste2, strlen(teste));
cout << teste2 << endl;
system("PAUSE");
}
try this
-
September 3rd, 2009, 08:11 AM
#18
Re: Inserting element in array
I am facing memory constraints when am accessing the dll in Labview , i get a memory insufficient message.
Please guide me in dynamically allocating big memory sizes for th purpose.
Thankyou for the guidance.
Rahul
-
September 3rd, 2009, 08:19 AM
#19
Re: Inserting element in array
There's no other way to do it unless you have control over the way the original array is created. As I said, an array is not really the right structure. A list would be better.
-
September 3rd, 2009, 11:52 AM
#20
Re: Inserting element in array
Code:
_declspec(dllexport) long fly(unsigned char *a,unsigned char *b ,int s)
{ b= new unsigned char[s];
a= new unsigned char[s];
Again, what's the point of passing two arrays to the function if you're just going to discard the passed arrays immediately in favor of locally created ones?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|