[英]storing return value from function into pointer to char variable is rightway to do?
我編寫了一個讀取功能,該功能從串行端口(LINUX)讀取值。 它返回值作為char的指針。 我在另一個函數中調用此函數,並將其再次存儲在變量中作為char的指針。 我偶爾遇到堆棧溢出問題,不確定該函數是否引起問題。 下面提供了示例。 請給我一些建議或批評。
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 .
}
}
謝謝和問候,SamPrat
您不取消分配緩沖區。 使用完后需要free
。
char * getData()
{
char *buf = (char *)malloc(255);
// Fill buffer
return buf;
}
void anotherFunc()
{
char *data = getData();
// Process data
free(data);
}
就您而言,我認為您應該在printf
之后釋放緩沖區:
if( ( sResult = ReadToSerialPort()) >= 0)
{
printf("Response is %s\n", sResult);
// code to store char in string and put into db .
free(sResult);
}
UPDATE靜態緩沖區
使用靜態緩沖區的另一種選擇。 它可以稍微提高性能,但是getData
方法不是線程安全的。
char buff[1024];
char *getData()
{
// Write data to buff
return buff;
}
int main()
{
char *data = getData();
printf("%s", data);
}
更新有關代碼的一些注意事項
int iMax = buffer+buffer_size-bufptr;
-iMax始終為1024; bufptr
任何想法,因為它的值與buffer
相同,並且您不會在函數中的任何位置進行更改。 iIn = read( fd, bufptr, buffer_size-1 );
bufptr[(int)iIn<iMax?iIn:iMax] = '\\0';
與bufptr[iIn] = '\\0'
; if(bufptr != buffer)
始終為false
,這就是為什么指針不正確並且始終返回0的原因; errno == EAGAIN
為true,請不要忘記釋放緩沖區。 當前,您只返回0而沒有free(buffer)
。 祝好運 ;)
Elalfer是部分正確的。 您可以使用free()緩沖區,但並非在每種情況下都可以。
例如,當您到達if ( errno == EAGAIN )
並且其結果為true時,您return
而free
對緩沖區進行free
操作。
最好的方法是將緩沖區作為參數傳遞,並明確表明用戶必須在函數外部釋放緩沖區。 (這基本上是Elalfer在他的編輯答案中所說的)。
剛意識到這是一個C問題,對此我要怪SO過濾:D對不起! 忽略以下內容,我將其保留,以便使注釋仍然有意義。
正確的解決方案應使用std::vector<char>
,這樣析構函數將在作用域末尾為您處理內存釋放。
第二個指針的目的是什么?
char *buffer = (char *)malloc(buffer_size);
char *bufptr = buffer;
這樣做的目的是什么?
int iMax = buffer+buffer_size-bufptr; // eh?
這樣做的目的是什么?
bufptr[(int)iIn<iMax?iIn:iMax] = '\0'; // so you pass in 1023 (iMax - 1), it reads 1023, you've effectively corrupted the last byte.
我將重新開始,考慮使用std::vector<char>
,類似於:
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;
然后在調用函數中,使用vector<char>
,如果需要一個字符串,則從中構造一個: std::string foo(vect.begin(), vect.end());
等等
設置空終止符“ bufptr [(int)iIn
bufptr [iMax] => bufptr [1024] =>您的分配超出了一個字節,因為數組從0開始。
在這種情況下也是int“ int iMax = buffer + buffer_size-bufptr;” 可以重寫為iMax = buffer_size。 它使代碼的可讀性降低。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.