vector and iterator problem

This is a discussion on vector and iterator problem within the C++ Programming forums, part of the General Programming Boards category; Hi i got following code: Code: void STM32SPI::handleInterrupt() { if (SPI_I2S_GetITStatus(spiport, SPI_I2S_IT_TXE) == SET) { if (sendBufferPos != sendBuffer.end()) { ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Question vector and iterator problem

    Hi i got following code:

    Code:
    void STM32SPI::handleInterrupt() {
    	if (SPI_I2S_GetITStatus(spiport, SPI_I2S_IT_TXE) == SET) {
    		if (sendBufferPos != sendBuffer.end()) {
    			SPI_I2S_SendData(spiport, *sendBufferPos);
    			sendBufferPos++;
    		} else {
    			enableInterrupt(false);
    			sendBuffer.clear();
    		}
    	}
    	if (SPI_I2S_GetITStatus(spiport, SPI_I2S_IT_RXNE) == SET) {
    		receiveBuffer.push_back((u8) SPI_I2S_ReceiveData(spiport));
    	}
    }
    
    void STM32SPI::send() {
    	sendBufferPos == sendBuffer.begin();
    	enableInterrupt();
    }
    with sendBuffer and sendBufferPos beeing class members:
    Code:
    class STM32SPI{
    	vector<u8>::iterator sendBufferPos;
    	vector<u8> sendBuffer;
    }
    i am trying to do this:
    after calling send the iterator is set to the begin of the vector and the interrupt is enabled, now everytime the interrupt handler is called, the iterator *should* move to
    the next position till end(). but it doesnt. sendBufferPos != sendBuffer.end() is never true.
    I am not touching the vector, so the iterator should not be invalidated as far as i know.

    P.S.

    the code is running on a microprocessor

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything wrong in your code. It may be either that the STL library is no working correctly, or the compiler decides that it doesn't need to change the variable outside of the function for some reason. Check the generated code, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Code:
    	sendBufferPos == sendBuffer.begin();
    Are you sure you wanted comparison instead of assignment there?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by adeyblue View Post
    Code:
    	sendBufferPos == sendBuffer.begin();
    Are you sure you wanted comparison instead of assignment there?
    Good spot!

    @BeBu: You may want to raise the warning level - most compilers will warn for this sort of "code that doesn't do anything", and almost every time, the compiler is right in doing so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Yes thanks to both of you. That was it. Of course i didnt want a comparison but an assignement.

    I turned all warning on and it got reported like statement value is not used.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BeBu View Post
    I turned all warning on and it got reported like statement value is not used.
    Yes, the exact wording is somewhat varying from compiler to compiler, but they usually have something in them to say "Do you really mean to do this meaningless stuff here, or did you mean something else?" Compiler warnings are USUALLY added for a reason, so it's a good idea to listen to them (and fix them) - there is nothing worse than compiling a piece of code that give 100 warnings, and then spend two days realizing that the code change you made added a 101st warning, that if you had seen the tree in the forest of warnings, you would have fixed it in 30 seconds.

    A not so unusual one is "empty statement in if":
    Code:
    if (filePtr);
       printf("File opened OK");
    Now, you'll scratch your head a bit before you figure out that the time you passed in a file that COULDN'T be opened, it still did the printf!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    Quote Originally Posted by matsp View Post
    Now, you'll scratch your head a bit before you figure out that the time you passed in a file that COULDN'T be opened, it still did the printf!
    Mats
    I've done exactly that before.... it took over a day to find it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elkvis View Post
    I've done exactly that before.... it took over a day to find it.
    Yes. It took several people 6 months to find that sort of thing in some obscure part of our embedded OS! They can be quite hard to find. If the compiler warns about it, you should be able fix it in 30 seconds.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    A not so unusual one is "empty statement in if":
    Code:
    if (filePtr);
       printf("File opened OK");
    Now, you'll scratch your head a bit before you figure out that the time you passed in a file that COULDN'T be opened, it still did the printf!
    Here's where a good editor like Emacs (in fact, only Emacs) proves its value: abscence of auto-indentation and coloring will tell you straight away that there's something wrong...
    Microsoft Visual Studio never gave me that handy plus. Everybody, please use emacs :-)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,972
    Quote Originally Posted by MarkZWEERS
    Here's where a good editor like Emacs (in fact, only Emacs) proves its value: abscence of auto-indentation and coloring will tell you straight away that there's something wrong...
    Microsoft Visual Studio never gave me that handy plus.
    Auto-indentation is a common enough feature that is present in Visual Studio as well, but it is the syntax highlighting that is more useful here since the code might not have been written by you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by MarkZWEERS View Post
    Microsoft Visual Studio never gave me that handy plus. Everybody, please use emacs :-)
    Try

    Ctrl+a, Alt+F8
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21