结构值未在函数内正确执行 [英] Structure values are not executed correctly inside a function

查看:54
本文介绍了结构值未在函数内正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在编写一个c代码,我创建了一个如下所示的结构,并且'typedef'可以轻松访问它。



Hi all,

I am writing a c code and I made a struct as shown below and also 'typedef'ed it to have easy access.

struct files
{
    char* Name;
    int size;
    time_t time;
    int Fileblock[32];
};

typedef struct files file;





我已按以下方式为命令分配空间:





I have allocated space for commands in the following way:

#define BUFFERSIZE 64
int buffsize= BUFFERSIZE;
int i=0;
char **commands = malloc(sizeof(char)* buffsize);





然后我在启动函数中设置值如



Then I set values in my launch function as under

int launch(char *line)
{
    commands[i] = strtok(line," ");
    printf("%s\n", commands[0]);
    i++;
    while ((commands[i] = strtok(NULL, " ")) != NULL)
    {
        printf("%s\n", commands[i]);
        i++;
    }
    file fnow;
    
    fnow.Name = commands[1]; 
    //printf("Getting status for -%s-\n", commands[1] );
    status = stat(commands[1], &buf);
    //printf("the status given by stat on putting test.txt is %d\n", status);
    fnow.size = buf.st_size;
}





然后我将fnow对象传递给函数。





And then I pass the fnow object to the function.

goput(fnow);





现在,当我打印文件的名称时,它打印正确,但是,当我尝试获取文件的大小时,它给我零。



如果我对下面的值进行硬编码,它会自动将文件的名称附加一些随机字符,如?或 - ,它可以正常工作。



now, when I print the Name of the file, it prints correctly, however, when I try to take the size of the file it gives me zero.

It automatically appends the Name of the file with some random character like a '?' or '-' if I hardcode a value like below, it works fine.

fnow.Name=commands[1]; 
//printf("Getting status for -%s-\n", commands[1] );
status = stat("foo.txt", &buf);
//printf("the status given by stat on putting test.txt is %d\n",status);
fnow.size = buf.st_size;





如何避免这种情况?< br $> b $ b

我尝试过的事情:



到目前为止,我差不多了丢失。我需要关于如何避免这种事情的想法,因为我必须使用由用户给出的动态输入。



How do I avoid that?

What I have tried:

So far, I am pretty much lost. I need ideas on how can I avoid this kind of thing, because I have to work using a dynamic input which is given by the user.

推荐答案

你可能正在经历一个缓冲区溢出的情况,但很难说是因为你遗漏了某些信息。



例如



*如何声明变量命令?

它可能是一个char *的数组,但是你需要显示它吗?



同样有了这个声明,你没有指定命令[1]的值,但地址。

You are probably experiencing a case of buffer overrun, but it is hard to tell because you have left out certain information.

For example

* How is the variable commands declared?
It is probably an array of char*, but you need to show that?

Also with this statement, you are not assigning the value of commands[1], but the address.
fnow.Name = commands[1];





尝试为名称分配内存



Try either to allocate memory for Name

fnow.Name = (char*)calloc(strlen(commands[1]) + 1, sizeof(char));
strcpy(fnow.Name, commands[1]);



别忘了完成后使用免费(fnow.Name)。



您也可以更改


Don't forget to use free(fnow.Name) when you are done.

You can also change from

char* Name to char Name[25] // or whatever a proper length would be



这是一个非常简单的测试,看看你是否遭受缓冲区溢出。


This is a very simple test to see if you suffer from buffer overrun.


这篇关于结构值未在函数内正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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