_getch returns extra null terminators - c++

I got this function from a tutorial
void read( char* buf, int maxSize ) {
const char* const pEnd = buf + maxSize;
for ( char c = _getch(); c != 13 && (buf + 1 < pEnd); c = _getch(), buf++ ) {
_putch( c );
*buf = c;
}
*buf = 0;
}
It filled buf with null terminators after each character was entered.
I had to modify it like this to make it work:
void read( char* buf, int maxSize ) {
const char* const pEnd = buf + maxSize;
char c = 0;
while ( c != 13 && (buf < pEnd) ) {
c = _getch();
if ( c != 0 ) {
_putch( c );
*buf = c;
buf++;
}
}
*buf = 0;
}
What is wrong with _getch()? Why does it return null terminators constantly? Even in the working function if I step through it, I can see _getch() returning '\0' 3 or 4 times after each character is typed.
EDIT --
I'm using Visual Studio 2017. While this looks like C code, it's because the tutorial series starts by teaching cstrings before moving on to std::string.

It turns out there's a bug in Visual Studio 2017 with _getch() which causes null terminators to be inserted after every character. If you switch from debug to release mode the bug goes away.
See here: https://developercommunity.visualstudio.com/content/problem/252047/something-wrong-with-getch-in-loops.html

Related

Strange behavior with char pointer and char pointer returned by fonction in C/C++ with "cout"

I have a strange behavior with a char pointer initialized by the value of a return function and with the cout.
All my code is for an Arduino application, this is why I use char pointer, char array and string.h.
I created a class named FrameManager, with a function getDataFromFrame to extract data from a string (in fact a char array). See above:
`char * FrameManager::getDataFromFrame ( const char frame[], char key[] )
{
char *pValue = nullptr;
int frameLength = strlen ( frame );
int previousStartIndex = 0;
for ( int i=0; i<frameLength; i++ ) {
char c = frame[i];
if ( c == ',' ) {
int buffSize = i-previousStartIndex+1;
char subbuff[buffSize];
memset ( subbuff, 0, buffSize ); //clear buffer
memcpy ( subbuff, &frame[previousStartIndex], i-previousStartIndex );
subbuff[buffSize]='\0';
previousStartIndex = i+1;
int buffLength = strlen ( subbuff );
const char *ptr = strchr ( subbuff, ':' );
if ( ptr ) {
int index = ptr-subbuff;
char buffKey[index+1];
memset ( buffKey, 0, index+1 );
memcpy ( buffKey, &subbuff[0], index );
buffKey[index+1]='\0';
char buffValue[buffLength-index];
memset ( buffValue, 0, buffLength-index );
memcpy ( buffValue, &subbuff[index+1], buffLength-index );
buffValue[buffLength-index]='\0';
if ( strcmp ( key,buffKey ) == 0 ) {
pValue = &buffValue[0];
break;
}
}
} else if ( i+1 == frameLength ) {
int buffSize = i-previousStartIndex+1;
char subbuff[buffSize];
memcpy ( subbuff, &frame[previousStartIndex], frameLength-1 );
subbuff[buffSize]='\0';
int buffLength = strlen ( subbuff );
const char *ptr = strchr ( subbuff, ':' );
if ( ptr ) {
int index = ptr-subbuff;
char buffKey[index+1];
memset ( buffKey, 0, index+1 );
memcpy ( buffKey, &subbuff[0], index );
buffKey[index+1]='\0';
char buffValue[buffLength-index];
memset ( buffValue, 0, buffLength-index );
memcpy ( buffValue, &subbuff[index+1], buffLength-index );
buffValue[buffLength-index]='\0';
if ( strcmp ( key,buffKey ) == 0 ) {
pValue = &buffValue[0];
break;
}
}
}
}
return pValue;
}`
In the main(), I created juste a little code to test the returned value:
int main(int argc, char **argv) {
const char frame[] = "DEVICE:ARM,FUNC:MOVE_F,PARAM:12,SERVO_S:1";
FrameManager frameManager;
char key[] = "DEVICE";
char *value;
value = frameManager.getDataFromFrame(frame, &key[0]);
cout << "Retrieved value: " << value << endl;
cout << "Retrieved value: " << frameManager.getDataFromFrame(frame, &key[0]) << endl;
printf("%s",value);
return 0;
}
and here the result:
Retrieved value: y%R
Retrieved value: ARM
ARM
The first "cout" doesn't display the expected value.
The second "cout" display the expected value and the printf too.
I don't understand what is the problem with the first "cout".
Thanks
Jocelyn
pValue points into local arrays, which get out of scope. That's undefined behavior. It might work, but your program might also crash, return wrong values (that's what you experience), corrupt your data or do any other arbitrary action.
Given that you're already using C++, consider using std::string as a result instead or point into the original frame (if possible).

