Hi,

I'm embedded c coder and relatively new to c++. I'm working with borland 6.
What i'm trying to do is call my "SerialThread" named worker thread which processes the line.

Call

//char input_sentence[9]; this includes the textline "ANGLE 360"
char * buff = input_sentence;
 thread = _beginthread( SerialThread, 8192, (void *)&buff);

This thread version gives access violation

void SerialThread( void *input)
{

     char *  line = *(char**)input;
     Fmain->Memo1->Lines->Append(line);
     _endthread();
}

This version outputs character "T".

void SerialThread( void *input)
{

     char  line = *(char*)input;
     Fmain->Memo1->Lines->Append(line);
     _endthread();
}

Tried with string but got std::bad_alloc
If i just pass the input_sentence, i will get only first charachter to output.

Any help wil be appriciated.

Are you sure that your input string is null terminated?

Are you sure that your input string is null terminated?

Thank you for your fast reply.

Yes it is null terminated. I forgot to mention that. There is input_sentence[times]=0; line just before the call.

do not pass the address of a local variable (with automatic storage duration) to a thread function; it may not be around by the time the thread executes.

// ...
char* input_sentence = new char[32];
std::strcpy( input_sentence, "ANGLE 360" ) ;
thread = _beginthread( SerialThread, 8192, input_sentence ) ;
// ...

void SerialThread( void* input )
{

     char*  line = static_cast<char*>(input) ;
     Fmain->Memo1->Lines->Append(line);
     delete [] line ;
     _endthread();
}

Thank you! Your reply really saved my day. I had spent 4 hours dealing with that issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.