0

In a few places in the code I have a struct definition that looks like this:

typedef struct tagNameOfStruct{
  //some methods and members
}T_NameOfStruct, * PT_NameOfStruct, FAR LPT_NameOfStruct;

typedef struct tagNameOfStructLists{
  //some methods and members
}T_NameOfStructLists, * PT_NameOfStructLists, FAR LPT_NameOfStructLists;
 typedef pair<T_NameOfStruct, T_NameOfStructLists> CStructPair;
 typedef map<T_NameOfStruct, T_NameOfStructLists> CStructMap;

Than I see that inside a method within a loop the following line of code

T_NameOfStruct instance;
T_NameOfStructLists listInstance;
m_MapInstance.insert(CStructPair(instance, listInstance));
//structMapInstance is a members of method class of type CStructMap

That instance is being inserted to data-structure which is used outside the scope of the function.It is passed to the data-structure by reference.

  1. Shouldn't instance die when we leave the function scope?
  2. What does the last line of the struct definition mean?

Edit ---- Why this is not a duplicate, please reopen---- : The * PT_NameOfStruct, FAR LPT_NameOfStruct in the struct definition are different from the question you guys linked too. Also there is the issue of the passing the instance by ref while it is defines on the method stack. The strange thing is that the code works so I'm wondering what I'm missing here. Why don't I get an exception while trying to access destroyed objects in a different function which iterates over the data structure. Still think it's a duplicate?