How to implement a stream that can be splitted by newline

The following code works, but is about twice as inefficient compared to when I use a (linux) pipe that gives unzipped data to the (modified) program. I need a steady stream within the program which I can keep splitting by \n. Is there a way to do this using a (string?) stream or any other trick?
int main(int argc, char *argv[]) {
static const int unzipBufferSize = 8192;
long long int counter = 0;
int i = 0, p = 0, n = 0;
int offset = 0;
char *end = NULL;
char *begin = NULL;
unsigned char unzipBuffer[unzipBufferSize];
unsigned int unzippedBytes;
char * inFileName = argv[1];
char buffer[200];
buffer[0] = '\0';
bool breaker = false;
char pch[4][200];
Read *aRead = new Read;
gzFile inFileZ;
inFileZ = gzopen(inFileName, "rb");
while (true) {
unzippedBytes = gzread(inFileZ, unzipBuffer, unzipBufferSize);
if (unzippedBytes > 0) {
unzipBuffer[unzippedBytes] = '\0'; //put a 0-char after the total buffer
begin = (char*) &unzipBuffer[0]; // point to the address of the first char
do {
end = strchr(begin,(int)'\n'); //find the end of line
if (end != NULL) *(end) = '\0'; // put 0-char to use it as a c-string
pch[p][0] = '\0'; \\ put a 0-char to be able to strcat
if (strlen(buffer) > 0) { // if buffer from previous iteration contains something
strcat(pch[p], buffer); // cat it to the p-th pch
buffer[0] = '\0'; \\ set buffer to null-string or ""
}
strcat(pch[p], begin); // put begin (or rest of line in case there was a buffer into p-th pch
if (end != NULL) { // see if it already points to something
begin = end+1; // if so, advance begin to old end+1
p++;
}
if(p>3) { // a 'read' contains 4 lines, so if p>3
strcat(aRead->bases,pch[1]); // we use line 2 and 4 as
strcat(aRead->scores,pch[3]); // bases and scores
//do things with the reads
aRead->bases[0] = '\0'; //put them back to 0-char
aRead->scores[0] = '\0';
p = 0; // start counting next 4 lines
}
}
while (end != NULL );
strcat(buffer,pch[p]); //move the left-over of unzipBuffer to buffer
}
else {
break; // when no unzippedBytes, exit the loop
}
}
Your main problem is probably the standard C string library.
With using strxxx() funcions, you are iterating through the complete buffer multiple times each call, first for strchr(), then for strlen(), then for each of the strcat() calls.
Using the standard library is a nice thing, but here, it's just plain inefficient.
Try if you could come up with something simpler that touches each character only once like (code just to show the principle, do not expect it working):
do
{
do
{
*tp++ = *sp++;
} while (sp < buffer_end && *sp != '\n');
/* new line, do whatever it requires */
...
/* reset tp to beginning of buffer */
} while (sp < buffer_end);
I am trying to get this to work, but all it does is giving a Segmentation Fault at runtime:
do {
unzippedBytes = gzread(inFileZ, unzipBuffer, unzipBufferSize);
if (unzippedBytes > 0) {
while (*unzipBuffer < unzippedBytes) {
*pch = *unzipBuffer++;
cout << pch;
i++;
}
i=0;
}
else break;
} while (true);
What am I doing wrong here?

Determine if an number (encoded as a string) will fit in a 64-bit integer in C++?

I'm looking for a portable way to a) convert a string to a 64-bit signed integer (int64_t), and b) determine if it won't fit (overflows). Is there any way to do this?
strtoll is pretty portable anymore. And if not in your case, you could always crib the GNU C runtime library and add that to your project...
errno = 0;
long long val = strtoll (string, NULL, 0);
if (errno == ERANGE)
// there was an overflow conversion error
Run through the characters of the string one at a time and make your integer. if the character you're parsing will cause an overflow, then you know you're about to overflow. this code is the basic idea- doesn't handle errors or negative numbers, but should give you the idea...
bool ConvertToInt( const char* inString, int64_t& outInt )
{
int64_t kInt64Max = 0x7fffffffffffffff;
const char* c = inString;
outInt = 0;
while( *c != '\0' )
{
int charValue = *c - '0';
//outInt will be assigned outInt * 10 + charValue, so to check if that will overflow
//use algebra and move stuff around so that you can do the math without overflowing
if( outInt > ( kInt64Max - charValue ) / 10 )
{
//overflow
return false;
}
outInt = outInt * 10 + charValue;
++c;
}
return true;
}
if you want full credit on your homework, make sure to handle negative numbers and non-numeric characters. [ Edited to increment c ptr- thanks for the tip :) )
So a 'long long'? An signed int64_ can hold from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, and you can just see that from the string. For example, with std::string:
int stringLength;
string myString("123456789");
stringLength = myString.length();
That code gets the length of your string. To determine whether it overflows just check the number of digits, and if there might be an overflow, check the first digit. To convert to int64_, use casting:
http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/
That link should answer your question. (However it's for C-style strings.) And one last clarification, is your string a std::string or not?
To cater for Visual C++ 10.0 (as I write this 11.0 is in beta), which apparently does not have strtoll or any equivalent,
#include <assert.h> // assert
#include <errno.h> // errno
#include <stdint.h> // int64_t
#include <string> // std::string
#include <stdexcept> // std::runtime_error, std::range_error
#include <stdlib.h> // EXIT_FAILURE, EXIT_SUCCESS, strtoll
#include <iostream>
using namespace std;
#if defined( _MSC_VER )
# if _MSC_VER <= 1600
# include <ole2.h>
inline long long strtoll( char const *str, char **str_end, int base )
{
assert(( "Only base 10 for Visual C++ 10 and earlier", base == 10 ));
std::wstring const ws( str, str + strlen( str ) );
LONG64 result;
HRESULT const hr = VarI8FromStr(
ws.c_str(), 0, LOCALE_NOUSEROVERRIDE, &result
);
switch( hr )
{
case S_OK:
if( str_end != 0 )
{
*str_end = const_cast<char*>( str + strlen( str ) );
}
return result;
case DISP_E_OVERFLOW:
errno = ERANGE;
if( str_end != 0 )
{
*str_end = const_cast<char*>( str );
}
return (*str == '-'? LLONG_MIN : LLONG_MAX);
default:
errno = EILSEQ;
if( str_end != 0 )
{
*str_end = const_cast<char*>( str );
}
return 0;
}
}
# endif
#endif
template< class Type >
bool hopefully( Type const& v ) { return !!v; }
bool throwX( string const& s ) { throw runtime_error( s ); }
bool throwRangeX( string const& s ) { throw range_error( s ); }
int64_t int64From( string const& s )
{
errno = 0;
int64_t const result = strtoll( s.c_str(), nullptr, 10 );
if( errno == ERANGE )
throwRangeX( "int64From: specificed nr too large" );
else if( errno != 0 )
throwX( "int64From: parsing failed" );
return result;
}
int main( int argc, char** argv )
{
try
{
int64_t const x = int64From( argv[argc - 1] );
wcout << x << endl;
return EXIT_SUCCESS;
}
catch( runtime_error const& x )
{
cerr << "!" << x.what() << endl;
}
return EXIT_FAILURE;
}
Then for Visual C++ 10 and earlier, link with [oleaut32.lib].
I tested this with MinGW g++ and Visual C++.
PS: Alternatively you can just an istringstream, but it does not reliably tell you why it failed when it fails – and it seems to be a requirement to detect overflow as such.
Based on a helpful response from Joshua Glazer, I came up with the following solution which does error checking and also works for negative integers:
#define __STDC_LIMIT_MACROS
#include <stdint.h>
// convert a string to an integer, return whether successful
bool string_to_int(string in, int64_t &out) {
size_t pos = 0;
if (in.size() == 0)
return false;
if (in[pos] == '+')
pos++;
out = 0;
if (in[pos] == '-') {
pos++;
while (pos < in.size()) {
if (in[pos] < '0' || in[pos] > '9')
return false;
int c = in[pos]-'0';
if (out < (INT64_MIN+c)/10)
return false;
out = out*10-c;
pos++;
}
} else {
while (pos < in.size()) {
if (in[pos] < '0' || in[pos] > '9')
return false;
int c = in[pos]-'0';
if (out > (INT64_MAX-c)/10)
return false;
out = out*10+c;
pos++;
}
}
return true;
}

storing return value from function into pointer to char variable is rightway to do?

I have written a read function which reads values from serial port(LINUX) . It returns values as pointer to char . I am calling this function in another function and storing it again in a variable as pointer to char . I occasionally got stack over flow problem and not sure if this function is creating problem.
The sample is provided below. Please give some suggestions or criticism .
char *ReadToSerialPort( )
{
const int buffer_size = 1024;
char *buffer = (char *)malloc(buffer_size);
char *bufptr = buffer;
size_t iIn;
int iMax = buffer+buffer_size-bufptr;
if ( fd < 1 )
{
printf( "port is not open\n" );
// return -1;
}
iIn = read( fd, bufptr, iMax-1 );
if ( iIn < 0 )
{
if ( errno == EAGAIN )
{
printf( "The errror in READ" );
return 0; // assume that command generated no response
}
else
printf( "read error %d %s\n", errno, strerror(errno) );
}
else
{
// *bufptr = '\0';
bufptr[(int)iIn<iMax?iIn:iMax] = '\0';
if(bufptr != buffer)
return bufptr;
}
free(buffer);
return 0;
} // end ReadAdrPort
int ParseFunction(void)
{
// some other code
char *sResult;
if( ( sResult = ReadToSerialPort()) >= 0)
{
printf("Response is %s\n", sResult);
// code to store char in string and put into db .
}
}
Thanks and regards,
SamPrat
You do not deallocate the buffer. You need to make free after you finished working with it.
char * getData()
{
char *buf = (char *)malloc(255);
// Fill buffer
return buf;
}
void anotherFunc()
{
char *data = getData();
// Process data
free(data);
}
In your case I think you should free the buffer after printf:
if( ( sResult = ReadToSerialPort()) >= 0)
{
printf("Response is %s\n", sResult);
// code to store char in string and put into db .
free(sResult);
}
UPDATE Static buffer
Another option to use static buffers. It could increase performance a little bit, but getData method will be not a thread-safe.
char buff[1024];
char *getData()
{
// Write data to buff
return buff;
}
int main()
{
char *data = getData();
printf("%s", data);
}
UPDATE Some notes about your code
int iMax = buffer+buffer_size-bufptr; - iMax will always be 1024;
I do not see any idea of using bufptr since its value is the same as buffer and you do not change it anywhere in your function;
iIn = read( fd, bufptr, buffer_size-1 );
You can replace bufptr[(int)iIn<iMax?iIn:iMax] = '\0'; with bufptr[iIn] = '\0';
if(bufptr != buffer) is always false and this is why your pointer is incorrect and you always return 0;
Do not forget to free the buffer if errno == EAGAIN is true. Currently you just return 0 without free(buffer).
Good luck ;)
Elalfer is partially correct. You do free() your buffer, but not in every case.
For example, when you reach if ( errno == EAGAIN ) and it evaluates to true, you return without doing free on your buffer.
The best would be to pass the buffer as a parameter and make it obvious that the user must free the buffer, outside the function. (this is what basically Elalfer sais in his edited answer).
Just realized this is a C question, I blame SO filtering for this :D sorry! Disregard the following, I'm leaving it so that comments still make sense.
The correct solution should use std::vector<char>, that way the destructor handles memory deallocation for you at the end of scope.
what is the purpose of the second pointer?
char *buffer = (char *)malloc(buffer_size);
char *bufptr = buffer;
what is the purpose of this?
int iMax = buffer+buffer_size-bufptr; // eh?
What is the purpose of this?
bufptr[(int)iIn<iMax?iIn:iMax] = '\0'; // so you pass in 1023 (iMax - 1), it reads 1023, you've effectively corrupted the last byte.
I would start over, consider using std::vector<char>, something like:
std::vector<char> buffer(1500); // default constructs 1500 chars
int iRead = read(fd, &buffer[0], 1500);
// resize the buffer if valid
if (iRead > 0)
buffer.resize(iRead); // this logically trims the buffer so that the iterators begin/end are correct.
return buffer;
Then in your calling function, use the vector<char> and if you need a string, construct one from this: std::string foo(vect.begin(), vect.end()); etc.
When you are setting the null terminator "bufptr[(int)iIn
bufptr[iMax]=>bufptr[1024]=>one byte beyond your allocation since arrays start at 0.
Also int this case "int iMax = buffer+buffer_size-bufptr;" can be re-written as iMax = buffer_size. It makes the code less readable.

