Thread: Seg fault

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    Seg fault

    For some reason this code is seg faulting when I try to run it. count=g_width * j; seems to have something to do with it. Please help


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "transform.h"
    
    
    
     void mirror(struct pixel *theArray[])
    {
        struct pixel * temp;
       
        int count;
        int i;
        int j;
    
       for(j=0; j<g_height; j++)
       {
        count=g_width * j;
        for(i=0; i < g_width/2; i++)
        {
            temp=theArray[i+count];
            theArray[i+count] = theArray[g_width-1-i + count]; 
            theArray[g_width-1-i + count]; 
        }
       }
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    You might be trying to access an element outside of array theArray. You could print the value of each index being used before trying to access the element in that index: at least one of them might be outside the bounds of the array.

    Also, and I'm not completely sure, but I think you don't need to write

    Code:
    struct pixel *theArray[]
    as the argument to function mirror. I suspect the operators * and [] are "cancelling" each other. Maybe you could just write

    Code:
    struct pixel theArray
    Last edited by stdq; 04-20-2015 at 08:58 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by stdq
    I suspect the operators * and [] are "cancelling" each other.
    In the context of a parameter declaration, they are not operators. Rather, this:
    Code:
    void mirror(struct pixel *theArray[])
    is equivalent to:
    Code:
    void mirror(struct pixel **theArray)
    i.e., theArray is declared to be a pointer to a pointer to struct pixel.

    Anyway, titaniumnuke: you should explain what your mirror function is supposed to do. It would be better if you could provide the smallest and simplest compilable program that demonstrates this problem. This could be a simple program that defines struct pixel, creates your array, populates it, then calls mirror (and maybe another function that would print the array should the mirror function actually work). This way, you would show us code that we can compile and run if necessary, along with your test input (i.e., populating the array). You should also tell us your expected output (e.g., the expected print out of the array after mirror has been called on it) and actual output (in this case the segfault).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,995
    Post your main(), as in this thread -> Help with pointers and ppm images

    Here are some examples of passing 2D arrays, and things which mimic 2D arrays.
    Code:
    void foo ( int arr[][10] ) {
    }
    int main ( ) {
      int a[10][10];
      foo(a);
    }
    Code:
    void foo( int *arr[] ) {
    }
    int main ( ) {
      int *a[10]; // malloc each a[i]
      int **b; // malloc b and each b[i]
      foo(a);
      foo(b);
    }
    If you try to mix and match other combinations, it will not work!


    If you're getting ANY warnings at all from your compiler, then you need to be posting your code AND your warnings.

    Ignoring warnings and just posting code saying "seg fault" is just dumb. It's a complete waste of our time and yours.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  3. i have a seg fault
    By ck1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 12:02 AM
  4. Seg Fault... ???
    By justin69enoch in forum C Programming
    Replies: 2
    Last Post: 01-26-2003, 05:00 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread