Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > NULL value?

Reply
Thread Tools

NULL value?

 
 
Vicent Giner-Bosch
Guest
Posts: n/a
 
      02-07-2011
Hello.

I have a very basic question for you. I hope you can help me.

I have a variable with a customized data type (a struct, in fact).
There are some moments where the variable exists but does not still
contain anything.

Which is the best way to manage that? I mean, I want to detect when
that variable has not been updated to anything yet.

For me, the most natural option would be initializing that variable to
"NULL" or something similar, but I don't know if such a concept exists
in C++ or not.

As it is a struct, I can't use "zero" for that —or can I?

Which is the most usual approach in this case of matters, when
programming in C++??

Thank you very much in advance for your answers!

--
Vicent ^^
 
Reply With Quote
 
 
 
 
cppquester
Guest
Posts: n/a
 
      02-07-2011
On 7 Feb., 17:21, Vicent Giner-Bosch <(E-Mail Removed)> wrote:
> Hello.
>
> I have a very basic question for you. I hope you can help me.
>
> I have a variable with a customized data type (a struct, in fact).
> There are some moments where the variable exists but does not still
> contain anything.
>
> Which is the best way to manage that? I mean, I want to detect when
> that variable has not been updated to anything yet.
>
> For me, the most natural option would be initializing that variable to
> "NULL" or something similar, but I don't know if such a concept exists
> in C++ or not.
>
> As it is a struct, I can't use "zero" for that —or can I?
>
> Which is the most usual approach in this case of matters, when
> programming in C++??
>
> Thank you very much in advance for your answers!
>
> --
> Vicent ^^


Often it is a practical apporach to have one field of the struct set
to an invalid value and then check for this.
(e. g. myStruct.ID == -1 while valid values for ID start with 0)
The invalid value could be set in the default constructor.

Cheers,
Marc


 
Reply With Quote
 
 
 
 
Vicent Giner-Bosch
Guest
Posts: n/a
 
      02-07-2011

> Often it is a practical apporach to have one field of the struct set
> to an invalid value and then check for this.
> (e. g. myStruct.ID == -1 while valid values for ID start with 0)
> The invalid value could be set in the default constructor.
>
> Cheers,
> Marc


The problem is I am not using constructors or classes, just
structures.

In my case, I have something like this:


// My Struct is something defined in a header file :

MyStruct ms ;

// ... here there is some code not involving ms ...

do {

// The first time we enter here, the content in ms doesn't mean
anything

// Then, in this do-while loop we perform some operations on ms,
// so the next time we enter here, ms will have a proper value

} while (something) ; // end do




Maybe I can define something equivalent to "-1", but using the struct
(I mean, something like {-1, -1, -1}, or similar)

Any other approach??

--
Vicent
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-07-2011

>"Vicent Giner-Bosch" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>Hello.


>For me, the most natural option would be initializing that variable to
>"NULL" or something similar, but I don't know if such a concept exists
>in C++ or not.


>As it is a struct, I can't use "zero" for that —or can I?


Yes you can initialise a struct like so:
/*******************/
struct foo{
char x;
int y;
};
foo my_pod = {0};
/******************/

I think you need to access one of its data member to do the logic i.e:
/******************/
if(my_pod.x) //This should work
if(my_pod) //This won't work
/*****************/


Alternatively you could use a null initialised pointer to struct. Whatever
suits your needs.

HTH









 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-07-2011
Check out this webpage its all explained quite well at bottom of page.

http://www.devx.com/getHelpOn/Article/16039/1954
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      02-07-2011
Paul <(E-Mail Removed)> wrote:
> I think you need to access one of its data member to do the logic i.e:
> /******************/
> if(my_pod.x) //This should work
> if(my_pod) //This won't work
> /*****************/


It's rather hypocritical how you insult all the people in this
newsgroup while you yourself have no idea what you are talking about,
and have to resort to expressions like "I think you need to", and
"this should work".

What exactly is the situation where if(my_pos.x) "should work"? To
see if the object has been initialized properly? What if it has been
initialized properly to zero?
 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      02-07-2011
Vicent Giner-Bosch <(E-Mail Removed)> writes:

> // My Struct is something defined in a header file :
>
> MyStruct ms ;
>
> // ... here there is some code not involving ms ...
>
> do {
>
> // The first time we enter here, the content in ms doesn't mean
> // anything
>
> // Then, in this do-while loop we perform some operations on ms,
> // so the next time we enter here, ms will have a proper value
>
> } while (something) ; // end do


Refactor your code as:

// Place here the code of the first iteration
// ms doesn't mean anything useful, so it's not yet declared

MyStruct ms = ...; // construct and initialize your object

while (something) {

// Here is the regular body of the loop
// using and probably updating ms

}

Yes, some code is duplicated. But that code would anyway be peppered
with tests on the "availability" of ms. If this is really a problem,
think again about your algorithm.

BTW, there is no essential difference between a struct and a class. You
can have a constructor in a class.

-- Alain.
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-07-2011

"Juha Nieminen" <(E-Mail Removed)> wrote in message
news:4d502a8a$0$2845$(E-Mail Removed)...
> Paul <(E-Mail Removed)> wrote:
>> I think you need to access one of its data member to do the logic i.e:
>> /******************/
>> if(my_pod.x) //This should work
>> if(my_pod) //This won't work
>> /*****************/

>
> It's rather hypocritical how you insult all the people in this
> newsgroup while you yourself have no idea what you are talking about,
> and have to resort to expressions like "I think you need to", and
> "this should work".
>
> What exactly is the situation where if(my_pos.x) "should work"? To
> see if the object has been initialized properly? What if it has been
> initialized properly to zero?
>

Your intent is unclear.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-07-2011
On Mon, 2011-02-07, Vicent Giner-Bosch wrote:
>
>> Often it is a practical apporach to have one field of the struct set
>> to an invalid value and then check for this.
>> (e. g. myStruct.ID == -1 while valid values for ID start with 0)
>> The invalid value could be set in the default constructor.

>
> The problem is I am not using constructors or classes, just
> structures.


Then start using them. You have no excuse not to.

> Maybe I can define something equivalent to "-1", but using the struct
> (I mean, something like {-1, -1, -1}, or similar)


What I use varies a lot with the problem, but I often end up with a
member function 'bool Foo::valid() const' or 'bool Foo::empty()
const'. Sometimes there's a corresponding flag beneath, but usually
you don't need to add one -- there is already some obviously invalid
state for the type, for example that -1 of yours.

Also see boost:ptional.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-08-2011
On Feb 7, 4:21 pm, Vicent Giner-Bosch <(E-Mail Removed)> wrote:

> I have a very basic question for you. I hope you can help me.


> I have a variable with a customized data type (a struct, in fact).
> There are some moments where the variable exists but does not still
> contain anything.


> Which is the best way to manage that? I mean, I want to detect when
> that variable has not been updated to anything yet.


> For me, the most natural option would be initializing that variable to
> "NULL" or something similar, but I don't know if such a concept exists
> in C++ or not.


> As it is a struct, I can't use "zero" for that —or can I?


> Which is the most usual approach in this case of matters, when
> programming in C++??


The fallible idiom is more or less standard for this, but only
if you have no "normal" invalid value; you don't see Fallible
used with a pointer or reference type, for example. (In the
case of a reference, of course, the fallible type for a
reference is a pointer.)

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
Is there a null ostream (like /dev/null) in cpp? Bo Peng C++ 13 07-18-2004 07:17 PM



Advertisments