在C中遇到字符串问题 [英] Having a problem with strings in C

查看:85
本文介绍了在C中遇到字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我已经被这个非常简单的例子打倒了,这个例子起初似乎已经给我带来了很多令人头疼的问题。有人能告诉我这些功能有什么问题吗?



注意:我正在使用C.这是一项要求。



Hi guys, I''ve been knocked down with this pretty simple example that seems at first but it''s been giving me a lot of headaches lately. Can someone tell me what''s wrong with these function?

Note: I am working with C. It is a requirement.

#include "stdafx.h"
#include <string.h>
#include <stdio.h>

char* telegram_input()
{
	char message[100];

    printf("Write down the telegram: ");
	gets(message);

    return message;
}


int _tmain(int argc, _TCHAR* argv[])
}

        printf("Write your message:\n\n");
	char * myMessage; 

	myMessage = telegram_input();

        //HERE's the problem!!!!! -->
        printf("The written message is: %s.", myMessage);


	return 0;
}





问题是,当我将数组的值返回给char *指针时,这只能保存数组的第一个值是不正确的。

当我用''printf'打印它时,它会显示一个笑脸字符。怎么样?

为什么会这样?如果我没有使用上述功能,我就不会遇到这个问题。



The problem is that when I return the the value of the array to the char* pointer, this only holds the first value of the array and it is an incorrect one.
When I print it with ''printf'' it shows a smile face character. How is that?
Why does this happen? If I hadn''t use the above functions I wouldn''t have this problem.

推荐答案

问题是当你在一个函数中分配内存时,它会去在堆栈上,并被下一个要调用的函数删除并重用。

因此,您在堆栈上分配一个包含100个字符的数组,填充它,并返回指向它的指针。然后退出该函数,该函数释放堆栈中的所有项目,然后返回到您的调用函数。指针是完全有效的(从某种意义上说,它在内存中,并且不会导致访问冲突),但它指向不再分配的内存。当你调用printf来显示字符串内容时,同一个指针会转到函数,然后可以自由地使用堆栈来获取它自己的变量。由于它们共享相同的内存,它会在打印过程中覆盖字符串数据的内容!





图像它''一堆硬币:一堆有五个硬币,你输入telegram_input功能。它需要空间,因此它会在堆中添加100个硬币,并记住第一个硬币的位置。然后它会在每个新的100个硬币上写一封信,然后返回到调用函数。作为回归的一部分,一堆硬币在调用函数之前返回到它的状态--5个硬币 - 并且调用printf函数,它将一些硬币放在堆上。因为你传递的指针会记住堆中硬币的位置 - 它们从第六枚硬币开始 - 你最终没有读到正确的信息,因为你的硬币已经很久了!



希望这是有道理的 - 用图片更容易,并且能够看到你的眼睛睁着眼睛......:笑:



这被称为悬挂参考,真的必须避免。避免它的最好方法是将缓冲区作为参数传递给电报输入函数,如果要使用它(并且你应该),它的长度也是如此,以便内存始终正确且可用。

The problem is that when you allocate memory in a function, it goes on the stack, and is removed and reused by the next function to be called.
So, you allocate an array of 100 characters on the stack, fill it, and return a pointer to it. Then you exit the function, which releases all the items on the stack, and goes back to your calling function. The pointer is perfectly valid (in the sense that it''s in memory, and won''t cause a access violation) but it is pointing to memory that is no longer assigned. When you call printf to show the string content, the same pointer goes over to the function, which is then free to use the stack for it''s own variables. Since these share the same memory, it overwrites the content of your string data in the process of printing it!


Image it''s a pile of coins: there are five coins in a pile, and you enter the telegram_input function. It needs space, so it adds 100 coins to the pile, and remembers where the first one is. it then writes a letter on each of the new 100 coins, and returns to the calling function. As part of the return, the pile of coins is returned to it''s state before the function was called - 5 coins - and the printf function is called, which puts some coins of it''s own on the pile. because the pointer you are passing about remembers the location of the coins in the pile - "they start at the sixth coin" - you end up not reading the right info, because your coins are long gone!

Hopefully this makes some sense - it''s a lot easier with pictures and being able to see when your eyes glaze over...:laugh:

This is called a "hanging reference" and really has to be avoided. The best way to avoid it is to pass the buffer into the telegram-input function as a parameter, along with it''s length if you are going to use it (and you should) so that the memory is always correct and available.
char* telegram_input(char* message)
{
    printf("Write down the telegram: ");
    gets(message);
    return message;
}


int _tmain(int argc, _TCHAR* argv[])
}

        printf("Write your message:\n\n");
    char message[100];

    telegram_input(message);
    printf("The written message is: %s.", message);
    return 0;
}

另一个解决方案是使用 malloc 函数在telegram_input函数中分配内存 - 但是你必须确保完成后释放内存。 malloc 分配不在堆栈上的内存(它改为堆),因此没有这个问题。

The other solution is to use the malloc function to allocate the memory within the telegram_input function - but then you must be sure to free the memory when you have finished with it. malloc allocates memory that is not on the stack (it is on a "heap" instead) so it does not have this problem.


telegram_input 中的 char 数组消息超出范围当函数返回时。使用 malloc 动态分配内存并返回 char * 。完成后,请记得在上面打电话给免费
The char array message in telegram_input goes out of scope when the function returns. Dynamically allocate memory using malloc and return that char*. Remember to call free on it when done.


这篇关于在C中遇到字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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