Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

iterator question

Posted on 2003-10-22
10
Medium Priority
?
308 Views
Last Modified: 2013-12-14
I have a program that compiles and runs fine under MS VS6, but gets compiler errors with .NET.  I just wanted to know the difference between 2 statements.

I declare a vector
typedef vector<MyClass*>MyClassArray;
MyClassArray MyClassVar;
then an iterator
MyClassArray::iterator ppMyClass;
for (ppMyClass=MyClassVar.begin(); ppMyClass!=MyClassVar.end(); ppMyClass++) {
// .. do some things
      return ppMyClass; // this works in vs6, but error in .net
}

.Net accepts
        return &(*ppMyClass);

any insight would be appreciated
Thank You



0
Comment
Question by:marvinm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9600757
The problem is that you're returning an iterator to a local variable......

This is like returning a pointer to a local array (An iterator could be considered as a pointer to an element of the vector).

when you make

void function()
{
   MyClassArray MyClassVar;
   MyClassArray::iterator ppMyClass;
}

The myClssVar is a local variable, so it is stored in the stack...........
when the function exits, all the stack is released.....

so, if you return an iterator (or let's say a pointer), you're referencing memory that after the function exits, is not going to be available

This is why I believe that .net is giving an error. (I believe that MsVC6 didn't take this as an error).

Hope it's clear enough

Tincho

0
 
LVL 9

Expert Comment

by:tinchos
ID: 9600785
It depends in your code, but a way of solving this is passing the array as a parameter to the function, so it will be available even after the function exits....

A way of solving this is......

MyClassArray::iterator function( MyClassArray & MyClassVar )
{
  MyClassArray::iterator ppMyClass;

  // Your code

  return ppMyClass;
}

and you would call it like this


MyClassArray MyClassVar;
MyClassArray::iterator ppMyClass = function( MyClassVar );
0
 
LVL 1

Author Comment

by:marvinm
ID: 9600811
that makes sense, but leads to my next question...
what should I be returning?  What I am looking for is to get the address of the pointer to MyClassVar item in the vector, which is what I was treating the iterator value as.
Thank You
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:tinchos
ID: 9600884
I'm not sure if you read my last post...........

You can return an iterator, but there are two ways of solving it

one way is to pass a reference to the array as a parameter (like I said)

another way would be to allocate the array in the heap
MyClassArray *MyClassVar = new MyClassArray();

and in this case you won't have problem in returning iterators to the array.....
(as the array is stored in the heap, it won't get destroyed when the function exits)........



Finally, I just wanted to add that if I were you I would choose the solution in the previous post, as you won't have problem in freeing the memory allocated with new (which you will have if you use this second possibility)
0
 
LVL 1

Author Comment

by:marvinm
ID: 9600967
sorry, I posted my followup before refreshing the browser window.  I will look into
doing this with our complete app and either post followup questions or grade your response.
Thank You
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9601001
Ok, just let me know marvinm
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 200 total points
ID: 9601198
Returning &(*ppMyClass), returns a the address of the object referenced by the iterator (and would cause a problem if the iterator was MyClassVar.end()).

Returning ppMyClass, simply returns the iterator and would not cause a problem if the iterator was MyClassVar.end().

They are not the same.
0
 
LVL 4

Expert Comment

by:havman56
ID: 9606021
see ur STL of vs6 and .net they are different STL library i guess...
0
 
LVL 1

Author Comment

by:marvinm
ID: 9614695
tinchos, thank you for your help as well.
rstaveley's comment pointed me more in the correct direction though.
Thank You
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9614728
Ok marvinm, glad you could solve your problem
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question