qballer
  • 2,033
  • 2
  • 22
  • 40
  • 1
    please provide "insertion" code so that we may see a full picture – mangusta Mar 11 '14 at 09:32
  • 1
    It is a stock question, and has been answered a lot of times. For example, see http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions?rq=1 – ach Mar 11 '14 at 09:38
  • have a look at this blog (http://arraynotfound.blogspot.in/), i have written article about "tag name as struct", you can have a idea – VINOTH ENERGETIC Mar 11 '14 at 09:42
  • @Vinoth Your blog is irrelevant. This is C++, where tag names and typedefs are not in different namespaces. – user207421 Mar 11 '14 at 09:46
  • Wait you guys I know what typedef struct tagNameOfStruct{ //some methods and members }T_NameOfStruct; means. There are loads of stuff here which is going on which I don't know. Don't close this yet\ – qballer Mar 11 '14 at 09:48
  • @qballer So what is it that you *don't* know exactly? If your question hasn't been answered, maybe you need to clarify it? – user207421 Mar 11 '14 at 09:50
  • @EJP All I meant is not to close it as a duplicate. You and mike gave really good answers. I just need to understand something from Mike and I think his answer will be valid. – qballer Mar 11 '14 at 09:59
  • 1
    As to the `T_NameOfStruct, * PT_NameOfStruct, FAR* LPT_NameOfStruct` part. The syntax of a declaration consists of two parts, a sequence of specifiers, and a comma-separated list of declarators. At least one specifier must specify a type; specifiers may be compound and define new types. – ach Mar 11 '14 at 10:58
  • 1
    Your declaration has two specifiers, `typedef` and `struct…{…}`, and three declarators, `T_NameOfStruct`, `* PT_NameOfStruct`, `FAR* LPT_NameOfStruct`. Declarators introduce names and assign them types. Were there no `typedef`, `T_NameOfStruct`, `PT_NameOfStruct`, and `LPT_NameOfStruct` would be names of objects, and their types would be such that the type of expressions `T_NameOfStruct`, `* PT_NameOfStruct`, `* LPT_NameOfStruct` would be `struct…{…}`. However, with `typedef`, the declaration introduces type names instead of objects. – ach Mar 11 '14 at 11:02
  • `FAR` is a remnant of old days when 16-bit Intel processors used segmented memory address space and there was distinction between full 'far' pointers (containing both segment and offset; denoted by a `LP` prefix in Hungarian notation) and 'near' pointers (containing only offset, so that to use them a segment had to be assumed; denoted by a `NP` prefix in Hungarian notation). After a `FAR` or `NEAR` a `*` or `&` *must* follow, therefore I assumed that there must have been a typo in your original post. Nowadays, `FAR` and `NEAR` are empty macros kept for compatibility. – ach Mar 11 '14 at 11:13
  • And at last, as to instances passed by reference. The constructor of `std::pair` (assuming that it is `std::`) accepts const references, but what it actually does is making copies. `std::map::insert` also accepts a reference, but stores copies. – ach Mar 11 '14 at 11:30
  • How does a ref store copy? They get a ref to the object and make a full copy of it? What if it contains other references. – qballer Mar 11 '14 at 13:07
  • 1
    @qballer, by the means of copy-initialization or move-initialization which means calling a copy-constructor or move-constructor of the structures in question. – ach Mar 11 '14 at 13:59

3 Answers3

2

Shouldn't instance die when we leave the function scope?

Yes. If the code you haven't shown us actually uses a reference to it after that, then it's wrong. But since we can't see it, we can only guess whether or not it does that.

UPDATE: Now you've shown us what's actually happening, it's being passed to map::insert, which stores a copy of its argument (even though it takes the argument by reference). There's no problem when instance itself is destroyed, assuming it has valid copy semantics.

What does the ending of the struct definition mean?

It declares:

  • a class called tagNameOfStruct.
  • an alias for the class type, called T_NameOfStruct. In C++ this is completely pointless; in C, some people have a bad habit of doing that to avoid typing struct when specifying the type.
  • an alias for a pointer to the type, called PT_NameOfStruct. This is an even worse idea than the first typedef, since it hides the important information that something is a pointer.
  • an alias for an obsolete "far" pointer to the type, called LPT_NameOfStruct. That's a hangover from 16-bit platforms; on a modern platform it will be the same as the regular pointer type.

Don't use this as a model for how to declare class types. It's better to keep it simple:

class NameOfStruct {  // or "struct", if you want members to be public by default
    // members
};

NameOfStruct instance;  // No need for a typedef
NameOfStruct * pointer; // Make it clear that it's a pointer
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • The pair constructor takes const _T1& _V1, const _T2& _V2 and the map insert takes a const value_type& _X. This might be stupid question but how is this not by ref? – qballer Mar 11 '14 at 09:57
  • @qballer: Sorry, you're right, they are passed by reference. But both the `pair` constructor, and `insert` itself, take copies of the arguments. I'll update the question to be pedantically accurate. – Mike Seymour Mar 11 '14 at 10:05
1

struct tagNameOfStruct{

In C++, struct tags have equivalent status to typedefs. Not so in C.

T_NameOfStruct

This is another and better name for tagNameOfStruct.

*PT_NameOfStruct

This is a typedef for a pointer to NameOfStruct.

FAR LPT_NameOfStruct;

This is an obsolete name for a FAR pointer to NameOfStruct, given that near and far pointers have meant the same thing since Windows 95 as far as I can see. Note that this is because of the preceding * in the preceding typename, which carries over to this one because of C's strange pointer syntax.

The only things you need to worry about here are NameOfStruct, which is a typedef for the struct itself, and PT_NameOfStruct, which is a pointer to it. The rest is fluff.

That instance is being inserted to data-structure which is used outside the scope of the function. It is passed to the data-structure by reference.

Shouldn't instance die when we leave the function scope?

Yes. You have found a bug.

What does the ending of the struct definition mean?

'The ending of the struct definition' means that this is the end of the struct definition. What else would it mean? Did you mean the ending of the declaration? Did you mean everything I've stated above?

user207421
  • 305,947
  • 44
  • 307
  • 483
0

In addition to the above answers,

struct s
{

}

In above struct definition you can use as follows

in c for declaring instance you should do like this

struct s instance1;

in c++ you should define as follows

struct s instance1; (or)
s instance1;

Both are valid in c++.

In c instead of using struct s at all area you can use typedef for simplification.

VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38