简体   繁体   中英

[C]Malloc problems

I'm trying to write a program that uses some basic threading for an assignment. Below are the relevant snippets that I think are causing the problem.

The program runs fine with 25 or less threads but usually results in a segfault when 26 or more are used. This led me to think that my malloc statement were incorrect. Can anyone nudge me in the right direction? If you need more code to be posted, I'd be happy to provide it.

Also, these problems only come up when I run the program on my school's student machines. It appears to work fine my local machine. Might anyone know why?

Thanks for your time!

...

struct thread_args {
    struct bitmap *bm;
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    int max;
    int start;
    int end;
};

...

int num_threads; //Given by user input
struct bitmap *bm = bitmap_create(500, 500); //All threads share the same bitmap
int i;
pthread_t *thread_id = malloc(num_threads * sizeof(*thread_id));
struct thread_args *args = malloc(num_threads * sizeof(*args));
for (i = 0; i < num_threads; i++) { 
    args[i].bm = bm;
    args[i].xmin = xcenter-scale;
    args[i].xmax = xcenter+scale;
    args[i].ymin = ycenter-scale;
    args[i].ymax = ycenter+scale;
    args[i].max = max;
    args[i].start = bitmap_height(bm) * i / num_threads;
    args[i].end = bitmap_height(bm) * (i + 1) / num_threads;

    pthread_create(&thread_id[i], NULL, compute_image, &args[i]);
}

for (i = 0; i < num_threads; i++) {
    pthread_join(thread_id[i], NULL);
}

...

void* compute_image(void *arg)
{
    struct thread_args* args = (struct thread_args*) arg;
    int i,j;
    int width = bitmap_width(args->bm);
    int height = bitmap_height(args->bm);

    // For every pixel in the image...

    for(j=args->start;j<args->end;j++) {
        for(i=0;i<width;i++) {
            // Determine the point in x,y space for that pixel.
            double x = args->xmin + i*(args->xmax-args->xmin)/width;
            double y = args->ymin + j*(args->ymax-args->ymin)/height;

            // Compute the iterations at that point.
            int iters = iterations_at_point(x,y,args->max);
            // Set the pixel in the bitmap.
            bitmap_set(args->bm,i,j,iters);
        }
    }
    return 0;
}

...

valgrind log

==24919== 
==24919== HEAP SUMMARY:
==24919==     in use at exit: 1,000,000 bytes in 1 blocks
==24919==   total heap usage: 56 allocs, 55 frees, 1,018,884 bytes allocated
==24919== 
==24919== Searching for pointers to 1 not-freed blocks
==24919== Checked 87,112 bytes
==24919== 
==24919== 1,000,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
==24919==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==24919==    by 0x401256: bitmap_create (bitmap.c:21)
==24919==    by 0x400CEC: main (mandel.c:103)
==24919== 
==24919== LEAK SUMMARY:
==24919==    definitely lost: 1,000,000 bytes in 1 blocks
==24919==    indirectly lost: 0 bytes in 0 blocks
==24919==      possibly lost: 0 bytes in 0 blocks
==24919==    still reachable: 0 bytes in 0 blocks
==24919==         suppressed: 0 bytes in 0 blocks
==24919== 
==24919== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
--24919-- 
--24919-- used_suppression:      4 U1004-ARM-_dl_relocate_object
--24919-- used_suppression:      2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a
==24919== 
==24919== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)

Edit: Added compute_image code and valgrind log although the log is missing the error messages it was showing earlier today. The 1,000,000 bytes lost is something I know about.

Make sure that pthread_create is not returning an error. The machine has a global limit on the number of threads that can be spawned, and is probably fluctuating close to that limit. If you fail to spawn a thread, you will have a garbage pthread_t , which will likely cause pthread_join to blow up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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