which is the faster in windows

Pages: 12
which is the faster in windows , 1 or 2
1)

1
2
3
4
5
6
7

wstring write_stuff = generate_string(happy);

wstring generate_string(wstring happy)
{

}


2
1
2
3
4
5
6
LPWSTR write_stuff  =  generate_string(happy);

LPWSTR generate_string(LPWSTR happy)
{

}

that means using class is faster or LPWSTR?

besides,for a same function program(achieve the same function)
why c++ is faster than C ?
Last edited on
closed account (S6k9GNh0)
For those who do not know, LPWSTR is a point to wchar_t or wchar_t *.

Well in theory, wchar_t * wins in performance simply because a pointer is almost always smaller than something huge like a string. They aren't really comparable though.
using wstring class function
or
using wstring as a parameter

will slower the proccess then
then i want to ask Long Pointer is * or & ?
it should be * , pass by value, right?
Last edited on
then i want to ask Long Pointer is * or & ?
Long Pointers are like other pointers, so *
it should be * , pass by value, right?
With * is pass by address

using wstring as a parameter will slower the proccess then
It will if you are passing by value: wstring generate_string(wstring happy) as you have to copy an object of wstring
but if you pass by reference it shouldn't: wstring generate_string(const wstring &happy)
Last edited on
thanks for bazzy
i think find out i can not use LPWSTR as a function return type, it is not allowed

1
2
3
4
5
6
LPWSTR write_stuff  =  generate_string(happy);

LPWSTR generate_string(LPWSTR happy)
{

}


btw, * and & also means pass by address?

don't * means pass by value?
Last edited on
i think out i can not use LPWSTR as a function return type, it is not allowed
LPWSTR is a pointer to wchar_t so you can use it as all other basic types and you can use it as return type


btw, * and & also means pass by address?
don't * means pass by value?
* and & have different meanings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Function declaration
int f1 ( int i ); //Pass by value
int f2 ( int &i ); //Pass by reference
int f3 ( int *i ); //Pass by address

// Function call
int number = 123;
int *pointer = &number;
f1 ( 456 ); // pass by value
f1 ( number ); //pass by value
f1 ( *pointer ); //pass by value
f2 ( number ); // pass by reference
f2 ( *pointer ); // pas by reference
f3 ( &number ); //pass by address
f3 ( pointer ); // Here you are passing 'pointer' by value but its value is a memory address  
hello , bazzy

can u help me double check your example above

i am so confuse now, and think that there are 3 mistake in above exmaple?

actually , what is pass by reference?
and
what is pass by address?

what is the differnece between "pass by reference" and "pass by Value"?
would u provide example each?

big big thanks
Last edited on
what is pass by reference?
what is the differnece between "pass by reference" and "pass by Value"?

