|
The example is for a vector<int> and in this case its better returning by value, but in cases where we have a vector<my_object> and the object class is big its better returning a reference or a pointer. Just would like to know in those cases which one is better?
Thanks
|
|
|
|
|
Neither - always return by value from a vector. Don't mess about with storing pointers or references. And if you have to store something by reference use a shared_ptr.
If you write the class being stored in the vector correctly (fast default construction, move constructor, transactional exception safe assignment operator, using named return values) copying objects out of collections is very fast.
Cheers,
Ash
|
|
|
|
|
If there are some directories and each directory contains some files then i want to make a program which will compare each file of any directory with each file of all directories.
Can someone suggest me a best approach which works faster and dont miss any comparision.
|
|
|
|
|
Hi friend,
use findfirstfile() and findnextfile()
msdn api's.....hope this will help you
|
|
|
|
|
Hi,
Thanks for your reply. FindFirst/Next file will give me file name.
Here i asked about the approach i.e. container and logic to implement comparision which should be faster and reliable.
|
|
|
|
|
It really depends on what kind a value you wish to return from the function.
For instance I can envisage two basic approaches - true/false for same/not same, or a -/0/+ value as strcmp does.
Here's an approach that just returns true/false.
I've timed it - it takes 6 seconds to compare a 347MB video file with itself - Win7 Home Premium x64, Intel i3 @ 2.13Ghz, 4GB ram - gcc 4.4.1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void loadFile(char *szFilename, size_t &sizeOut, char* &bufferOut)
{
FILE *fp;
fp = fopen(szFilename, "rb");
fseek(fp, 0, SEEK_END);
sizeOut = ftell(fp);
fseek(fp, 0, SEEK_SET);
bufferOut = (char*) malloc(sizeOut);
fread(bufferOut, 1, sizeOut, fp);
fclose(fp);
}
bool isSame(char *szFilename1, char *szFilename2)
{
size_t len1, len2;
long curPos;
char *buffer1, *buffer2;
bool result = true;
loadFile(szFilename1, len1, buffer1);
loadFile(szFilename2, len2, buffer2);
if (len1 == len2)
{
for (curPos=0; curPos<len1; curPos++)
{
if (buffer1[curPos] != buffer2[curPos])
result = false;
}
}
else
result = false;
free(buffer1);
free(buffer2);
return result;
}
int main()
{
if (isSame("testVideo.avi", "testVideo.avi"))
printf("Same\n");
else
printf("Not Same\n");
}
|
|
|
|
|
for SuperDuper[^] i did this:
0. build the list of files to compare.
1. read the first 1000 bytes of each file to compare.
2. from those 1000 bytes, generate an SHA1 hash.
3. store the hashes, file size, and file names in a list
4. sort the list by hash values. duplicates will clump together.
5. for hash matches, do a full file compare.
|
|
|
|
|
Chris Losinger wrote: 2. from those 1000 bytes, generate an SHA1 hash.
3. store the hashes, file size, and file names in a list
Why would you want to waste all those cpu cycles by calculating a SHA1 hash? If you know that the maximum number of bytes you're going to read is 1000 then you could have used an inexpensive hashing algorithm... there are dozens of inexpensive hash algorithms that will have a collision probability of zero within 1000 bytes. Your application would probably have a huge performance boost. Not to mention... the SHA1 hash will produce 20 bytes of data to store... you could reduce your database size by 50% just by moving to a more efficient algorithm.
Were you initially planning on hashing the entire file or something?
Best Wishes,
-David Delaune
|
|
|
|
|
it's actually an MD5, but can be switched to SHA1 or whatever, at compile time. (wrote the OP from memory, forgot the default).
Randor wrote: If you know that the maximum number of bytes you're going to read is 1000 then you could have used an inexpensive hashing algorithm.
probably. but the goal was to avoid collisions while using a hash method that was built into the .Net framework. this was a quick utility project. and really, calculating MD5 on 1K is definitely not the bottleneck here; the file scanning and open/read/close totally dominates the process - especially when those files are on a network drive somewhere.
Randor wrote: Were you initially planning on hashing the entire file or something?
actually, yes. (and that's what happens when the 1K scan finds matches). the partial hash pre-scan was a later addition, and i just reused the hash stuff that was already written instead of coming up with a new method.
modified 25-Apr-12 7:48am.
|
|
|
|
|
Hi friends,
I'm trying to create My own service for .exe.
my service create function successfully creates service but when i try to start to that service
then it gives error code 1053 which is ERROR_SERVICE_REQUEST_TIMEOUT. also i saw that startservice() fun goes in waiting for 30seconds
and then gives error 1053.
pls help,
here is my code for ur ref...
hServiceMGROpen=OpenSCManager(serverName,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if(hServiceMGROpen!=NULL)
{
hServiceOpen=OpenService(hServiceMGROpen,"myService",SERVICE_ALL_ACCESS);
if(hServiceOpen!=NULL)
{
succStart=StartService(hServiceOpen,0,NULL);
if(succStart)
{
printf("Service starts successfully ");
}
else
{
DWORD err=GetLastError();
printf("Error %d",err);
}
}
}
Thanks in advance...!!!
Sangam
modified 24-Apr-12 5:46am.
|
|
|
|
|
What does it mean:
here is my code for ur ref...
Anyway, ERROR_SERVICE_REQUEST_TIMEOUT, means that your ServiceControlHandler (or whatever name you have used to register handler callback) is not responding fast enough to the setvice start request, blocking StartServiceCtrlDispatcher. Or you did not call StartServiceCtrlDispatcher at all.
If you have to perform some lenghty procedure when the service starts, do it in the thread, or after setting service start status.
JohnCz
|
|
|
|
|
here is my code for ur ref means
i'm trying to show you that what i have done
|
|
|
|
|
Hi,
Where is your code?
Another thing: Could you lease refrain from using cryptic code like "ur ref means"? I would really appreciate it, since to understand it I need translation to English.
JohnCz
|
|
|
|
|
hServiceMGROpen=OpenSCManager(serverName,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if(hServiceMGROpen!=NULL)
{
hServiceOpen=OpenService(hServiceMGROpen,"myService",SERVICE_ALL_ACCESS);
if(hServiceOpen!=NULL)
{
succStart=StartService(hServiceOpen,0,NULL);
if(succStart)
{
printf("Service starts successfully ");
}
else
{
DWORD err=GetLastError();
printf("Error %d",err);
}
}
}
this is my code....!!!!
please findout problem....
|
|
|
|
|
I have dll that export funtion and on of the parameters is C-style strings a serial number.
extern "C" int __stdcall calc(char *SerialNumber)
The parameters can be come from VB, C#, C++ , C
I want to make som basic indata validation check that C-style is good kondition. That point is
not pointing at null and that the array is null-terminated. Somebody can point me in the right
direction. I don't want buffer over flow with staring to handel the string. Code sample?
|
|
|
|
|
Why don't you perform the input validation inside your DLL's function?
Veni, vidi, vici.
|
|
|
|
|
Hi, I am using mpi and cuda which is basically like a c programming. Just confused about the pointers here. Some help would be appreciated.
my cuda code
#include <stdio.h>
__global__ void add(int *devarray, int *devsum)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
*devsum = *devsum + devarray[index];
}
extern "C"
int * run_kernel(int array[],int nelements)
{
int *devarray, *sum, *devsum;
sum =(int *) malloc(1 * sizeof(int));
printf("\nrun_kernel called..............");
cudaMalloc((void**) &devarray, sizeof(int)*nelements);
cudaMalloc((void**) &devsum, sizeof(int));
cudaMemcpy(devarray, array, sizeof(int)*nelements, cudaMemcpyHostToDevice);
add<<<2, 3>>>(devarray, devsum);
cudaMemcpy(sum, devsum, sizeof(int), cudaMemcpyDeviceToHost);
printf(" \nthe sum is %d\n", *sum);
cudaFree(devarray);
cudaFree(devsum);
return sum;
}
This function returns int * as sum.
my MPI code
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAYSIZE 2000
#define MASTER 0
int data[ARRAYSIZE];
int main(int argc, char* argv[])
{
int numtasks, taskid, rc, dest, offset, i, j, tag1, tag2, source, chunksize, namelen;
int mysum;
long sum;
int update(int myoffset, int chunk, int myid);
char myname[MPI_MAX_PROCESSOR_NAME];
MPI_Status status;
double start = 0.0, stop = 0.0, time = 0.0;
double totaltime;
FILE *fp;
char line[128];
char element;
int n;
int k=0;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Get_processor_name(myname, &namelen);
printf ("MPI task %d has started on host %s...\n", taskid, myname);
chunksize = (ARRAYSIZE / numtasks);
tag2 = 1;
tag1 = 2;
if (taskid == MASTER){
fp=fopen("integers.txt", "r");
if(fp != NULL){
sum = 0;
while(fgets(line, sizeof line, fp)!= NULL){
fscanf(fp,"%d",&data[k]);
sum = sum + data[k];
k++;
}
}
printf("Initialized array sum %d\n", sum);
offset = chunksize;
for (dest=1; dest<numtasks; dest++) {
MPI_Send(&offset, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);
MPI_Send(&data[offset], chunksize, MPI_INT, dest, tag2, MPI_COMM_WORLD);
printf("Sent %d elements to task %d offset= %d\n",chunksize,dest,offset);
offset = offset + chunksize;
}
offset = 0;
mysum = run_kernel(&data[offset], chunksize);
printf("Kernel returns sum %d", mysum);
for (i=1; i<numtasks; i++) {
source = i;
MPI_Recv(&offset, 1, MPI_INT, source, tag1, MPI_COMM_WORLD, &status);
MPI_Recv(&data[offset], chunksize, MPI_INT, source, tag2,MPI_COMM_WORLD, &status);
}
MPI_Reduce(&mysum, &sum, 1, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);
printf("\n*** Final sum= %d ***\n",sum);
}
if (taskid > MASTER) {
start= MPI_Wtime();
source = MASTER;
MPI_Recv(&offset, 1, MPI_INT, source, tag1, MPI_COMM_WORLD, &status);
MPI_Recv(&data[offset], chunksize, MPI_INT, source, tag2,MPI_COMM_WORLD, &status);
mysum = run_kernel(&data[offset], chunksize);
printf("\nKernel returns sum %d ", mysum);
stop = MPI_Wtime();
time = stop -start;
printf("time taken by process %d to recieve elements and caluclate own sum is = %lf seconds \n", taskid, time);
dest = MASTER;
MPI_Send(&offset, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);
MPI_Send(&data[offset], chunksize, MPI_INT, MASTER, tag2, MPI_COMM_WORLD);
MPI_Reduce(&mysum, &sum, 1, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);
}
MPI_Finalize();
}
Now here mysum = run_kernel(&data[offset], chunksize);
gets the sum and stores into mysum.
Here mysum should be pointer, iguess but then
MPI_Reduce(&mysum, &sum, 1, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);
does not work properly and system throws memory error.
here is the output of above code -
MPI task 2 has started on host 4
MPI task 3 has started on host 4
MPI task 0 has started on host 4
MPI task 1 has started on host 4
Initialized array sum 9061
Sent 500 elements to task 1 offset= 500
Sent 500 elements to task 2 offset= 1000
Sent 500 elements to task 3 offset= 1500
run_kernel called..............
the sum is 10
Kernel returns sum 159300360 time taken by process 2 to recieve elements and caluclate own sum is = 0.290016 seconds
run_kernel called..............
the sum is 268452367
run_kernel called..............
the sum is 10
Kernel returns sum 145185544 time taken by process 3 to recieve elements and caluclate own sum is = 0.293579 seconds
run_kernel called..............
the sum is 1048
Kernel returns sum 156969736 time taken by process 1 to recieve elements and caluclate own sum is = 0.297599 seconds
Kernel returns sum 152148496
*** Final sum= 613604136 ***
The final sum and initialized sum is not matching.
Any idea what is going wrong?
Thanks
|
|
|
|
|
hello guys... I am trying to load a bitmap into the picture control. Well when I debug, I see that the picture gets loaded but it does not show onto the control. I load the first bitmap at design time like this: Properties->Image->IDB_BITMAP1 . Then to load second image, I write following code in the click event of button. Here is what I am trying.
void CMyAppDlg::OnBnClickedNext()
{
HBITMAP bitmap [2];
bitmap[1] = ::LoadBitmapW(NULL, _T("All_The_Path\\Resources\\quaid.bmp"));
bitmap[0] = ::LoadBitmapW(NULL, _T("All_The_Path\\Resources\\Allama.bmp"));
if(CurrentImage == 0) {
m_Picture.SetBitmap(bitmap[1]); CurrentImage = 1;
}
else if(CurrentImage == 1)
{
m_Picture.SetBitmap(bitmap[0]); CurrentImage = 0;
}
}
What am I doing wrong? Thanks for any input.
This world is going to explode due to international politics, SOON.
modified 24-Apr-12 5:32am.
|
|
|
|
|
|
Thanks for the link. So I know another way for task.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
What happens to your HBITMAP variable when the function returns?
|
|
|
|
|
This is what I did.
CString strFileName = _T("All_The_Path\\Resources\\quaid.bmp");
bitmap[0] = ::LoadBitmapW(NULL, strFileName);
But I should have done this.
CString strFileName = _T("All_The_Path\\Resources\\quaid.bmp");
bitmap[0] = (HBITMAP)::LoadImage(NULL, strFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
Thanks for your time.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Yeah, but what SandipG was getting at is you have a memory leak - you load both images each time the button gets pressed.
You're better off making HBITMAP bitmap[2]; a member variable of the dialog instead of local variables in the button press-code. Then you load both images in OnInitDialog.
In the button-press handler you work out which image to display then display it.
That way, you only call LoadImage once per image.
|
|
|
|
|
Thanks for this useful tip.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
I was working in Windows Vista, and I moved the project over to XP to create the setup and deployment program.
I guess on the XP, I have some small details to fix. At first I thought it was just a Vista thing, I looked up the function, at it is Vista and up.
Not sure how to proceed on this, looking for suggestions.
Is there a way to use the ex and non ex version of the function, and to somehow keep them seperate?
|
|
|
|
|