memset for a given range in an array - memset

I know how we use memset to set the contents of an array to a particular value. Now, I am interested in something that does the same for a given range.
scanf("%d %d",&a,&b);
//set the content from A[a] to A[b] as '0' without looping
I don't want to go through for loop. Please tell if there is some other and efficient method to achieve this.

Assigning values with memset works only with 2 values : 0 and -1,
Rest will only give garbage values
Still, Let me help you to use memset for a range.
I'm using an example to explain your query below:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[10]; // Array of 10 elements with val of 0;
memset(a,0,sizeof(a));
// Sets all array elements to 0
memset(a+2,-1,sizeof(int)*5);
// Sets value of array elements from 2 to 6 index equals to -1
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
Output: 0 0 -1 -1 -1 -1 -1 0 0 0
* I've checked the code on various IDE's before posting. Hope it helps a bit!

Related

MQL4 CArrayObj how can i set reserve to 0 or avoid it completely?

MQL4 CArrayObj member function At(n) returns zero and member function Total() displays 5 instead of 3 and At(n) returns 0 at index 0-1 and the expect value at 2-4. So my expected data is at 2-4 and the reserve i suspect is at 0-1. How can i stop CArrayObj from reserving space?
I tried setting the CArrayObj member function Reserve to 0, but it is set to ignore 0 as seen in the below
bool CArrayObj::Reserve(const int size){
int new_size;
//--- check
if(size<=0)
return(false);
//--- resize array
if(Available()<size)
{
new_size=m_data_max+m_step_resize*(1+(size-Available())/m_step_resize);
if(new_size<0)
//--- overflow occurred when calculating new_size
return(false);
if((m_data_max=ArrayResize(m_data,new_size))==-1)
m_data_max=ArraySize(m_data);
//--- explicitly zeroize all the loose items in the array
for(int i=m_data_total;i<m_data_max;i++)
m_data[i]=NULL;
}
//--- result
return(Available()>=size);
}
I am expecting the CArrayObj to be 3 only, as i want it to be.

Write Int16 Into AVAudioPCMBuffer swift

I have a Data object in swift that is an array of Int16 objects. For some reason using ".pcmFormatInt16" did not work for the format of my AVAudioPCMBuffer and yielded no sound, or a memory error. Eventually, I was able to get white noise/static to play from the speakers by converting the Int16 to a float and putting that onto both channels of my AVAudioPCMBuffer. I have a feeling that I am getting close to the answer, because whenever I speak into the microphone I hear a different frequency of static. I think the issue is that I am not converting the converted Int16 into the buffer floatChannelData.
Here is my code:
for ch in 0..<2 {
for i in 0..<audio.count {
var val = Float( Int16(audio[i]) ) / Float(Int16.max)
if( val > 1 ){
val = 1;
}
if( val < -1 ){
val = -1;
}
self.buffer.floatChannelData![ch][i+self.bufferCount] = val
self.bufferCount+=1
}
}
self.audioFilePlayer.scheduleBuffer(self.buffer, at:nil, options: .interruptsAtLoop, completionHandler: {
print("played sum")
self.bufferCount=0
})
a typical multi-channel PCM buffer has the channels interleaving on a per sample basis although, not being familiar with swift audio, I find it refreshing to see here channels given a dimension on the buffer datastructure
... a flag goes up when I see your guard checks clamping val > 1 set to val = 1 etc. ... elsewhere that is not needed as those boundary checks are moot as the data nicely falls into place as is
... my guess is your input audio[] is signed int 16 because of your val > 1 and val < -1 ? if true then dividing by max int float is wrong as you would be loosing half your dynamic range ...
I suggest you look closely at your
var val = Float( Int16(audio[i]) ) / Float(Int16.max)
lets examine range of your ints in audio[]
2^16 == 65536 // if unsigned then values range from 0 to (2^16 - 1) which is 0 to 65535
2^15 == 32768 // if signed then values would range from -32768 to (2^15 - 1) which is -32768 to 32767
Please tell is whether input buffer audio[] is signed or not ... sometimes its helpful to identify the max_seen and min_seen values of your input data ... do this and tell us the value of max and min of your input audio[]
Now lets focus on your desired output buffer self.buffer.floatChannelData ... since you are saying its 16 bit float ... what is the valid range here ? -1 < valid_value < 1 ?
We can continue once you tell us answers to these basic questions

