Question

When I run debugging it points to the line: 105 (and writes "segmentation fault" in the left corner). I don't know what does red line in "Call stack" window mean...

Please, tell waht it is and where can I read more about it.

Here is the function's code:

/* Separates stereo file's samples to L and R channels. */
struct LandR sepChannels_8( unsigned char *smp, unsigned long N, unsigned char *L, unsigned char *R, struct LandR LRChannels )
{
    int i;

    if ( N % 2 == 0 )   // Each channel's (L,R) number of samles is 1/2 of all samples.
    {
        L = malloc(N / 2);
        R = malloc(N / 2);
    }
    else
        if ( N % 2 == 1 )
        {
            L = malloc(N + 1 / 2);
            R = malloc(N + 1 / 2);
        }

    int m = 0;
    for ( i = 0; i < N; i++ )    // separating
    {
        L[m] = smp[2 * i + 0];   // THIS IS THE "LINE: 105"
        R[m] = smp[2 * i + 1];
        m++;
    }
    return LRChannels; 
}

And here is sreenshot of the windows (easier to show it instead of trying to describe)

enter image description here

Was it helpful?

Solution

The line in red is your call stack: Basically, it's telling you that the problem occurred inside the the sepChannels_8() function, which was called from main(). You have, in fact, several bugs in your sepChannels_8() function.

Here is my analysis:

struct LandR sepChannels_8(unsigned char *smp, unsigned long N, unsigned char *L, unsigned char *R, struct LandR LRChannels)

sepChannels_8 is a function that takes five arguments of varying types and returns a value of type struct LandR. However, it's not clear what the five arguments passed to the function are. unsigned char *smp appears to be a pointer to your audio samples, with unsigned long N being the total number of samples. But unsigned char *L, unsigned char *R, and struct LandR LRChannels, it's not at all clear what the point is. You don't use them. unsigned char *L and unsigned char *R, your function promptly discards any passed-in pointers, replacing them with memory allocated using malloc(), which is then thrown away without being free()d, and the only thing you do with struct LandR LRChannels is simply return it unchanged.

{
    int i;

    if ( N % 2 == 0 )   // Each channel's (L,R) number of samles is 1/2 of all samples.
    {
        L = malloc(N / 2);
        R = malloc(N / 2);
    }
    else
        if ( N % 2 == 1 )
        {
            L = malloc(N + 1 / 2);
            R = malloc(N + 1 / 2);
        }

Now this is interesting: If the passed-in unsigned long, N, is an even number, you use malloc() to allocate two blocks of storage, each N / 2 in size, and assign them to L and R. If N is not even, you then double-check to see if it's an odd number, and if it is, you use malloc() to allocate two blocks of storage, each N in size, and assign them to L and R. I think you may have intended to allocate two blocks of storage that were each (N + 1) / 2 in size, but multiplication and division happen before addition and subtraction, so that's not what you get. You also fail to account for what happens if N is neither even nor odd. That's OK, because after all, that's an impossible condition... so why are you testing for the possibility?

    int m = 0;
    for ( i = 0; i < N; i++ )    // separating
    {
        L[m] = smp[2 * i + 0];   // THIS IS THE "LINE: 105"
        R[m] = smp[2 * i + 1];
        m++;
    }

Mostly pretty standard: you've got a loop, with a counter, and arrays to traverse. However, your terminating condition is wrong. You're walking down your smp data two steps at a time, and you're doing it by multiplying your array index, so your index counter needs to run from 0 to N / 2, not from 0 to N. (Also, you need to account for that last item, if N was odd...). Further, you're using m and i for the same thing at the same time. One of them is unnecessary, and redundant, and not needed, and extra.

    return LRChannels;
}

And, return the LRChannels struct that was passed in to the function, unmodified. At the same time, you're discarding the L and R variables, which contain pointers to malloc()-allocated storage, now lost.

What were L and R supposed to be? It almost looks as though they're supposed to be unsigned char **, so you could give your allocated storage back to the caller by storing the pointers through them... or perhaps struct LandR has two elements that are pointers, and you were intending to save L and R in the struct before returning it? for L and R, and LRChannels, I don't see why you're passing them to the function at all. You might as well make them all automatic variables inside the function just as int i and int m are.

OTHER TIPS

You have malloced N/2 elements in the array but in the loop, your counter goes from 0 to N. And that will imply that you are trying to access elements from 0 to N because you increment m on every iteration. Obviously, you will get a seg fault.

What is the value of 'smp'?

It either needs to have been allocated prior to the call to sepChannels_8(), or point to a valid placeholder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top