Re: Passing values from spinbuttons to a calculation/drawing function via

To Eric Cashon or other,
I am still in the process of rebuilding my app based on the example you sent me “You are on the right track there” in response to “Passing values from spinbutton callbacks to a calculation drawing function”. I have been implementing dynamic memory allocation with malloc(), due to the many large arrays required. I am passing the values using a struct containing drawing-area widgets, single-valued integers and doubles, and one and two-dimensional C arrays. I have it working with three notebook pages each with their own drawing area function (contained in separate .c files compiled together). Each of the drawing areas are about 4000 x 800 and each also has similarly sized dynamically allocated arrays. The drawing areas are declared in the struct and defined as NULL in main() as in your example and I until now have simply added more drawing areas into the struct following that example. I have then added a fourth notebook page and a drawing/calculation function which compiles and runs (showing a blank notebook page as expected). However, upon trying to implement a fourth drawing area a segmentation fault results, even by simply declaring it in the struct, with or without the NULL pointer being defined in main(), (which until now had worked for the other drawing areas).

The program runs with the struct containing:
3 drawing areas (3800x800 pixels),
30 single-valued integers,
60 single-valued doubles,
9 one-dimensional double arrays (each 4096 in length),
3 two-dimensional double arrays (40x5),
4 two-dimensional double arrays (4096x1024),
but won’t run without segmentation during the initial drawing of the window when compiled with just one more drawing area added.

Do I need to dynamically allocate memory for the drawing areas as well, if so how do I do this, or is it something to do with the size of the structure? I’ve checked the code numerous times for mistakes and omissions to no avail, help….
Thanks,
Roger.

C language, Ubuntu 18.04.1 LTS, Gtk 3.2 and 8GB RAM, 64 bit

Hi Roger,

First find where the seg fault is. Seg faults can be tough to find. For example the following has seg fault errors and the code will probably run without being shut down by the OS for segfaults. To help find segfaults compile your code with -g and run valgrind. Another thing to try is a static analyzer. Give cppcheck a try and see if that turns up anything. Both valgrind and cppcheck are easy to use and if you are doing a fair amount of pointer arithmetic it is a good idea to check for seg faults with both of these programs. Hopefully they can pin point the error.

Eric


/*
    gcc -Wall -g valgrind1.c -o valgrind1
    valgrind ./valgrind1
    cppcheck valgrind1.c
*/

#include<stdio.h>
#include<stdlib.h>

int main()
  {
    int i=0;
    char string1[5]={'T', 'e', 's', 't', '\0'};
    char *string2=malloc(5*sizeof(char));
    char *p=string2;

    for(i=0;i<5;i++)
      {
        *p=string1[i];
        p++;
      }

    p=string2;
    printf("String1 %s\n", string1);
    printf("String2 %s\n", string2);
    printf("Seg Fault1 %c\n", string1[5]);
    printf("Seg Fault2 %c\n", *(p+5));
    free(string2);
    return 0;
  }

Here is another tool to check,

gcc -Wall -g -fsanitize=address valgrind1.c -o valgrind1

Eric

Hi Eric and others,

I am embarrassed to have found that the problem can be eliminated by making a change in every file.c in the program, in order to force a complete re-compilation.

Sheepishly Yours,

Roger


Virus-free.
www.avg.com

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.