NULL value?

  • Thread starter Vicent Giner-Bosch
  • Start date
V

Vicent Giner-Bosch

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!
 
C

cppquester

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!

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
 
V

Vicent Giner-Bosch

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??
 
P

Paul

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
 
J

Juha Nieminen

Paul said:
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?
 
A

Alain Ketterlin

Vicent Giner-Bosch said:
// 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.
 
P

Paul

Juha Nieminen said:
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.
 
J

Jorgen Grahn

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::eek:ptional.

/Jorgen
 
J

James Kanze

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top