-4

I've encountered an issue with my code, see the following code snippet:

bool flag = false;

if(flag==false)
{ 
int var=0;
flag=true;
}

if(flag==true)
{
var=10;
}

In this case var is marked as undefined and CCS generates an error, which is perfectly right. As long as a variable is defined inside an if case it's not know to the outside. For sure you can rewrite the code in this case. But in my actual code I've to built an object from a class with a non default constructor and it can't be solved otherwise than with an if case (at least I don't have an idea how)

My actual code:

SelectedSocket2=VCRT_selectset(&MasterSocket,1,-1); 

if((SelectedSocket != VCRT_SOCKET_ERROR) && (SelectedSocket != 0))
{
ClientSocket=accept(MasterSocket, NULL, NULL);
CStreamer    Streamer(ClientSocket);                  
CRtspSession RtspSession(ClientSocket,&Streamer);          
flag=true;
}
//Streamer, RtspSession are outside unknown and CCS generates an error

Any ideas how I can solve the issue or trick the compiler?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Zaiga
  • 43
  • 7

2 Answers2

2

You can use dynamic allocation to accomplish conditional construction:

std::unique_ptr<CStreamer> Streamer;
if (...) {
    Streamer = make_unique<CStreamer>(ClientSocket);
}

if (Streamer) Streamer->something();

Remember that since the object was conditionally constructed, it only exists what that branch was taken. So you need the additional conditional before use (or construct it also on the else branch).

Another approach is boost::optional. It needs no dynamic allocation. You can also roll your own with a buffer and placement new. Be careful about alignment.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I can't because anything from std to boost isn't available on the OS I am programming for. – Zaiga Feb 27 '15 at 15:27
  • @Croin Then use a "normal" pointer and remember to deallocate. – molbdnilo Feb 27 '15 at 15:31
  • @molbdnilo I would but the dynamic allocation doesn't work properly. This is actually how I've run into this issue in the first place everything was dynamically allocated but the OS can't handle it properly. (yeah I know what a crap). – Zaiga Feb 27 '15 at 15:36
  • @croin code composer is used across the whole TI product line from MSP430 with 4k RAM to quad core OMAP. How am I supposed to know you can't use dynamic allocation unless you say so in the question? – Ben Voigt Feb 27 '15 at 15:37
  • @BenVoigt Sorry my fault. – Zaiga Feb 27 '15 at 15:39
1

This snippet should work better :

bool flag = false;
int  var  = 0;

if (flag == false)
{ 
    var = 0;
    flag = true;
}

if (flag == true)
{
    var = 10;
}

In your code snippet, var is declared within the if scope (between its associated {}). It is destroyed when the first } is reached. If you want it to stay alive beyond the first if, you have to declare it outside.

EDIT :

With pointer without dynamic allocation (Using char[] to fake allocation) :

  // Allocate the needed size for CStreamer (Statically
  char _dummyCStreamer[sizeof(CStreamer)];
  // Same for CRtspSession
  char _dummyCRtspSession[sizeof(CRtspSession)];
  SelectedSocket2 = VCRT_selectset(&MasterSocket, 1, -1);

  // The following two lines are the trick
  CStreamer *streamerPtr = (CStreamer *)_dummyCStreamer;
  CRtspSession *RtspSessionPtr = (CRtspSession *)_dummyCRtspSession;

  // Go ahead, you can now consider your two pointer as if they were statically allowed
  if ((SelectedSocket != VCRT_SOCKET_ERROR) && (SelectedSocket != 0))
  {
    ClientSocket = accept(MasterSocket, NULL, NULL);
    CStreamer    Streamer(ClientSocket);
    CRtspSession RtspSession(ClientSocket, &Streamer);
    streamerPtr->operator=(Streamer);
    sessionPtr->operator=(RtspSession);
    flag = true;
  }

Be careful, streamerPtr and RtspSessionPtr have their lifetime bound to _dummyCStreamerand _dummyCRtspSession's ones. (Respectively) Of course, your classes have to implement a proper operator=.

Telokis
  • 3,399
  • 14
  • 36
  • Thank you for your comment, as I've written this was just an example of my problem. How are you going to write the same code but in place of the variable with an object built from a class with a none default constructor. If I am going to declare it first I need to pass the argument for the constructor which I don't have at the time of the declaration? – Zaiga Feb 27 '15 at 15:32
  • Then you have no choice but to use dynamic allocation and pointers. – Telokis Feb 27 '15 at 15:36
  • pointers on the stack would work but no dynamic allocation. Any idea how to solve this without an dynamic allocation? – Zaiga Feb 27 '15 at 15:41
  • Are there copy operators ? You could do something like `CStreamer *streamerPtr; if (...) { CStreamer streamer(ClientSocket); streamerPtr->operator=(streamer); }` – Telokis Feb 27 '15 at 15:45
  • 1
    Even if it works, it shouldn't : it is subject to crashes because the pointers are not allocated. The best possibility would be to write default constructors. It's what I advice you to do if you cannot use dynamic allocation. – Telokis Feb 27 '15 at 16:00
  • Ok, I finally found a proper answer. It should do the job without crashes ! – Telokis Feb 27 '15 at 17:40