c中的局部变量和内存 [英] Local variables and memory in c

查看:40
本文介绍了c中的局部变量和内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C 有点陌生,并且想知道有关内存分配的某些事情.我的功能如下:

size_t count_nwords(const char* str) {//char* Copy_str = strdup(str);//因为 'strtok()' 改变了它通过的字符串char copy_str[strlen(str)];strcpy(copied_str, str);size_t 计数 = 1;strtok(c​​opied_str, " ");而(strtok(NULL,")!= 0){计数++;}//免费(copyed_str);返回计数;}

该函数统计一个字符串中的单词数量(分隔符是一个空格,即").我不希望修改传入参数的字符串.

我有两个问题:

  1. strdup() 方式(代码中的注释部分)是否应该优于 strcpy() 方式?我的理解是 strcpy() 足够快,但我不确定.
  2. 既然没有为要返回的 size_t 值分配内存(它是一个局部变量),是否应该这样做以确保函数的健壮性?或者使用 size_t nwords = count_nwords(copied_input); 是完全安全的并且总是能正确获取返回值?

谢谢!

我已经接受了与我的问题完全相关的唯一答案,但我建议您阅读其他答案,因为它们对我在代码中所犯的错误提供了很好的见解.

解决方案

  1. strdup() 方式(代码中的注释部分)是否应该优于 strcpy() 方式?我的理解是 strcpy()足够快,但我不确定.

您的解决方案很干净,效果很好,所以不要打扰.唯一的一点是您正在使用 VLA,它现在是可选的,然后使用 strdup 会不太标准.现在关于性能,由于没有指定 VLA 是如何实现的,性能可能因编译器/平台到编译器/平台而异(已知 gcc 对 VLA 使用堆栈,但任何其他编译器都可能使用堆).我们只知道 strdup 在堆上分配,仅此而已.我怀疑这样的选择会导致性能问题.

注意:你的分配大小是错误的,至少应该是strlen(str)+1.

<块引用>

  1. 由于没有为要返回的 size_t 值分配内存(它是一个局部变量),是否应该这样做以确保功能强大吗?或者正在使用 size_t nwords =count_nwords(copied_input);完全安全,将始终正确获取返回值?

管理返回值和适合的内存是编译器关心的问题.通常,这些值在堆栈上传输/从堆栈传输(在堆栈帧"上有一些读数).正如您可能怀疑的那样,在调用之前在堆栈上为其分配空间,并在调用之后释放空间(只要您丢弃或复制返回值).

I'm somewhat new to C and am wondering about certain things about memory allocation. My function is as follows:

size_t count_nwords(const char* str) {
    //char* copied_str = strdup(str);  // because 'strtok()' alters the string it passes through
    char copied_str[strlen(str)];
    strcpy(copied_str, str);
    size_t count = 1;

    strtok(copied_str, " ");
    while(strtok(NULL, " ") != 0) {
        count++;
    }

    //free(copied_str);

    return count;
}

This function counts the amount of words in a string (the delimiter is a space, ie ""). I do not want the string passed in argument to be modified.

I have two questions:

  1. Should the strdup() way (which is the commented part in the code) be preferred over the strcpy() one? My understanding is that strcpy() is sufficient and faster, but I am not certain.
  2. Since no memory is allocated for the size_t value to be returned (it's a local variable), should it be done in order to ensure the function is robust? Or is using size_t nwords = count_nwords(copied_input); completely safe and will always properly get the returned value?

Thank you!

EDIT: I've accepted the only answer that concerned my questions precisely, but I advise reading the other answers as they provide good insights regarding errors I had made in my code.

解决方案

  1. Should the strdup() way (which is the commented part in the code) be preferred over the strcpy() one? My understanding is that strcpy() is sufficient and faster, but I am not certain.

Your solution is clean and works well so don't bother. The only point is that you are using VLA which is now optional, then using strdup would be less standard prone. Now regarding performance, as it is not specified how VLAs are implemented, performance may vary from compiler/platform to compiler/platform (gcc is known to use stack for VLAs but any other compiler may use heap). We only know that strdup allocates on the heap, that's all. I doubt that performance problem will come from such a choice.

Note: you allocation size is wrong and should be at least strlen(str)+1.

  1. Since no memory is allocated for the size_t value to be returned (it's a local variable), should it be done in order to ensure the function is robust? Or is using size_t nwords = count_nwords(copied_input); completely safe and will always properly get the returned value?

Managing return values and memory suitable for is a concern of the compiler. Usually, these values are transfered on/from the stack (have some reading on "stack frame"). As you may suspect, space is allocated on the stack for it just before the call and is deallocated after the call (as soon as you discard or copy the returned value).

这篇关于c中的局部变量和内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