tags:

views:

215

answers:

3

Hi,

I am rewriting some code from having a regular pointer to where the pointer is in a struct.

This is the original code which works:

int wrote = sf_writef_double(outfile, *mono_channel, frames);

In the new code, where I have put the mono_channel pointer in a struct have I written

int wrote = sf_writef_double(outfile, data->mono_channel, frames);

The compiler doesn't complain, but the program crashes.

So the question is. Is data->mono_channel the same as *mono_channel?

Hugs, Louise

Edit: To be more precise here is what I do:

ltfat_complex* fm;
fm = malloc(data->L * sizeof(ltfat_complex));
if (fm == NULL) { puts("fm malloc failed"); exit(1); }

/* Writes output to fm */      
idgt_fac(data->coef, gdf, data->L, 1, 1, data->a, data->M, fm);

free(data->mono_channel);

data->mono_channel = (double*) fm;
free(fm);

...

int wrote = sf_writef_double(outfile_handler, data->mono_channel, frames);

Could the problem be, that I try to re-use the data->mono_channel pointer?

Edit2: Here are the entire source codes (~ 700 lines each):

Old which works: http://www.student.dtu.dk/~s011392/gabor-io.zip

New: http://www.student.dtu.dk/~s011392/gui.zip

Sadly they require a lot of libraries to compile: ltfat from SVN, fftw3, lapack, blas, sndfile.

But there tey are =)

+4  A: 

You're dereferencing mono_channel in the first version but not in the second. Try

int wrote = sf_writef_double(outfile, *(data->mono_channel), frames);
Michael Myers
If I do that I get this compiler error:audio_save.c:25: error: incompatible type for argument 2 of ‘sf_writef_double’/usr/include/sndfile-64.h:517: note: expected ‘const double *’ but argument is of type ‘double’make: *** [audio_save.o] Error 1Any idea what then could be the problem?
Louise
Was `mono_channel` of the exact same type that `data->mono_channel` is?
Michael Myers
Mordachai
Sorry, sounds like you actually have a deeper problem. What exactly does sf_writef_double expect - an array of doubles, or a single double? If an array, then look at how you've setup your struct - you problably need its member to a be a pointer to doubles, and that pointer needs to be allocated just as the old plain pointer was (i.e. it needs to be assigned the array of doubles at some point before calling the above function)
Mordachai
Yes, the types are the same. Both doubles. But I try and re-use the mono_channel pointer, which might then be the problem? I have added the code snippet to the question, and added the entire source, for both cases. ~ 700 lines each. Is the re-use of pointers legal?
Louise
+1  A: 

data->mono_channel is the same as (*data).mono_channel

If you want the contents of the pointed to variable I suggest you want *(data->mono_channel)

You may want to re-read K&R.

PP
+2  A: 

No, data->mono_channel and *mono_channel are not the same thing. *(data->mono_channel) is the same as *mono_channel.

I really wonder how you got by with *mono_channel in your program if you wanted to pass a pointer to a double instead of a double. Clearly mono_channel was a pointer to a pointer to a vector of doubles. Most likely mono_channel was a pointer to an array (which is actually a vector) of doubles.

double *mono_channel[1000];   // let's say

Anyway, if you're crashing, it's probably because you haven't allocated any storage and pointed mono_channel to it, instead you've just defined it as a member of your struct that is a double * and are using it. You either need to point mono_channel to a predefined global array or allocate it with malloc/new.

Southern Hospitality
+1 for going into detail. (My brain is fried right now from working with VBA all day.)
Michael Myers
Now that you mention it, I wonder too why it worked in the original. The sndfile specs says:sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;I have linked to the source code in the question, is case you can see what's going wrong?
Louise