|
Hello
Thanks again for your reply! I got two more questions . Take a look at this code;
char* MyText = new char[5];
cin>>MyText;
MyText = new char[10];
delete[] MyText;
Now , I called a second new without deleting the first one. Is there a leak here??
What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed?
Or will it be permanently locked until reboot?
Regards
|
|
|
|
|
Nader Elshehabi wrote: Is there a leak here??
Yes.
Nader Elshehabi wrote: Will the memory be freed?
Yes.
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hello
Short, and straight answers!!
Thanks
Regards
|
|
|
|
|
Nader Elshehabi wrote: Now , I called a second new without deleting the first one. Is there a leak here??
Are you coming from a Java/C# background by chance? Yes, this will result in a memory leak. Whenever you use new, you must have a delete call to match it. Having 2 calls to new and only 1 call to delete means here is a leak.
Nader Elshehabi wrote: What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed?
Or will it be permanently locked until reboot?
When the program exits its memory will be freed. You do NOT want to rely on this, though. Always clean up your memory properly.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hello
Zac Howland wrote: Are you coming from a Java/C# background by chance?
Well, somehow ! I was a good old timer C++ programmer . But last time I ever wrote C++ code was more than 3 years ago. So I got really rusty, coming back these days to C++ as a new comer.
So, while refreshing all that good ol' C++ of mine, it's nice to have you guys around;)
Thanks again.
Extra Regards
|
|
|
|
|
That's code is a good the way to read a single line, try this instead:
getline(cin, input);
Steve
|
|
|
|
|
That still has the problem that he will need to limit his buffer size in order to prevent overrun problems. And the safer version is:
char buffer[100] = {0};
cin.getline(buffer, 99);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
You don't have to limit the size of the buffer, you use std::string . ie.
string line;
getline(cin, line);
Assumes using namespace std; and #include <iostream> and finally #include <string>
Steve
|
|
|
|
|
Ah, you are correct. I completely forgot about the general getline function ...
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Beside what Zac said, another suspicious area is
cin>>MyText;
When you expected MyText is one char, your input could easily be more than one char, which would cause a memory problem.
Best,
Jun
|
|
|
|
|
Hello Jun.
Actually my input field is char* , and I was expecting a text rather than just one char. Besides, this is nothing more than an experimental project which turned out to be disastrous!!;P
Regards
|
|
|
|
|
Try this instead.
#include <iostream>
#include <iomanip>
int main(int argc, char *argv[])
{
using namespace std;
char *pChar = new char[2];
cin >> setw(2) >> pChar;
delete [] pChar;
return 0;
}
You're reading too many charcters: you've got room for a NULL terminator (a string of zero length!) but your reading more and getting a buffer overrun. My code tells the IO classes how big the buffer is so it will not overrun it.
If you only want to read a single char you could try doing this (in your code):
cin >> *MyText;
Steve
|
|
|
|
|
Thanks Steve! I appreciate your response.
Regards
|
|
|
|
|
In the following program the control code seems to be returning to the start of void::main() after the last line of code has been executed:
void main ()
{
CSentenceList LoadFile("c:/testfile.txt", true);
for (int iIndex = 1; iIndex <= LoadFile.Size; iIndex++)
{cout << LoadFile.display(iIndex) << endl;}
cout << LoadFile.search("john") << endl;
}
Presumably after a number of attempts the program crashes.
crt0.c
...
if ( !managedapp ) // return value is 0 (false)
exit(mainret); // this is where I think the program should exit
...
Any idea why?
Jon
|
|
|
|
|
Have you debugged the code? I would first check if any iIndex value causes "out of range" in your LoadFile.display() method.
Best,
Jun
|
|
|
|
|
Actually that was the problem thanks.
Jon
|
|
|
|
|
probably won't fix the crash, but main should be declared as:
int main(int argc, char *argv[])
or
int main(int argc, char **argv)
(note: this is strictly for style reasons)
-- modified at 15:17 Thursday 31st August, 2006
|
|
|
|
|
|
|
Chris Losinger wrote: probably won't fix the crash, but main should be declared as:
int main(int argc, char *argv[])
or
int main(int argc, char **argv)
Microsoft's compiler allows you to define main in any of the following ways (all completely acceptable):
int main()
int main(int argc, char** argv)
int main(int argc, char** argv, char** env)
void main()
void main(int argc, char** argv)
void main(int argc, char** argv, char** env)
While the 3-parameter version and the void versions are not portable, they are fine for Win32 Console programming.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: Microsoft's compiler allows you to define main ...
oh, i know. and like i said, it probably wouldn't help fix the crash. just thought i'd point out a style issue.
|
|
|
|
|
Chris Losinger wrote: just thought i'd point out a style issue.
No problem. Of course, his entire problem goes away if the suggested changes for the new standard are approved with regard to string , where the following code would actually work:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cin >> input;
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hi, i want to create an SDI application without Frame And Menu bars.
And in this SDI application ,i split the window into 3 views.
How can i close all the views at a time by clicking one button in the top view?
|
|
|
|
|
Hello
I have written an application which is using the msflexigrid. The problem I am seeing is with a fast PC. Intel 3GHz P4 using Hyperthreading.
The problem is the cells do not always update. Below is a sample of code which updates a cell depending of the outcome of the function.
if(!(CTESTINTERFACE::SetupTxVcoTop()))
{
if(!(CTESTINTERFACE::SetupTxVcoTop()))
{
m_vco_parameter_grid.SetRow(2);
m_vco_parameter_grid.SetCol(3);
m_vco_parameter_grid.SetCellForeColor(RGB(255,0,0));
m_vco_parameter_grid.SetText("Failed");
UpdateWindow();
if(OnTestFailed("TxVcoTop") == FALSE)
return;
}
else
{
m_vco_parameter_grid.SetRow(2);
m_vco_parameter_grid.SetCol(3);
m_vco_parameter_grid.SetCellForeColor(RGB(0,255,0));
m_vco_parameter_grid.SetText("Passed");
UpdateWindow();
CMEASUREMENT::strPASS_FAIL.ialign_pass_count++;
}
}
else
{
m_vco_parameter_grid.SetRow(2);
m_vco_parameter_grid.SetCol(3);
m_vco_parameter_grid.SetCellForeColor(RGB(0,255,0));
m_vco_parameter_grid.SetText("Passed");
UpdateWindow();
CMEASUREMENT::strPASS_FAIL.ialign_pass_count++;
}
I have tried replacing UpdateWindow() with Refresh(),RedrawWindow(),Invalidate(), and calls directly to OnPaint();
The problem is randam. Some times it runs flowlessly.
Any idears would be appreciated.
Regards Desmond Mardle
|
|
|
|
|
I have a program that reads text input form users and echo it in a textbox, similiar to chat. When I compile it using debug configuration everything works fine and great, but when I switched to release configuration the program crashes and generates an application error after the 1st line of text is echoed to screen. I have some TRACE statement in the code but as far as I know the TRACE statement does nothing in release configuration.
Is there anything else that I should check for might be the cause of this? I know the release configuration does some optimization but I'm not sure why it works in debug configuration but not release?
Any suggestion is welcome!
|
|
|
|