When passing an argument by value, the parameter of the function is a copy of the argument passed.
When passing by reference, the parameter works on the same memory space as the argument. The result is that all the changes you do to the parameter will affect the value of the argument passed. ( If you pass by const reference const TYPE & you avoid copying the argument but it won't be modified )
http://www.cplusplus.com/doc/tutorial/functions2/

what is pass by address?
Is when you use a pointer as parameter


Pass by value:
1
2
3
4
5
6
7
8
9
10
11
12
13
void f ( int param )
{
    param++;
    cout << param;
}

f ( 1 ); // valid

int x = 5;
f ( x ); // valid, x is not changed by the function

int *ptr = new int;
f ( *ptr ); // valid, the value of the dynamically allocated int is not increased 

Pass by reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void f ( int &param )
{
    param++;
    cout << param;
}

f ( 1 ); // not valid

int x = 5;
f ( x ); // valid, x is changed by the function: all the changes to the parameter will affect the argument
// x == 6

int *ptr = new int;
f ( *ptr ); // valid, the value of the dynamically allocated int is increased 

Pass by address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void f ( int *param )
{
    (*param)++;
    cout << param;
}

f ( 1 ); // not valid

int x = 5;
f ( x ); // not valid
f ( &x ); // valid, &x returns the address of 'x' and the value of x is changed by (*param)++. 
        //The value shown by cout << param is the hex representation of the memory address of 'x'
// x == 6

int *ptr = new int;
f ( ptr ); // valid, the value of the dynamically allocated int is increased

f ( NULL ); // valid but will cause runtime error as (*param)++ will try to change a value which doesn't exist 



thanks for you, bazzy

now i can distinguish pass by refernece and pass by value, the after will not be affect in caller(call function)


but i can't distinguish pass by refernece and pass by address ~

pass by value , refernece or address is depend on called function , not caller(call function), right?

and one more thing that, can u tell me your sequence of pass by address?

in my brain, pass by address means caller pass the parameter address to called function !
(i don't want to seperate arugment and parameters)

in any situation caller is run in front of called function
i don't want to make you mad , Bazzy :)

the most important thing is :

what should i add before parameter (parameter is in function prototype)

when i want
the value change in function
effect the call function?

and

what should i add before parameter (parameter is in function prototype)

when i don't want
the value change in function
effect the call function?

Last edited on
let's say you have the int variable 'x' at position 0xFEEF1E in memory with value 123, you have func_ref which takes an argument by reference, and func_add which takes the argument by address.
func_ref ( x ) the parameter will be an int with value 123 at position 0xF00 -exacly the same as 'x'
func_add ( x ) the parameter will be a pointer ( int* ) with value 0xFEEF1E at position 0xF0EF00
Last edited on
actually , i can't distinguish when i should use & at call function , when i should use & at called function..

1
2
3
4
5
6
7
8
9
10
11
12
13
main()
{
   int x =123; //memory position = 0xFEEF1E 
   func_ref ( x ) ;
  func_ref ( x )
}

func_ref ( & x ) // in ref (), x value = 123 , memory position = 0xF00 
{  }

func_add( * x ) // in add(), x value = 0xFEEF1E , it's a pointer ( int* ), memory position = 0xF0EF00
{  }


no matter pass by reference &
or
pass by address *,

it will use 2 memory address
Last edited on
Forget my last post, I've edited it wrong and I had to go.
Here is the right version of it:

let's say you have the int variable 'x' at position 0xFEEF1E in memory with value 123, you have func_ref which takes an argument by reference, and func_add which takes the argument by address.
func_ref ( x ) the parameter will be an int with value 123 at position 0xFEEF1E -exacly the same as 'x'
func_add ( &x ) the parameter will be a pointer ( int* ) with value 0xFEEF1E at position 0xF0EF00

You have to use & in the function parameter (the function prototype) when passing by reference, in the argument (the function call) when passing it by address
OK, Bazzy, Thanks
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  int x =123;  // x value = 123 , memory position =  0xFEEF1E 
  func_ref (x) ;
  func_add(&x) ;
}

func_ref (&x) // in ref (), x value = 123 , memory position =  0xFEEF1E 
{   }

func_add(&*x) // in func_add , x value = 0xFEEF1E , memory position = 0xF0EF00
{  }


func_add which takes the argument by address.

func_add ( &*x ) the parameter will be a pointer ( int* ) with value 0xFEEF1E at position 0xF0EF00

You have to use &
in the function parameter (the function prototype) when passing by reference; (called function)

You have to use &
in the argument (the function call) when passing arugment by address

pass by refernce will not use one more address, the value is memory address~ ~

so what i need to do for using x ? if i want to calculate b= x*1000


do you type
func_add ( &x ){}
wrong?

it should be
func_add ( *x ) {}
Last edited on
If x is a variable ( int x; ) and you are passing by address, you should use &x:

&variable = memory address
*pointer = value

here is an example:
1
2
3
4
5
6
7
8
9
10
11
int variable = 1234;

int *pointer;

pointer = &variable; // set pointer to the value of the memory address of 'variable'

cout << *pointer; // display the value of 'variable'
cout << *variable; // Wrong: * unary operator doesn't exist for the type int

cout << &pointer; // display the memory address of pointer
cout << &variable; // display the memory address of variable 


Is easy to confuse * and & if you aren't fluent with pointer/reference sintax as they have different meanings.
If you want a better explanation than what I can give you, you should read these tutorials:
http://www.cplusplus.com/doc/tutorial/pointers/ -explains pointers and & * operators
http://www.cplusplus.com/doc/tutorial/functions2/ -explains references in the first section
so pass address not means the function prototype's parameter must be * in front of parameter , right?

Besides,
can i haven't initialized p1 as
int *p1;

and use this

*p1 = &firstvalue; // p1 = 0xefefefef
Last edited on
will it change the arugment value when using passing by address , after cpu finish the called function?
Pages: 12