JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
[ 1 2 ]    NEXT >
memset ...! | Reply
wat i understood from misof's article is that by filling the memory with bytes...taking care not to set the signed bit will ensure a sufficiently large value. I am also assuming that signed bit is the MSB.
i am usually wary of the idea of memset...but decided to try...only to result in spending a frustrating 1 hour trying to debug my code.


can anyone tell me why replacing the line
option 1 works for all test cases while option 2 fails for certain cases.
i am basically trying to set arbitrarily large vaues in the cnt array.

long long cnt[1<<15];
//option 1 
for(int i=1; i<(1<<15); i++) cnt[i]=10000; 
//option 2
memset(cnt,63,1<<15); 
cnt[0]=0;
Re: memset ...! (response to post by smartnut007) | Reply
I've never used memset before, but from what I read in misof's article, I think the third parameter you pass to memset is the number of bytes to fill. Since you're array is of long long's, there'll be 8 bytes per element so you're only actually filling an eighth of the elements in the array. You'll need to use 1<<18 instead of 1<<15 in the memset call.
Re: memset ...! (response to post by aussie) | Reply
Or if you want to increase portability then (1<<15)*sizeof(long long). Of course since long long isn't standard... this doesn't really increase portability.
Re: memset ...! (response to post by Ryan) | Reply
lol, that's a great post. :-)
Re: memset ...! (response to post by Ryan) | Reply
Unless you're using it in C, where "long long" is standard.
Re: memset ...! (response to post by smartnut007) | Reply
I just use memset(array,value,sizeof(array)) :-)
Re: memset ...! (response to post by Ryan) | Reply
You are all insane. This is C++ not C:
vector<long long> vl(1<<12, MAX);
vl[0]=0;
Re: memset ...! (response to post by tolkienfan) | Reply
And should you need to reset it later, there's fill and fill_n (in algorithm), which are, according to my tests, no slower than memset:
fill( vl.begin(), vl.end(), 10000 );
fill( vl.begin(), vl.begin()+(1<<15), 10000 );
fill_n( vl.begin(), 1<<15, 10000 );
Re: memset ...! (response to post by lovro) | Reply
thanks ,i shall remember to use fill func in the future.

but , i guess memset is more useful to prefill mulitdimensional arrays.

and from the above posts i discern that
memset(_array_name,value,sizeof(_array_name));
is the most general way to use memset .
Re: memset ...! (response to post by smartnut007) | Reply
You can fill multidimensional arrays with fill_n as well. (In fact, I sometimes use that.)
int x[200][200];
 
fill_n((int*)x,sizeof(x)/sizeof(int),value);

This is more general than memset, since you can use it for any value.
Re: memset ...! (response to post by smartnut007) | Reply
vector<vector<int> > vvi(30,vector<int>(50,-1));
Re: memset ...! (response to post by tolkienfan) | Reply
what if i want to initialize a 3D array ..

i tried the following but it doesnt work

vector < vector < vector<int> > > vvvi(10,vector<int>(2,vector<int>(10,5)));
 
for(int k=0;k<10;k++)
for(int i=0;i<2;i++)
{
cout<<"\n";
for(int j=0;j<10;j++)
cout<<" "<<vvvi[k][i][j];
}
Re: memset ...! (response to post by rajeshrathod) | Reply
Your initialization needs to be:
vector < vector < vector <int> > > vvvi(10,vector < vector <int> >(2,vector <int>(10,5)));
Re: memset ...! (response to post by rajeshrathod) | Reply
That's horrible. Apart from the uglyness of the declaration of a vector of vectors of vectors (which isn't too bad for STL really), in a multi-dimensional array the overheads of vector::operator[]() really start to stack up.
Re: memset ...! (response to post by StevieT) | Reply
thanks !!
it works !!
[ 1 2 ]    NEXT >

RSS