CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Inserting element in array

    Quote Originally Posted by rahulnair87 View Post
    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++;
    }

  2. #17
    Join Date
    Aug 2009
    Posts
    10

    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

  3. #18
    Join Date
    Sep 2009
    Posts
    6

    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

  4. #19
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  5. #20
    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,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?

Page 2 of 2 FirstFirst 12

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