specialaizing templates --error

T

Thomas

Hello,
I 'm writitng a simple program which contains the following template


template <class T> int exch(T& t1 ,T& t2){
T tmp ;
tmp = t1;
t1 = t2;
t2 = tmp;
return 1;
}

When i run this for the elements of such table :
char* imiona [] = {"anastazja", "maja", "gucio", "aleksander",
"klementyna"};
from another function the program crushes. For other types templates runs
fine. What should I do ?, thx
 
M

Marcus Kwok

Thomas said:
Hello,
I 'm writitng a simple program which contains the following template


template <class T> int exch(T& t1 ,T& t2){
T tmp ;
tmp = t1;
t1 = t2;
t2 = tmp;
return 1;
}

This function looks very similary in functionality to what std::swap()
(found in said:
When i run this for the elements of such table :
char* imiona [] = {"anastazja", "maja", "gucio", "aleksander",
"klementyna"};

This should be:

const char* imiona[] = {"anastazja", "maja" /*, ... */ };

Note the 'const'.
from another function the program crushes. For other types templates runs
fine. What should I do ?, thx

What happens, and how are you using it? Please see FAQ #5.8. Also,
consider using std::swap() (see above) if it meets your requirements.
 
F

Fei Liu

Thomas said:
Hello,
I 'm writitng a simple program which contains the following template


template <class T> int exch(T& t1 ,T& t2){
T tmp ;
tmp = t1;
t1 = t2;
t2 = tmp;
return 1;
}

When i run this for the elements of such table :
char* imiona [] = {"anastazja", "maja", "gucio", "aleksander",
"klementyna"};
from another function the program crushes. For other types templates runs
fine. What should I do ?, thx
It'd be helpful to show us the code you use exch with imiona. I'd guess
you are not taking care of pointers and corrupting memory.

Fei
 
K

Kai-Uwe Bux

Thomas said:
Hello,
I 'm writitng a simple program which contains the following template


template <class T> int exch(T& t1 ,T& t2){
T tmp ;
tmp = t1;

make those two lines:

T tmp ( t1 );

This way, you can use the function for types that are not default
constructible.
t1 = t2;
t2 = tmp;
return 1;

Don't return anything. Just change the signature so that the function
returns void.

Oops, now that I fixed it, it looks like std::swap.

When i run this for the elements of such table :
char* imiona [] = {"anastazja", "maja", "gucio", "aleksander",
"klementyna"};
from another function the program crushes. For other types templates runs
fine.

Without seeing the code, I just have to guess: you initialize an array of
char* from an array of char const *. This is allowed by the language for
backward compatibility with C functions that use char* where char const *
should be used. However, you might end up modifying const data. That is
undefined behavior and can resul in crashes.

For better answers, please post a minimal but complete program the shows the
problem.

What should I do ?

a) Use std::vector instead of a built-in array.
b) Use std::string instead of char*.
c) Use std::swap instead of exch.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top