Get/Modify single entries in non-contiguous submatrix views

I would like to access and modify single entries in a non-contiguous submatrix view. I tried it like this:
#include <armadillo> // version 5.200.2
int main()
{
arma::mat A(4, 4, arma::fill::zeros);
arma::uvec b(4);
b << 2 << 3;
auto view = A.elem(b, b);
view(0, 0) = 1.0; // Error: No operator()
}
This doesn't work because the expression returned by A.elem(b, b) appears to have no operator() defined. I found that the same thing works with contiguous views like e.g. submat(). Is there any solution/workaround for this or is it simply not possible in the non-contiguous case?

convert big yml data

There is any other way/trick to store the depth map in the database? Basically, we are trying to store 300000 double values. If it helps we can convert the array into a NSMutableArray or similar so that we can serialize it. I don’t know yet how to do it. I tried to find a way to convert it to a binary file instead of ASCII but no luck yet.
You can save a lot of memory by storing the raw binary data in a BLOB.
If you don't have fixed rows and cols of your matrix, you can put at the beginning of the file two integers for rows and cols.
I'll add a simple example on how to save and load the data of matrix, preceded by rows and cols.
#include <opencv2/opencv.hpp>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
Mat1d m = (Mat1d(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
Mat1d n;
{
ofstream ofs("test.bin", fstream::binary);
ofs.write((char*)&m.rows, sizeof(int)); // Save rows
ofs.write((char*)&m.cols, sizeof(int)); // Save cols
ofs.write((char*)m.data, m.total()*sizeof(double)); // Save data
}
{
ifstream ifs("test.bin", fstream::binary);
int rows, cols;
ifs.read((char*)&rows, sizeof(int)); // Load rows
ifs.read((char*)&cols, sizeof(int)); // Load cols
n = Mat1d(rows, cols); // Resize the matrix according to rows, cols
ifs.read((char*)n.data, rows*cols*sizeof(double)); // Load data
}
// Now m and n are equal
return 0;
}
If you need further compression you can read and write the stream using gzstream

C++ memory issue

I'm currently building a prime number finder, and am having a memory problem:
This may be due to a corruption of the heap, which indicates a bug in PrimeNumbers.exe or any of the DLLs it has loaded.
PS. Please don't say to me if this isn't the way to find prime numbers, I want to figure it out myself!
Code:
// PrimeNumbers.cpp : main project file.
#include "stdafx.h"
#include <vector>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Until what number do you want to stop?");
signed const int numtstop = Convert::ToInt16(Console::ReadLine());
bool * isvalid = new bool[numtstop];
int allattempts = numtstop*numtstop; // Find all the possible combinations of numbers
for (int currentnumb = 0; currentnumb <= allattempts; currentnumb++) // For each number try to find a combination
{
for (int i = 0; i <= numtstop; i++)
{
for (int tnumb = 0; tnumb <= numtstop; tnumb++)
{
if (i*tnumb == currentnumb)
{
isvalid[currentnumb] = false;
Console::WriteLine("Error");
}
}
}
}
Console::WriteLine(L"\nAll prime number in the range of:" + Convert::ToString(numtstop));
for (int pnts = 0; pnts <= numtstop; pnts++)
{
if (isvalid[pnts] != false)
{
Console::WriteLine(pnts);
}
}
return 0;
}
I don't see the memory problem.
Please help.
You are allocating numtstop booleans, but you index that array using a variable that ranges from zero to numtstop*numtstop. This will be severely out of bounds for all numstop values greater than 1.
You should either allocate more booleans (numtstop*numtstop) or use a different variable to index into isvalid (for example, i, which ranges from 0 to numstop). I am sorry, I cannot be more precise than that because of your request not to comment on your algorithm of finding primes.
P.S. If you would like to read something on the topic of finding small primes, here is a link to a great book by Dijkstra. He teaches you how to construct a program for the first 1000 primes on pages 35..49.
Problem is that you use native C++ in managed C++/CLI code. And use new without delete of course.
`currentnumb` :
is bigger than the size of the array, which is just numtstop. You are probably going out of bound, this might be your issue.
You never delete[] your isvalid local, this is a memory leak.

Resources