-
September 1st, 2009, 09:00 AM
#1
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
-
September 1st, 2009, 09:04 AM
#2
Re: Inserting element in array
What's a lac?
Can you show what you tried?
-
September 1st, 2009, 10:01 AM
#3
Re: Inserting element in array
-
September 1st, 2009, 10:08 AM
#4
Re: Inserting element in array
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).
-
September 1st, 2009, 10:12 AM
#5
Re: Inserting element in array
I was interpreting his post to mean set every 4th element to 0, but who knows.
-
September 3rd, 2009, 12:13 AM
#6
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
-
September 3rd, 2009, 12:17 AM
#7
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
-
September 3rd, 2009, 12:42 AM
#8
Re: Inserting element in array
-
September 3rd, 2009, 12:45 AM
#9
Re: Inserting element in array
Originally Posted by dgonline
Light Aghfhgfhgfhfhgfvbnbvn ghjghjghjghjghj
fghgfhgfhfhfg fhfgh fghgf
-
September 3rd, 2009, 12:46 AM
#10
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.
-
September 3rd, 2009, 12:48 AM
#11
Re: Inserting element in array
-
September 3rd, 2009, 04:01 AM
#12
Re: Inserting element in array
Why don't you use a vector to accomplish what you are doing?
-
September 3rd, 2009, 06:32 AM
#13
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
-
September 3rd, 2009, 07:09 AM
#14
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.
-
September 3rd, 2009, 07:25 AM
#15
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
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
|