函数在Windows中起作用,但在Linux中没有函数
我已经学习C了大约5天,所以我尝试做一些小项目。 我创建了一个非常简单的库,该库可自动从用户获取字符串,然后在刚刚输出字符串值的程序中实现。
它可以完美地在Windows上运行(通过MSVC:'Cl。\ Main.c /link。 >'gcc -o test main.c m_string.o')
我知道max是1024绝对没有意义,这只是一个测试值。
//main.c
#include <stdio.h>
#include "get_string/m_string.h"
int main(int argc, char *argv[])
{
char *inputf = get_string("Something: ");
printf("%s", inputf);
return 0;
}
// m_string.h
#include <stdio.h>
#define MAX 1024
char *get_string(char *prompt);
//m_string.c
#include "m_string.h"
char *get_string(char *prompt)
{
char *s[MAX];
printf("%s", prompt);
fgets(s, MAX, stdin);
return s;
}
运行main.c.c Windows上的工作原理:
Something: something
something
在Linux上运行main.c:
Something: something
(null)$
由于我使用的值很大,用于 fgets()中使用的最大内存,所以我不认为缓冲区溢出是原因(否则可能会分割故障)。
感谢您的阅读,我希望有人可以为像我这样的绝对初学者提供一些启示。
I've been learning C for like 5 days, and so I tried doing some little projects.
I created a very simple library that automates getting a string from the user,then implemented it in a program which just outputs the string's value.
It runs perfectly on Windows (Compiled through MSVC: 'cl .\main.c /link .\get_string\m_string.obj') but it doesn't in Linux (Compiled with gcc using : 'gcc -o test main.c m_string.o')
I know that MAX being 1024 makes absolutely no sense, it's just a test value.
//main.c
#include <stdio.h>
#include "get_string/m_string.h"
int main(int argc, char *argv[])
{
char *inputf = get_string("Something: ");
printf("%s", inputf);
return 0;
}
// m_string.h
#include <stdio.h>
#define MAX 1024
char *get_string(char *prompt);
//m_string.c
#include "m_string.h"
char *get_string(char *prompt)
{
char *s[MAX];
printf("%s", prompt);
fgets(s, MAX, stdin);
return s;
}
Running main.c on Windows works as expected:
Something: something
something
Running main.c on Linux:
Something: something
(null)$
Since I am using a very big value for the maximum memory used in fgets(), I don't think that a buffer overflow is the cause (Would have probably gotten Segmentation Fault otherwise).
Thank you for reading and I hope someone can shed some light on the matter for an absolute beginer like me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
char *s [max];
创建max
指针的数组,而不是char
值。始终启用编译器的警告。它会找到这个。使用GCC,我使用
-wall -wextra -pedantic
。get_string
返回一个指向变量的指针,该变量不再存在get_string
返回。解决此问题的一种方法是使用
malloc
和免费
该缓冲区中的缓冲区。demo 在编译器资源管理器上
char *s[MAX];
creates an array ofMAX
pointers, notchar
values.ALWAYS enable your compiler's warnings. It would have found this. With gcc, I use
-Wall -Wextra -pedantic
.get_string
returns a pointer to a variable that no longer exists onceget_string
returns.One way to solve this is to allocate a buffer using
malloc
, andfree
that buffer in the caller.Demo on Compiler Explorer
它确实确实可以“正确”运行,因为这是不确定的行为。您可能无法访问定义其定义范围之外的临时存储。
除此之外,您还有一个类型错误:
char *s [max];
定义了max> max
char pointers 的数组。将其用作字符串缓冲区不应超过编译器。如果您不想为此动态分配内存,请进行字符串缓冲区静态。如果您计划在并发环境中使用此功能,则您甚至可以将其进行线程本地(自C11)。
It really does not run "correctly", because this is undefined behavior. You may not access temporary storage outside of the scope in which it was defined.
In addition to this, you also have a type error:
char *s[MAX];
defines an array ofMAX
char pointers. Using this as a string buffer should not have been allowed past the compiler.If you do not want to allocate memory dynamically for this, then make the string buffer static. You can even make it thread-local (since C11) if for some reason you plan to use this function in concurrent environments.