Inserting element in array
I am tying to write a dll in vc++ 2005 to insert a '0' at every 4th index of an array.
The array size is about 13 lacs of the type unsigned char.
I wrote a basic code in c++, and made the dll but it is takin a processing time of around 50 secs, which is undesirable.
I am now tying on vectors, but without luck.
Please help me out with the code as am new to cpp.
I am accessing the dll in labview 8.2
urgent
pls reply
Re: Inserting element in array
What's a lac?
Can you show what you tried?
Re: Inserting element in array
Re: Inserting element in array
Quote:
Originally Posted by
GCDEF
What's a lac?
Light Attack Craft. Duh.
In all seriousness, if you need to make that many insertions, you're going to be far better off building an entirely new array and inserting as you go, rather than trying to do "in-place" insertions (which will need to shuffle over every successive element one position on every single insertion....very slow).
Re: Inserting element in array
I was interpreting his post to mean set every 4th element to 0, but who knows.
Re: Inserting element in array
First of all,, 1 lac=100000.
I am not buliding the array , and hence i hav to insert a '0' at every fourth index and shift the elements each time.
I am creating a dll project and am calling this function in Labview(software).
But the processing time is too high.
So pls suggest me with the use of vectors..
I am pasting the current code:
// flycap.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
//#include <math.h>
//#include "stdlib.h"
extern "C" _declspec(dllexport) long fly(unsigned char *a,long s);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
_declspec(dllexport) long fly(unsigned char *a,long s)
{ a = new unsigned char[1400000];
int i,j;
for(i=0;i<s;i=i+4)
{
s=s+1;
for(j=s-1;j>=i;j--)
{
a[j]=a[j-1];
}
a[i]=0;
}
delete [] a;
a=0;
return(0);
}
Pls reply...
Regards
Rahul
Re: Inserting element in array
Also ,, one more query:
How to change the link settings in the VC project to include the multithreaded static runtime lib, which will link in all the neccessary VC runtime code, to make the DLL self contained ?
regards
Rahul
Re: Inserting element in array
Re: Inserting element in array
Quote:
Originally Posted by
dgonline
Light Aghfhgfhgfhfhgfvbnbvn ghjghjghjghjghj
fghgfhgfhfhfg fhfgh fghgf
Re: Inserting element in array
Code:
_declspec(dllexport) long fly(unsigned char *a,long s)
{
a = new unsigned char[1400000];
int i,j;
for(i=0;i<s;i=i+4)
{
s=s+1;
for(j=s-1;j>=i;j--)
{
a[j]=a[j-1];
}
a[i]=0;
}
delete [] a;
a=0;
return(0);
}
That code doesn't really make much sense. The very first thing you do is assign a new array to a, so whatever you tried to pass into the function as a parameter is immediately inaccessible.
You then do a lot of inefficient, time-consuming work moving around undefined values in the new array. Then you delete it. The function does not return anything meaningful, nor does it modify any of its arguments in a manner visible to the caller.
Forgive me if I'm confused as to what this is meant to accomplish.
Re: Inserting element in array
Re: Inserting element in array
Why don't you use a vector to accomplish what you are doing?
Re: Inserting element in array
Pls checkout the code for the same purpose but using two arrays
#include "stdafx.h"
#include <new>
#include "stdlib.h"
extern "C" _declspec(dllexport) long fly(unsigned char *a,unsigned char *b ,int s);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
_declspec(dllexport) long fly(unsigned char *a,unsigned char *b ,int s)
{ b= new unsigned char[s];
a= new unsigned char[s];
int i,j;
for(i=0,j=0;i<s;i=i+3,j=j+4)
{
b[j]=0;
b[j+1]=a[i];
b[j+2]=a[i+1];
b[j+3]=a[i+2];
}
delete [] a;
delete [] b;
return(s);
}
Pls debug the code 4 me.
am using dynamic memory allocation for the two arrays .
Is it okay to use two dynamic alocations,,
wat is the size of heap memory
Re: Inserting element in array
Please use code tags and complete, real words.
The code you're providing doesn't seem to do what you're trying to do based on the problem description. If you just want to set every fourth element of an array to 0 this is all you need.
Code:
for(int i = 3; i < nArrayLength; i += 4)
{
myArray[i] = 0;
}
It's unclear what your inner loops and other machinations have to do with the problem.
Re: Inserting element in array
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
Re: Inserting element in array
Quote:
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++;
}
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
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
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.
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?