Click here to Skip to main content
15,891,184 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I just used a goto! Pin
Fabio Franco20-Jun-19 23:21
professionalFabio Franco20-Jun-19 23:21 
GeneralRe: I just used a goto! Pin
Super Lloyd21-Jun-19 4:05
Super Lloyd21-Jun-19 4:05 
GeneralRe: I just used a goto! Pin
Fabio Franco21-Jun-19 5:14
professionalFabio Franco21-Jun-19 5:14 
GeneralRe: I just used a goto! Pin
Super Lloyd21-Jun-19 5:22
Super Lloyd21-Jun-19 5:22 
GeneralRe: I just used a goto! Pin
kalberts24-Jun-19 2:09
kalberts24-Jun-19 2:09 
GeneralRe: I just used a goto! Pin
TNCaver21-Jun-19 4:03
TNCaver21-Jun-19 4:03 
GeneralRe: I just used a goto! Pin
sparklesalt21-Jun-19 4:33
sparklesalt21-Jun-19 4:33 
GeneralRe: I just used a goto! Pin
Gerardo Orozco21-Jun-19 4:34
Gerardo Orozco21-Jun-19 4:34 
IMHO goto's can really offer a very clean solution when coding in C and you want to implement a sort of manual "finally" or "rollback" logic. Here is an example of code that is rolling back some state if something goes wrong. I like it because the intent is clear (there is a single label we are jumping to), I'm avoiding repeating cleanup code and I'm not introducing additional indentation levels that would be necessary with a structured approach - if we want to avoid multiple exit points.

static STATUS Android_PrepareAudioBuffers(void) {
   STATUS errCode;
   SLresult result;
   _slBufferQueueItf = NULL;
   /* Obtain interface to the AudioBufferQueue object */
   result = (*_slRecorderInstance)->GetInterface(_slRecorderInstance, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
            &_slBufferQueueItf);

   if( result != SL_RESULT_SUCCESS ) {
      LOGE("%s - Error calling _slRecorderInstance->GetInterface(SL_IID_ANDROIDSIMPLEBUFFERQUEUE)", __func__);
      errCode = S_ERR_OPENSLES_GET_INTERFACE;
      goto fail_point;
   }

   /* Register buffer-ready call-back */
   result = (*_slBufferQueueItf)->RegisterCallback(_slBufferQueueItf, Android_RecordCallback, NULL);
   if( result != SL_RESULT_SUCCESS ) {
      LOGE("%s - Error calling _slBufferQueueItf->RegisterCallback()", __func__);
      errCode = S_ERR_OPENSLES_REGISTER_CALLBACK;
      goto fail_point;
   }

   /* Specify an event mask to ensure the callback fires only when an audio buffer is full */
   (*_slRecorderItf)->SetCallbackEventsMask(_slRecorderItf, SL_RECORDEVENT_BUFFER_FULL);

   _activeBuffIdx = 0;
   /* Initialize buffer queue by enqueing all buffers at once.
    When a buffer is filled, it is dequeued and the registered call-back is called
    The buffer should be re-enqueued when processed to implement circular-buffer-list
    functionality */

   int i;
   for( i = 0; i < RECORD_BUFFER_COUNT; i++ ) {
      result = (*_slBufferQueueItf)->Enqueue(_slBufferQueueItf, _buffers[i], _buffSz);
      if( result != SL_RESULT_SUCCESS ) {
         LOGE("%s - Error calling_slBufferQueueItf->Enqueue()", __func__);
         errCode = S_ERR_OPENSLES_ENQUEUE_BUFF;
         goto fail_point;
      }
   }
   return S_OK;
fail_point:
   // rollback state
   if( _slBufferQueueItf ) {
      (*_slBufferQueueItf)->RegisterCallback(_slBufferQueueItf, NULL, NULL);
      (*_slBufferQueueItf)->Clear(_slBufferQueueItf);
      _slBufferQueueItf = NULL;
   }
   return errCode;
}


modified 21-Jun-19 11:29am.

GeneralRe: I just used a goto! Pin
englebart21-Jun-19 4:52
professionalenglebart21-Jun-19 4:52 
GeneralRe: I just used a goto! Pin
Super Lloyd21-Jun-19 5:27
Super Lloyd21-Jun-19 5:27 
GeneralRe: I just used a goto! Pin
sasadler21-Jun-19 6:28
sasadler21-Jun-19 6:28 
GeneralRe: I just used a goto! Pin
Michael Breeden21-Jun-19 8:29
Michael Breeden21-Jun-19 8:29 
GeneralRe: I just used a goto! Pin
gggustafson21-Jun-19 10:12
mvagggustafson21-Jun-19 10:12 
GeneralRe: I just used a goto! Pin
Member 916705723-Jun-19 23:05
Member 916705723-Jun-19 23:05 
GeneralRe: I just used a goto! Pin
Matt McGuire24-Jun-19 7:18
professionalMatt McGuire24-Jun-19 7:18 
GeneralCCC 20/6 Pin
PeejayAdams19-Jun-19 22:12
PeejayAdams19-Jun-19 22:12 
GeneralRe: CCC 20/6 Pin
pkfox19-Jun-19 23:30
professionalpkfox19-Jun-19 23:30 
GeneralRe: CCC 20/6 - SOLVED Pin
PeejayAdams19-Jun-19 23:35
PeejayAdams19-Jun-19 23:35 
GeneralRe: CCC 20/6 - SOLVED Pin
pkfox19-Jun-19 23:38
professionalpkfox19-Jun-19 23:38 
GeneralBug Of The Year, aka Booty! :D PinPopular
Super Lloyd19-Jun-19 16:40
Super Lloyd19-Jun-19 16:40 
GeneralRe: Bug Of The Year, aka Booty! :D Pin
Brisingr Aerowing19-Jun-19 17:24
professionalBrisingr Aerowing19-Jun-19 17:24 
GeneralRe: Bug Of The Year, aka Booty! :D Pin
Johnny J.19-Jun-19 19:30
professionalJohnny J.19-Jun-19 19:30 
GeneralRe: Bug Of The Year, aka Booty! :D Pin
the goat in your machine19-Jun-19 19:41
the goat in your machine19-Jun-19 19:41 
GeneralRe: Bug Of The Year, aka Booty! :D Pin
dan!sh 19-Jun-19 23:24
professional dan!sh 19-Jun-19 23:24 
GeneralRe: Bug Of The Year, aka Booty! :D Pin
Super Lloyd20-Jun-19 0:09
Super Lloyd20-Jun-19 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.