|
George_George wrote: 1.
So, your answer to my question 1 is _Buf is used to hold small sized content, and _Ptr is used to hold larger sizes content. Small sized content is on stack and larger sized content is on heap, correct?
Yes (and, as I stated that is a clever and desiderable behaviour).
George_George wrote: 2.
What is your answer to my original question item 2? I think because when the content is small, only _Buf is used and _Ptr is empty, so _Ptr is bad ptr? Correct understanding?
It is a bit more subtle: since _Buf & _Ptr share the same (at least sizeof(_Ptr) ) memory locations, because both of them belongs to a union , hence _Prt is a bad pointer when
(1) The context requires _Buf usage (i.e. short string).
(2) the initial characters of the string (for instance {_Buf[0], _Buf[1], _Buf[2], _Buf[3]} for ANSI strings), interpreted as a pointer, give an invalid address.
For instance the following code
union Foo
{
char a[4];
char *p;
};
void main()
{
Foo f;
f.a[0]='m';
f.a[1]='s';
f.a[2]='d';
f.a[3]='n';
}
(as side effect) assigns 0x6e64736d , i.e. an invalid address to f.p .
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]
|
|
|
|
|
Cool, CPallini!
1.
In item 2, you mean when the content is big, the first 4 bytes of _Buf is actually the address of the heap (i.e. the value of _Ptr) where the content resides?
2.
In my original code, I return local variable b to main function variable s1, is it dangerous code?
3.
Another of my confusion is, when return b is called, we return the reference of variable b or create a new temp instance and making s1 in main references it?
regards,
George
|
|
|
|
|
George_George wrote: 1.
In item 2, you mean when the content is big, the first 4 bytes of _Buf is actually the address of the heap (i.e. the value of _Ptr) where the content resides?
Yes (union s my friend, union s).
George_George wrote: 2.
In my original code, I return local variable b to main function variable s1, is it dangerous code?
Nope, because:
George_George wrote: when return b is called it creates a new temp instance and making s1 in main references it .
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]
|
|
|
|
|
Thanks CPallini,
1.
So, in my original code, there are 3 instances,
- instance b and copied b to a new temp instance as the return value;
- instance s1 which is copy contructed from the temp instance.
Correct understanding?
2.
If I change the code in the following way, return the reference to local variable b, is it safe code?
#include <string>
using namespace std;
string& foo()
{
string b;
b.append ("msdn");
b.append (".microsoft");
b.append (".com");
return b;
}
int main()
{
string& s1 = foo();
return 0;
}
regards,
George
|
|
|
|
|
George_George wrote: 1.
So, in my original code, there are 3 instances,
- instance b and copied b to a new temp instance as the return value;
- instance s1 which is copy contructed from the temp instance.
Correct understanding?
Yes.
George_George wrote: 2.
If I change the code in the following way, return the reference to local variable b, is it safe code?
It's a mistake.
Change slightly the main function to see some output:
int main()
{
string& s1 = foo();
cout << s1 << endl;
return 0;
}
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]
|
|
|
|
|
Thanks CPallini,
1.
For your comments, I think using reference is better since it will save instance creation and useless copy?
2.
I have tried to use cout, and the output is correct "msdn.microsoft.com", what is wrong?
regards,
George
|
|
|
|
|
George_George wrote: 1.
For your comments, I think using reference is better since it will save instance creation and useless copy?
As I have pointed out, using a reference that way is a mistake. You're getting a reference to a local object going out of scope (i.e. being destroyed) on function return.
George_George wrote: 2.
I have tried to use cout, and the output is correct "msdn.microsoft.com", what is wrong?
I see garbage on my system.
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]
|
|
|
|
|
Thanks CPallini,
Is it the best solution -- I mean saving the time to create new instance and safe code?
(In my sample below, there should be only one instance of string called b, correct?)
#include <string>
#include <iostream>
using namespace std;
void foo(string& b)
{
b.append ("msdn");
b.append (".microsoft");
b.append (".com");
return;
}
int main()
{
string b;
foo (b);
cout << b << endl;
return 0;
}
regards,
George
|
|
|
|
|
George_George wrote: Is it the best solution -- I mean saving the time to create new instance and safe code?
It's a good solution: correct code without create/destroy overhead.
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]
|
|
|
|
|
Thanks for code review, question answered.
regards,
George
|
|
|
|
|
See this MSDN forums thread[^]. In short, the Dinkumware C++ library (used by Microsoft) applies the "small string optimization" technique.
A more in depth article about std::string implementations can be found here[^]
|
|
|
|
|
|
I wanna hit some rows in the DataGrid,and the rows hit would be output to the excel.But I have not find the driver function that when I hit a row,what should I do?
|
|
|
|
|
How can I output the data to excel with VC?
|
|
|
|
|
Hi all,
I am doing an update system which gets updates of a software from a site. Now the update is in the form of a zip file. What I am using is:
ShellExecute( this->m_hWnd, "open", InstallerPath.c_str(), NULL, NULL, SW_SHOWNORMAL);
This is what I am using when it was direct to exe now the format has been changed to zip. Any advice on how I will do this. I want to be able to install the installer inside the zip file which is in .exe
Thanks,
Jayjay
|
|
|
|
|
Write code to
1. unzip the zip and
2. run the installer
3. Cleanup
Greetings from Germany
|
|
|
|
|
is there a function to unzip the zip?
|
|
|
|
|
Search CodeProject for Unzip.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
[Message Deleted]
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: You could use Self-Extracting Zip Files ( For example see WinZip Self-Extractor[^])
So, you did not read the thread, did you?
Original Post: ... the format has been changed to zip ... I want to be able to install the installer inside the zip file which is in .exe
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
[Message Deleted]
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: I did. I just mentioned that an alternate way would be to make it a self extracting installer. Winzip Self-Extractor was just an example.
Let me say it again: The OP is already receiving a zip file from somewhere, and his problem is to run the executable which is within that zip file. How can Winzip self-extractor help?
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Winzip self-extractor was just an example. If you see the link I posted earlier, it gives usage details. He could have used his existing mechanism to launch the self-extractor instead of EXE
Rajesh R Subramanian wrote: s already receiving a zip file from somewhere
I believed the updates are controlled by him.
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: I believe the updates are controlled by him.
He is providing an update mechanism (which just executes a file using ShellExecute). The updates are coming to him from *a site*. Please read his original message. I won't be talking on this any further, as it serves no good to anybody.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
I said "I believed" (past tense). I was not defending my answer but simply explaining what I believed and thought while answering.
I have deleted my previous posts. I agree, it serves no good to anybody and I'll stop here.
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|