0

dear Friends of CS50X,

I have been working on pset4, filter-less, reflection for several hours now.

I think my code does what it is supposed to do. Also, in my solution I tried to use the concepts taught in the lecture (using pointers array) to do the swap.

My code does compile, however, upon running the code with an actual image I get the following error: segmentation fault (core dumped)

what did I do wrong? I thought by declaring the pointer array int*inter[height][width] I was already allocating all the memory needed.

I'm trying to use the inter pointer array as a temporary array to swap the values and paste them back into the original image[height][width] array.

should I use malloc? I think this is an error due to memory issues, trying to touch memory I can't mess with

I have read posts from other CS50X students, and it seems there is more than one way to solve this problem. Some of you just don't use pointers or memory addresses at all, while others do use memory addresses.

I compiled the filters.c file and it compiles ok.

Any input is appreciated, the code bellow is only for rgbtRed.

thank you, al.

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int *inter[height][width];

    //inter=malloc(height*width);

    for(int i=0;i<height;i++)
    {
        for(int j=0;j<width;j++)
        {
                *inter[i][j]=image[i][j].rgbtRed;
        }

        for(int j=width;0<j;j--)
        {
                image[i][width-j].rgbtRed=*inter[i][j];
        }

    }

}

1 Answer 1

1

"I thought by declaring the pointer array int*inter[height][width] I was already allocating all the memory needed."

No, this declaration merely created a 2-dimensional array of pointers of type int. This presents two problems. First, the pointers are pointers to type int, not RGBTRIPLEs. Second, to use the individual pointers in the array, the code either needs to assign memory address that have already been allocated, or would need to execute a malloc call and then save the address of the allocated memory in each array element. To be blunt, there's just a whole lot of missing code if you're allocating memory like this.

BTW, if your intent is to copy the data from the original array to a new array, you'd need to malloc memory and then copy. Be sure that you're not just inserting the address of the pixel from the original into the new array. That doesn't copy data, it merely assigns a second pointer to the same physical memory.

There's more going on here, but this will get you started. You deserve a chance to be able to resolve the other problems as you find them. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • thank you for your comments @Cliff B Commented Jul 31, 2022 at 20:43
  • hello again @Cliff B, it seems there are at least 2 different routes to solve the reflection problem. a. not using pointers, probably easier. My strategy was to copy the image[width][height] first by copying the rgbt values into a new array. I then flipped the values and paste them back on the the original image[width][height] array b. the second option seems to involve using pointers and malloc. I didn't try going down this option, I think it is more challenging. But I will give it a try once I finish all problems in pset4. thank you Commented Jul 31, 2022 at 22:35
  • 1
    Hint: There's a 3rd way. It can be done without the use of additional arrays or memory, other than a temporary holding var for one pixel. ;-)
    – Cliff B
    Commented Aug 1, 2022 at 2:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .