Thread: Speed of <queue> and <vector>

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by scwizzo
    Another question, though. I get a warning upon compiling for these functions. Is there a better/more correct way to do this so there is no warning or so it follows better standards?
    The warning is pretty important: you are trying to return a pointer to a local variable, which means that the caller of the function would get a pointer that points to an object that no longer exists.

    Unfortunately, changing your implementation to have three separate variables makes a solution more difficult. I suggest that you re-consider your decision to provide these functions in the first place. Instead of trying to provide accessors to an array, perhaps you should provide accessors to the individual coordinate values.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The optimal solution is to us a plain old array of 3 values. In this case a vector would use the same number of bytes on the stack just to track an even larger amount of space used fom the heap (considering minimum heap allocation size of 16 bytes on Windows).

    You could improve your code efficiency by passing instances of Vec3 by const-reference to function like operator + for example.

    The way you're using new, one can pretty much guarantee that your code has, or will have, memory leaks.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class and <vector>
    By zenaphex in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2004, 09:12 AM
  2. Shifting back <vector> items...
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2003, 10:30 AM
  3. Implementation of <vector> class
    By supaben34 in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2002, 09:14 AM