CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Sep 2009
    Posts
    6

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Inserting element in array

    What's a lac?

    Can you show what you tried?

  3. #3
    Join Date
    Sep 2009
    Posts
    1

    Re: Inserting element in array

    what is a lac there?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Inserting element in array

    Quote Originally Posted by GCDEF View Post
    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).

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Inserting element in array

    I was interpreting his post to mean set every 4th element to 0, but who knows.

  6. #6
    Join Date
    Sep 2009
    Posts
    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

  7. #7
    Join Date
    Sep 2009
    Posts
    6

    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

  8. #8
    Join Date
    Sep 2009
    Posts
    3

    Re: Inserting element in array

    Light Attack Craft. Duh.

  9. #9
    Join Date
    Sep 2009
    Posts
    3

    Re: Inserting element in array

    Quote Originally Posted by dgonline View Post
    Light Aghfhgfhgfhfhgfvbnbvn ghjghjghjghjghj
    fghgfhgfhfhfg fhfgh fghgf

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  11. #11
    Join Date
    Sep 2009
    Posts
    3

    Re: Inserting element in array

    this is my replay

  12. #12
    Join Date
    Aug 2009
    Location
    The Netherlands
    Posts
    7

    Re: Inserting element in array

    Why don't you use a vector to accomplish what you are doing?

  13. #13
    Join Date
    Sep 2009
    Posts
    6

    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

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  15. #15
    Join Date
    Sep 2009
    Posts
    6

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured