|
No sir, I don't use any threading there. Just only that part have in my code.
Seems I can't use the buffer in the other function, which I'm not define the buffer.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: Then later I want to get that string from a buffer. So I do the following.
what does exactly mean later on (is there a chance that buff var was overwritten in the meantime?)?
(i.e. follow Cedric Moonen suggestion and post intermediate code too).
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.
[my articles]
|
|
|
|
|
CPallini wrote: what does exactly mean later on (is there a chance that buff var was overwritten in the meantime?)?
No, at a time buffer not overwrite. Actually it is because of my incomplete question. As I said my last post, I've add name into the buffer in one member function and get the name from another member function.
Define the buffer globally and used.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Since you've also declared buff inside the assigning method, then it is lost when method terminates (it goes out of scope). You probably have a global buff that is never initialised (hence the error), and a local one, that is fully initilised and then lost for ever.
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.
[my articles]
|
|
|
|
|
Ok, actually I've try to just print the buffer elements within the buffer adding function. It works. Then I try to display elements within the second function. It wont display. Debugger point that buffer is empty. Actually I don't delete the buffer until it use in the second function.
That mean, there is an issue with the scope of the buffer.
I've declared the buffer in .h file as follows.
public:
char* buff;
and I suppose in this way, I can use it globally. Is that wrong. If so, how can I define the buffer globally.
I appreciate your help all the time...
Eranga
|
|
|
|
|
If you declared buff that way (i.e. a class member), then you can indeed use it inside all class methods (and also outside the class methods, if you have a reference to a class instance).
But if you declare a local instance of buff inside such methods, then the local instance will hide the class member one.
If you really need a global variable, though, then you have to declare it global, i.e. scoped in the global namespace, outside any class declaration.
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.
[my articles]
|
|
|
|
|
Ok, I'll try it and let you know what happened on my code.
I appreciate your help all the time...
Eranga
|
|
|
|
|
I have done it as you said.
Declared a buffer outside of the class definition, i mean just after the include statements of the *.cpp file. Then I done the work using it. After doing all the process, delete the buffer inside the destructor.
Everything fine.
Now my question is that why it is not work, define in the class and use it? You have any comments for me?
I appreciate your help all the time...
Eranga
|
|
|
|
|
In fact, You can also use for the purpose buff as class member, provided you access it in class methods (or, if you are outside the class context, you have an instance of the class and buff is declared public ).
You code failed because inside a method (or a function) you declared again buff and the latter, local declaration shadowed the class member one so that you initialised local instance of buff but then accessed the (uninitialised) class member one.
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.
[my articles]
|
|
|
|
|
Ok, thanks a lot pal. Now I got the point. At the same time. One of friend says that, as a good practice in programming, as much as possible it is better to avoid global variables and used member variables. Is that true?
Why I'm asking is, one is that I'm really new for C++, two is I most familiar with Java and there is no such a way, so that those tips can be really helpful to me
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: Ok, thanks a lot pal. Now I got the point. At the same time. One of friend says that, as a good practice in programming, as much as possible it is better to avoid global variables and used member variables. Is that true?
Well, I'm not dogmatic about, but yes, I suggest you to use sparingly global variables.
Eranga Thennakoon wrote: Why I'm asking is, one is that I'm really new for C++, two is I most familiar with Java and there is no such a way, so that those tips can be really helpful to me
Well if you're familiar with Java then you should be familiar with OOP principles too, such encapsulation and information hiding that both go against global variables usage.
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.
[my articles]
|
|
|
|
|
CPallini wrote: Well if you're familiar with Java then you should be familiar with OOP principles too, such encapsulation and information hiding that both go against global variables usage.
Yep, thats true. In any language SECURITY must be the most important word
I appreciate your help all the time...
Eranga
|
|
|
|
|
Hello everyone,
Bjarne mentioned in his book, that there is no way to catch exceptions from the initialization of global variables (section 14.7). But I do not think it is true, because we can add function try block to its constructor, and catch appropriate types of exceptions or using catch(...) to catch all exceptions.
Am I correct? Any comments?
thanks in advance,
George
|
|
|
|
|
Why don't you try by yourself?!
Maxwell Chen
|
|
|
|
|
Sure, Maxwell. I have posted below.
regards,
George
|
|
|
|
|
Okay, from observing the hundreds of your everyday questions, I guess that you have been having difficulty to understand what every authors (MSDN and C++ books) want to express. Maybe you should pick up another beginner level book first.
Here is the example for your original question.
class A
{
public:
A() {
cout << "A(), and going to throw! \n";
throw 3;
}
};
A obj;
void main()
try {
}
catch(...) {
}
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
Here is my code to verify exception from constructor of global variable could be caught and handled. Anything wrong?
using namespace std;
class Foo{
public:
Foo ();
};
Foo::Foo()
try{
cout << "constructing" << endl;
throw(0);
} catch (...)
{
cout << "catch exception in constructor" << endl;
}
Foo foo;
int main()
{
int i = 0;
cout << i;
return 0;
}
regards,
George
|
|
|
|
|
Your way is local catch (in the scope of the constructor), not what Stroustrup wanted to say: Global catch.
Maxwell Chen
|
|
|
|
|
Try this, and it won't compile.
try {
A obj;
}
catch(...) {
}
void main()
{
}
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
Can you post all the code please? So that we can see where is the compile error.
regards,
George
|
|
|
|
|
By following my first reply in this thread.
The class A sample.
Maxwell Chen
|
|
|
|
|
Sure Maxwell,
You can not put try/catch just around the global variable, but put try/catch into a function, constructor (my sample) or main.
regards,
George
|
|
|
|
|
Thanks Maxwell,
I have read again the chapter and Bjarne only mentioned, "there is no way to catch exceptions from the initialization of global variables (section 14.7)".
No words like local or global catch. Do you have the book at hand?
regards,
George
|
|
|
|
|
George_George wrote: No words like local or global catch.
Please look it up in Cambridge Dictionary for the definitions of "local" and "global"!
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
I have taken time to type down all the original words here,
--------------------
This will catch every exception, except those thrown by construction and destruction of global variables. There is no way of catching exceptions thrown during initialization of global variables. The only way of gaining control in case of throw from an initializer of a nonlocal static object is set_unexpected ($14.6.2). This is another reason to avoid global variable whenever possible.
--------------------
You can see there is no terms like local or global.
regards,
George
|
|
|
|