我的算法的奇怪行为
Weird behaviour of my algorithm
我不明白为什么当我在无限循环中打印它时打印出正确的结果,但当我在没有循环的情况下打印时不打印任何东西。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
char msg[1000];
time_t get_mtime(const char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) == -1) {
perror(path);
exit(1);
}
return statbuf.st_mtime;
}
void encode(char* msg, char * argv[], int argc) {
const int bmp_header_size = 54;
int read_code;
int msg_idx = 0;
int img_idx = 0;
int bit_idx = 0;
char c;
FILE *img_in = NULL;
FILE *img_out = NULL;
if( argc < 3 ){
printf( "Usage: %s source_bmp output_bmp message.\n", argv[0] );
exit(1);
}
img_in = fopen( argv[3], "rb" );
if( img_in == NULL ){
printf( "Could not open the input image file.\n" );
exit(1);
}
img_out = fopen( argv[2], "wb" );
if( img_out == NULL ){
printf( "Could not open the output file.\n" );
exit(1);
}
while( ( read_code = fgetc( img_in )) != EOF ){
c = (char)read_code;
if( img_idx >= bmp_header_size && msg_idx <= strlen( msg ) ){
char bit_mask = 1 << bit_idx;
if( ( msg[msg_idx] & bit_mask) > 0 )
c |= 1;
else
c &= 254;
bit_idx++;
if( bit_idx >= 8 ){
bit_idx = 0;
msg_idx++;
}
}
fputc( c, img_out );
img_idx++;
}
fclose(img_in);
fclose(img_out);
}
char* decode(char * argv[], int argc) {
/* printf("1 = %s\n", argv[1]);
printf("2 = %s\n", argv[2]);
printf("3 = %s\n", argv[3]); */
const int bmp_header_size = 54;
const int max_msg_size = 1000;
int i;
int c;
int img_idx = 0;
int msg_idx = 0;
int bit_idx = 0;
FILE *img_in = NULL;
// char msg[max_msg_size];
img_in = fopen( argv[2], "rb" );
if( img_in == NULL ){
printf( "Could not open the input image file.\n" );
exit(1);
}
for( i=0; i < max_msg_size; i++ )
msg[i] = 0;
while( ( c = fgetc( img_in )) != EOF ){
if( img_idx >= bmp_header_size ){
char bit_mask = 0;
if( ( c & 1 ) > 0 )
bit_mask |= 1 << bit_idx;
msg[msg_idx] |= bit_mask;
bit_idx++;
if( bit_idx >= 8 ){
if( msg[msg_idx] == '[=10=]' )
break;
bit_idx = 0;
msg_idx++;
}
}
img_idx++;
}
fclose(img_in);
return msg;
}
int main(int argc, char * argv[]) {
char message[1000];
while (1) {
printf("\n Send : ");
fgets(message, 1000, stdin);
encode(message, argv, argc);
long mod = get_mtime(argv[2]);
long new = mod;
while (1) {
new = get_mtime(argv[2]);
if (mod != new) {
break;
}
}
while (1) {
printf("Received : %s ",decode(argv, argc));
}
}
return 0;
}
让我们关注(在 main() 中接近尾声)
while (1) {
strcpy(received, decodeReverse(argv, argc));
printf("Received : %s ",received);
}
使用上面的代码我得到了正确的输出:
Received : Hey
Received : Hey
Received : Hey
Received : Hey
Received : Hey
但是如果在没有无限循环的情况下打印 received
我得到:
Received :
好像received
是空的。
编辑:
我清理了代码并应用了修复程序,但在没有无限循环的情况下它仍然无法正常工作(即使进行了几次迭代也没有)。
谢谢大家!
返回的缓冲区正在被覆盖。
char msg[max_msg_size];
只存在于函数中,但正在返回。
编译器为循环调整内存,这意味着它恰好存在,但可能不在不同的编译器/平台上
char msg[max_msg_size];
是函数内的局部变量decode
,你却试图return它。
它与无限循环一起工作的事实与编译器恰好解释循环的方式有关,但是,这是未定义的行为。
要修复,请在 decode
函数之外声明 msg
。
请记住,您当然也需要对
lines char* output = msg
delcare 在你设置 msg 到它之前先在函数之外,然后 return 它。
我不明白为什么当我在无限循环中打印它时打印出正确的结果,但当我在没有循环的情况下打印时不打印任何东西。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
char msg[1000];
time_t get_mtime(const char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) == -1) {
perror(path);
exit(1);
}
return statbuf.st_mtime;
}
void encode(char* msg, char * argv[], int argc) {
const int bmp_header_size = 54;
int read_code;
int msg_idx = 0;
int img_idx = 0;
int bit_idx = 0;
char c;
FILE *img_in = NULL;
FILE *img_out = NULL;
if( argc < 3 ){
printf( "Usage: %s source_bmp output_bmp message.\n", argv[0] );
exit(1);
}
img_in = fopen( argv[3], "rb" );
if( img_in == NULL ){
printf( "Could not open the input image file.\n" );
exit(1);
}
img_out = fopen( argv[2], "wb" );
if( img_out == NULL ){
printf( "Could not open the output file.\n" );
exit(1);
}
while( ( read_code = fgetc( img_in )) != EOF ){
c = (char)read_code;
if( img_idx >= bmp_header_size && msg_idx <= strlen( msg ) ){
char bit_mask = 1 << bit_idx;
if( ( msg[msg_idx] & bit_mask) > 0 )
c |= 1;
else
c &= 254;
bit_idx++;
if( bit_idx >= 8 ){
bit_idx = 0;
msg_idx++;
}
}
fputc( c, img_out );
img_idx++;
}
fclose(img_in);
fclose(img_out);
}
char* decode(char * argv[], int argc) {
/* printf("1 = %s\n", argv[1]);
printf("2 = %s\n", argv[2]);
printf("3 = %s\n", argv[3]); */
const int bmp_header_size = 54;
const int max_msg_size = 1000;
int i;
int c;
int img_idx = 0;
int msg_idx = 0;
int bit_idx = 0;
FILE *img_in = NULL;
// char msg[max_msg_size];
img_in = fopen( argv[2], "rb" );
if( img_in == NULL ){
printf( "Could not open the input image file.\n" );
exit(1);
}
for( i=0; i < max_msg_size; i++ )
msg[i] = 0;
while( ( c = fgetc( img_in )) != EOF ){
if( img_idx >= bmp_header_size ){
char bit_mask = 0;
if( ( c & 1 ) > 0 )
bit_mask |= 1 << bit_idx;
msg[msg_idx] |= bit_mask;
bit_idx++;
if( bit_idx >= 8 ){
if( msg[msg_idx] == '[=10=]' )
break;
bit_idx = 0;
msg_idx++;
}
}
img_idx++;
}
fclose(img_in);
return msg;
}
int main(int argc, char * argv[]) {
char message[1000];
while (1) {
printf("\n Send : ");
fgets(message, 1000, stdin);
encode(message, argv, argc);
long mod = get_mtime(argv[2]);
long new = mod;
while (1) {
new = get_mtime(argv[2]);
if (mod != new) {
break;
}
}
while (1) {
printf("Received : %s ",decode(argv, argc));
}
}
return 0;
}
让我们关注(在 main() 中接近尾声)
while (1) {
strcpy(received, decodeReverse(argv, argc));
printf("Received : %s ",received);
}
使用上面的代码我得到了正确的输出:
Received : Hey
Received : Hey
Received : Hey
Received : Hey
Received : Hey
但是如果在没有无限循环的情况下打印 received
我得到:
Received :
好像received
是空的。
编辑:
我清理了代码并应用了修复程序,但在没有无限循环的情况下它仍然无法正常工作(即使进行了几次迭代也没有)。
谢谢大家!
返回的缓冲区正在被覆盖。
char msg[max_msg_size];
只存在于函数中,但正在返回。
编译器为循环调整内存,这意味着它恰好存在,但可能不在不同的编译器/平台上
char msg[max_msg_size];
是函数内的局部变量decode
,你却试图return它。
它与无限循环一起工作的事实与编译器恰好解释循环的方式有关,但是,这是未定义的行为。
要修复,请在 decode
函数之外声明 msg
。
请记住,您当然也需要对
lines char* output = msg
delcare 在你设置 msg 到它之前先在函数之外,然后 return 它。