How to pass pointer and pointer to a function?

I implement a function that acts like getline( .. ). So my initial approach is:
#include <cstdio>
#include <cstdlib>
#include <cstring>
void getstr( char*& str, unsigned len ) {
char c;
size_t i = 0;
while( true ) {
c = getchar(); // get a character from keyboard
if( '\n' == c || EOF == c ) { // if encountering 'enter' or 'eof'
*( str + i ) = '\0'; // put the null terminate
break; // end while
}
*( str + i ) = c;
if( i == len - 1 ) { // buffer full
len = len + len; // double the len
str = ( char* )realloc( str, len ); // reallocate memory
}
++i;
}
}
int main() {
const unsigned DEFAULT_SIZE = 4;
char* str = ( char* )malloc( DEFAULT_SIZE * sizeof( char ) );
getstr( str, DEFAULT_SIZE );
printf( str );
free( str );
return 0;
}
Then, I think I should switch to pure C instead of using half C/C++. So I change char*& to char**:
Pointer to Pointer version ( crahsed )
#include <cstdio>
#include <cstdlib>
#include <cstring>
void getstr( char** str, unsigned len ) {
char c;
size_t i = 0;
while( true ) {
c = getchar(); // get a character from keyboard
if( '\n' == c || EOF == c ) { // if encountering 'enter' or 'eof'
*( *str + i ) = '\0'; // put the null terminate
break; // done input end while
}
*( *str + i ) = c;
if( i == len - 1 ) { // buffer full
len = len + len; // double the len
*str = ( char* )realloc( str, len ); // reallocate memory
}
++i;
}
}
int main() {
const unsigned DEFAULT_SIZE = 4;
char* str = ( char* )malloc( DEFAULT_SIZE * sizeof( char ) );
getstr( &str, DEFAULT_SIZE );
printf( str );
free( str );
return 0;
}
But this version crashed, ( access violation ). I tried run the debugger, but I could not find where it crashed. I'm running Visual Studio 2010 so could you guys show me how to fix it?
Another weird thing I've encountered is that, if I leave the "&" out, it only works with Visual Studio, but not g++. That is
void getstr( char* str, unsigned len )
From my understanding, whenever we use pointer to allocate or deallocate a block of memory, we actually modify where that pointer are pointing to. So I think we have to use either ** or *& to modify the pointer. However, because it run correctly in Visual Studio, is it just luck or it should be ok either way?
Then, I think I should switch to pure C instead of using half C/C++.
I suggest the other direction. Go full-blown C++.
Your pointer crash is probably in the realloc
*str = ( char* )realloc( str, len )
Should be
*str = ( char* )realloc( *str, len )
As Steve points out, your code leaks the original if realloc fails, so maybe change it to something like:
char* tmp = (char*) realloc(*str, len)
if (tmp) {
*str = tmp
} else {
// realloc failed.. sigh
}
Well, running it in a debugger highlights this line
*str = ( char* )realloc( str, len ); // reallocate memory
where there is a mismatch between str - the pointer to the variable - and *str - the pointer to the memory.
I'd be tempted to rewrite it so it returns the string, or zero on error, rather than having a void return and an in/out parameter ( like fgets does, which seems to be the function you're sort-of copying the behaviour of ). Or wrap such a function. That style doesn't let you get confused as you're only ever dealing with a pointer to char, rather than a pointer to pointer to char.
char* getstr_impl ( char* str, unsigned len ) {...}
void getstr( char** str, unsigned len ) {
*str = getstr_impl ( *str, len );
}