|
I've got friend suggesting me to convert cons char* variable to char* by copying the data. I mean using: char *x = strdup(const char *y).
But I think I can easily typecasting it, like this: x = (char *) y.
Am I doing a good coding practice here because I prefer typecasting it I don't even get warning for doing this.
Any way are they the same? Which one should be a good programmer do?
THX!!!!
From Indonesia with love..!!
|
|
|
|
|
auralius wrote: Am I doing a good coding practice here
No. Casting away const is bad. Without the const it implies
the location is writable, which it is not, otherwise it wouldn't
be const
You'd only need a strdup() duplicate of the string if you're going to
alter its contents.
Static casting is rarely necessary so I always consider it bad practice.
Casts are not for fixing compiler errors and warnings!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Casting is always "dangerous", so think what you do. It is really often the course of strange bugs and problems, which appear months or years after writing the crap. (believe me)
Types are your helpers, if it is a const think why!!! Why isnt char* x const?
x = (char *) y : the same string to which are pointing two pointers.
In your case with strdup you got another string !!!
(you must understand that, or stop coding )
PS: I compile my Release builds always with warning level 4
Greetings from Germany
|
|
|
|
|
yup....i think you booth are right..
i've got some old functions which return a const char* variable...
that's confusing me since i need also to modify the result...
thanks kastenk
thanks mark
From Indonesia with love..!!
|
|
|
|
|
hi
i want to create a property on my object and
type of it is system::io::ports::serialport , but i can't !
how can i create this property ?
|
|
|
|
|
iman_kh wrote: i want to create a property on my object
What is a property on an object?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
iman_kh wrote: i want to create a property on my object
do you mean property for your class? If yes see the below code
property System::IO::Ports::SerialPort^ SampleProperty
{
System::IO::Ports::SerialPort^ get(){
}
void set(System::IO::Ports::SerialPort^ port){
}
}; C++/CLI also has a simple way to declare the properties. You just need to write
property System::IO::Ports::SerialPort^ SampleProperty; . Compiler generates the backing fields for you.
|
|
|
|
|
hi,
yes
so thanks for your help,this was helpful
|
|
|
|
|
I am using Visual c++ NET 2005.
How can I get the system date ?
|
|
|
|
|
There is DateTime class in .Net Framework.
|
|
|
|
|
Use DateTime class to get the system date. The Now method returns the current system time.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
|
I want Copying the Data from an ASCII Text File on a PC Back to an Operating System Spooled File for Iseries don't use ftp.
thanks very much.
|
|
|
|
|
It is rude to cross post.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
I'm trying to write a function but I believe my parameters are wrong.
void printWinner(matchWon, gameWon, gameLost)
{
cout << endl << endl
<< "Player Matches Games Games" << endl;
<< " Won Won Lost " << endl;
<< "------ ------- ----- -----" << endl;
<< " A " << matchWonA << " " << gameWonA
<< " " << gameLostA << endl;
<< " B " << matchWonB << " " << gameWonB
<< " " << gameLostB << endl;
<< " C " << matchWonC << " " << gameWonC
<< " " << gameLostC << endl;
This being my code, I was wondering if I should give a function to each of my players (A, B, C).
So, void printAScore (matchWonA, gameWonA, gameLostA)
void printBScore (matchWonB, gameWonB, gameLostB)
and same thing with C.
What is the most efficient way of doing this?
|
|
|
|
|
kclkl18 wrote: What is the most efficient way of doing this?
You don't show valid C++ code, and you're wondering about efficiency?
Please post on the correct board:
Visual C++/MFC Board[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
kclkl18 wrote:
What is the most efficient way of doing this?
Reading a good C++ tutorial.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Any ideas why the following code runs faster (19 vs 37 msec in the first test) with /clr flag on? No clr is used in the code at all!
#include "stdafx.h"
class myClass
{
};
int main()
{
clock_t start;
const int COUNT = 100000;
myClass* objects[COUNT];
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start));
start = clock();
for (int i = 0; i < COUNT; i++){
objects[i] = new myClass();
}
clock_t start2 = clock();
for (int i = 0; i < COUNT; i++){
delete objects[i];
}
printf("Native without delete: %f\n", (double)(start2 - start));
printf("Deletion: %f\n", (double)(clock() - start2));
printf("Total: %f\n", (double)(clock() - start));
}
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Because managed code ROCKS!
But seriously...
gajatko wrote: 19 vs 37 msec
It's unlikely your tick count has a resolution better that 19ms. That
looks suspiciously like the difference between a single tick.
Maybe try timing something that takes a few seconds or use
the performance counter for timing.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
With
const int COUNT = 10000000;
myClass** objects = new myClass*[COUNT];
(heap because of stackoverflow)
I got 3582 native and 1612 with /clr
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
gajatko wrote: I got 3582 native and 1612 with /clr
Which numbers are you reporting there?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start) / CLOCKS_PER_SEC * 1000);
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Try this (and do NOT run in the debugger!)
class myClass
{
};
int _tmain(int argc, _TCHAR* argv[])
{
const int COUNT = 1000000;
myClass** objects = new myClass*[COUNT];
clock_t start1 = clock();
for (int i = 0; i < COUNT; i++)
{
objects[i] = new myClass();
delete objects[i];
}
clock_t end1 = clock();
clock_t start2 = clock();
for (int i = 0; i < COUNT; i++){
objects[i] = new myClass();
}
clock_t end2 = clock();
clock_t start3 = clock();
for (int i = 0; i < COUNT; i++){
delete objects[i];
}
clock_t end3 = clock();
printf("Loop with new/delete: %d\n\n", end1 - start1);
printf("Loop with new: %d\n", end2 - start2);
printf("Loop with delete: %d\n", end3 - start3);
printf("Total of loop new and loop delete: %d\n", end3 - start2);
printf("\n\nPress enter to end...");
getchar();
delete[] objects;
return 0;
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: Try this (and do NOT run in the debugger!)
Did you look something like this[^] while typing that in?
led mike
|
|
|
|
|
LMAO! You betcha!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|