简体   繁体   English

读取 argv 的字符指针? Memory 泄漏?

[英]Char pointer to read argv? Memory leak?

So my program is running and i also have no warnings from the compiler.所以我的程序正在运行,我也没有来自编译器的警告。

Right now, i simply use a char *name pointer to read in the argv[4] value.现在,我只是使用 char *name 指针来读取 argv[4] 值。

I use this value then as an argument in a function, like so:然后我将此值用作 function 中的参数,如下所示:

char *name = argv[4];
if (name != NULL)
   FunctionX(sl, rl, name, list1);

is there anything wrong with that approach, memory leak wise?这种方法有什么问题吗,memory 泄漏明智? If so, how should i change it?如果是这样,我应该如何改变它?

I ask, because my compiler shows me no warnings or errors and everything works fine.我问,因为我的编译器没有显示任何警告或错误,并且一切正常。 When i run: gcc -o programm -fsanitize=address *.c or gcc -o programm -fsanitize=address *.c -Wall it compiles just fine. When i run: gcc -o programm -fsanitize=address *.c or gcc -o programm -fsanitize=address *.c -Wall it compiles just fine. I had warnings before, but i fixed them.我之前有警告,但我修复了它们。 However, someone who reviewed my program said that they get a memory leak warning when using gcc -o programm -fsanitize=address *.c, altough i get no errors at all.但是,查看我的程序的人说,他们在使用 gcc -o programm -fsanitize=address *.c 时收到 memory 泄漏警告。 Any ideas what could cause that?有什么想法可能导致这种情况吗?

This snippet doesn't leak memory.此代码段不会泄漏 memory。 To do that, you'd need to call malloc , calloc or some other function that allocates memory, and then not call free .为此,您需要调用malloccalloc或其他分配 memory 的 function ,然后调用free Simply shuffling pointers around doesn't cause leaks.简单地改组指针不会导致泄漏。

The only potential problem in this snippet is accessing argv out of bounds, if you don't check argc before.如果您之前未检查argc ,则此代码段中唯一潜在的问题是越界访问argv

is there anything wrong with that approach, memory leak wise?这种方法有什么问题吗,memory 泄漏明智? If so, how should i change it?如果是这样,我应该如何改变它?

To have a memory leak, you first need to call malloc() .要发生 memory 泄漏,您首先需要调用malloc() And you haven't.而你没有。

I see no problem in the code you show.我认为您显示的代码没有问题。 argv[] is just an array of pointers to strings already allocated for you in the C runtime. argv[]只是一个指向在 C 运行时中已经为您分配的字符串的指针数组。 You need to consider it read only, as you can run in serious trouble if you modify any of the strings, or the array of pointers (it is normally implemented allocating the space for it in the stack, previous to call main() , so you had better not to touch it)您需要将其视为只读,因为如果您修改任何字符串或指针数组,您可能会遇到严重的麻烦(通常在调用main()之前在堆栈中为其分配空间,所以你最好不要碰它)

But for your question to be a good question, you need to show a complete example, the output you expect and the actual output you get, with a full, complete and ready to compile program (see How to create a Minimal, Reproducible Example ), and you have only post a snippet.但是要让您的问题成为一个好问题,您需要展示一个完整的示例,您期望的 output 和您获得的实际 output,以及完整、完整且可以编译的程序(请参阅如何创建最小的、可重现的示例) ,而您只发布了一个片段。 I can only say that I don't see anything wrong in your code.我只能说我没有在您的代码中看到任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM