简体   繁体   English

C 程序,初始化 int 数组上的“未初始化值错误”

[英]C program, “Uninitialized Value Error” on initialized int array

Update: Removing the print statement (line 52) entirely with all else the same as in the code below gave me 0 errors.更新:完全删除 print 语句(第 52 行),所有其他内容与下面的代码相同,这给了我 0 个错误。 The errors exist within my functions when printing (using philo[i]) and in this print statement (added for debugging), but do not exist if I run the entire program with no prints.打印时(使用 philo[i])和此打印语句(为调试添加)中存在错误,但如果我运行整个程序而没有打印,则错误不存在。 Any ideas what I'm doing wrong???任何想法我做错了什么???

Thank you for the help so far.感谢您迄今为止的帮助。 I have made a couple of changes based on comments received so far.根据目前收到的评论,我做了一些更改。

***** Original (Edited) Question ***** ***** 原始(已编辑)问题 *****

I can't figure out why I have been getting this error, "Uninitialised value was created by a stack allocation".我无法弄清楚为什么我会收到此错误,“未初始化的值是由堆栈分配创建的”。 I started with a completed program that works fine, but gives me a ton of uninitialized value errors.我从一个运行良好的完整程序开始,但给了我大量未初始化的值错误。 I have traced the problem down to a few lines of code by excluding all functions and adding additional print statements.通过排除所有函数并添加额外的打印语句,我已将问题追溯到几行代码。 This code is to solve the dining philosopher's problem using threads (homework), so I don't want to post too much.这段代码是用线程解决餐饮哲学家的问题(作业),所以不想发太多。 My code is now:我的代码现在是:

#include <all needed header files>

#define NUM_PHIL       5
#define MIN_EAT_TIME   10

pthread_t philosopher[NUM_PHIL];         // array to hold IDs for philosopher threads
pthread_mutex_t chopstick[NUM_PHIL];     // array to hold IDs for chopstick mutexes (locks)

// function definitions here: 
int philosopherFun(int *philo);
// All others have been bypassed at the time of my current problem

int main(int argc, char *argv[]) {
    
    int phil[NUM_PHIL];                  // Philosopher numbers ("names")
    
    for(int i = 0; i < NUM_PHIL; i++) {                 
        phil[i] = i + 1;
    }
    
    for(int i = 0; i < NUM_PHIL; i++) {                 
        // Print statement causes valgrind error to exist (as does calling a function using phil)
        printf("Value phil[%d] = %d\n", i, phil[i]);
    }
    
    // Initilize mutexes for chopsticks, report error as needed
    for(int i = 0; i < NUM_PHIL; i++) {
        if(pthread_mutex_init( stuff here) < 0) {
            // error reporting
            // print statement uses i
        }
    }

    for(int i = 0; i < NUM_PHIL; i++) {
        if(pthread_create(&philosopher[i], NULL, (void*) philosopherFun, (void*) &phil[i] ) != 0) {
            // error reporting
            // print statement uses phil[i]
        }
    }
    
    /* Code omitted as this is Homework */
    // Join threads created for philosophers (using pthread_join)
            // error reporting
            // print statement uses phil[i]

    // Destroy chopstick mutexes when done. (using pthread_mutex_destroy)
            // error reporting
            // print statement uses i

    printf("The philosophers have all finished eating and its time for bed. Goodnight...\n");
    return 0;
    
}

int philosopherFun(int *philo) {
    
    return 0;
}

Program output:程序 output:
Value phil[0] = 1值 phil[0] = 1
Value phil[1] = 2值 phil[1] = 2
Value phil[2] = 3值 phil[2] = 3
Value phil[3] = 4值 phil[3] = 4
Value phil[4] = 5值 phil[4] = 5
The philosophers have all finished eating and its time for bed.哲学家们都吃完了,该睡觉了。 Goodnight...晚安...

My Valgrind errors are:我的 Valgrind 错误是:
==46556== HEAP SUMMARY: ==46556== 堆摘要:
==46556== in use at exit: 0 bytes in 0 blocks ==46556== 退出时使用:0 个块中的 0 个字节
==46556== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==46556== 总堆使用量:0 分配,0 释放,0 字节分配
==46556== ==46556==
==46556== All heap blocks were freed -- no leaks are possible ==46556== 所有堆块都被释放——不可能有泄漏
==46556== ==46556==
==46556== ERROR SUMMARY: 10 errors from 1 contexts (suppressed: 0 from 0) ==46556== 错误摘要:来自 1 个上下文的 10 个错误(抑制:0 来自 0)
==46556== ==46556==
==46556== 10 errors in context 1 of 1: ==46556== 上下文 1 中的 10 个错误:
==46556== Conditional jump or move depends on uninitialised value(s) ==46556== 条件跳转或移动取决于未初始化的值
==46556== at 0x7FFF205A395F: ??? ==46556== 在 0x7FFF205A395F: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== by 0x7FFF2046FFFA: ??? ==46556== 由 0x7FFF2046FFFA: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== by 0x7FFF20478CF0: ??? ==46556== 由 0x7FFF20478CF0: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== by 0x7FFF2049D8B8: ??? ==46556== 由 0x7FFF2049D8B8: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== by 0x7FFF20475EF5: ??? ==46556== 由 0x7FFF20475EF5: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== by 0x7FFF20474061: ??? ==46556== 由 0x7FFF20474061: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== by 0x1000038CD: main (philo.c:52) ==46556== 由 0x1000038CD: 主要 (philo.c:52)
==46556== Uninitialised value was created by a stack allocation ==46556== 未初始化的值是由堆栈分配创建的
==46556== at 0x7FFF20475FDF: ??? ==46556== 在 0x7FFF20475FDF: ??? (in /dev/ttys000) (在 /dev/ttys000 中)
==46556== ==46556==
==46556== ERROR SUMMARY: 10 errors from 1 contexts (suppressed: 0 from 0) ==46556== 错误摘要:来自 1 个上下文的 10 个错误(抑制:0 来自 0)

line 52 is my print statement: printf("Value phil[%d] = %d\n", i, phil[i]);第 52 行是我的打印语句: printf("Value phil[%d] = %d\n", i, phil[i]);

which I believe accounts for 5 errors (phil[0] - phil[4]) and I call philosopherFun in my pthread_create on line 68 (this line of code included), again accounting for 5 errors (1 for each thread).我认为这导致了 5 个错误(phil[0] - phil[4]),我在第 68 行的 pthread_create(包括这行代码)中调用了哲人Fun,再次导致 5 个错误(每个线程 1 个)。 This function is currently only returning on the first line, so none of the rest of my program is involved (though I started with 50-250 errors depending on my parameters and which functions I excluded).这个 function 目前只在第一行返回,所以我的程序的 rest 都不涉及(尽管我开始时有 50-250 个错误,具体取决于我的参数和我排除的功能)。

I feel like my int array (int phil[]) is causing the problem, but it is immediately initialized, so I'm not sure how this is happening.我觉得我的 int 数组 (int phil[]) 导致了问题,但它立即被初始化,所以我不确定这是怎么发生的。 Please help!请帮忙!

Thanks, Kristine谢谢,克里斯汀

Edited... Added some comments from omitted code- Threads are joined and mutexes destroyed已编辑...从省略的代码中添加了一些注释-线程被加入并且互斥体被破坏

I also tried making int phil[NUM_PHIL] global by declaring it outside of my main function, but this made no change to the errors returned by Valgrind.我还尝试通过在我的主要 function 之外声明它来使 int phil[NUM_PHIL] 全局化,但这并没有改变 Valgrind 返回的错误。 defining phil[5] = {1, 2, 3, 4, 5};定义 phil[5] = {1, 2, 3, 4, 5}; as a global also didn't help.作为一个全球性的也没有帮助。

I believe this is where your problem comes from:我相信这就是您的问题所在:

int phil[NUM_PHIL];

and here和这里

if(pthread_create(&philosopher[i], NULL, (void*) philosopherFun, (void*) &phil[i] ) != 0) {
    more code ...
}

You're referencing a stack-allocated object (you're getting a pointer to the elements of the philo array).您正在引用堆栈分配的 object (您将获得指向 philo 数组元素的指针)。 When the main function returns, the memory will be deleted and the other threads will still be holding on to garbage.当主 function 返回时,memory 将被删除,其他线程仍将继续持有垃圾。 To be sure this is the problem, move your philo array to a global scope, or allocate the array on the heap instead.要确定这是问题所在,请将您的 philo 数组移动到全局 scope,或者改为在堆上分配数组。

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

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