If so, you need to let the caller get the updated pointer back.
Up vote 1 down vote favorite share g+ share fb share tw.
I'm getting some strange behaviour here. Any help would be great. I start like this: int *event_positions = (int *) malloc(1 * sizeof(int)); // let us start with 1 and then add more within the method.
This should continue until we have all the flags we want. Int number_of_flags = event_extractor(vocal_data, size, event_positions); // HERE I WOULD LIKE TO USE THE VALUES OF event_positions BUT THE ARE WEIRD I.E. THEY DON'T MATCH THE VALUES BEING PRINTED IN THE LAST METHOD. Event extractor then passes on the variable to another method.
Simplified this looks like this: int event_extractor (int *audio_samples, unsigned int size_of_audio ,int *event_flags) { int number_of_flags = apply_threshold (lopass_samples, length, event_flags); // PRINT ARRAY event_flags HERE // VALUES ARE INCORRECT AND WEIRD } The last method: int apply_threshold (int *audio_samples, unsigned int size_of_audio, int *event_flags) { // DO SOME STUFF HERE. // PRINT THE ARRAY WHICH SHOW THE CORRECT VALUES. } I hope this is clear.
Basically I have an array which I pass as an argument and am having trouble accessing those values after the method has finished. EDIT 1 First File: int *event_positions = (int *) malloc(1 * sizeof(int)); // let us start with 1 and then add more within the method. This should continue until we have all the flags we want.
Int number_of_flags = event_extractor(vocal_data, size, event_positions); Second File: int apply_threshold (int *audio_samples, unsigned int size_of_audio, int *event_flags) { int flag = 0; // this will be the number of flags that I have bool run = true; // this will make sure that a minimum amount of time passes before I grab another flag. It's a guard. Int counter = 0; // this is the counter for the above guard.
Printf("\n\nCURRENT MINIMUM TIME: 20100 SAMPLES \n\n"); // event_flags0 = 1; // this first one is a dud. Within the loop we will automatically start adding flags int threshold = calculate_threshold_value(audio_samples, size_of_audio); printf("\n\n this is the threshold %d \n\n", threshold); int length = (int)size_of_audio; for (int I = 0; I threshold && run) { // ** is this realloc working? Event_flags = (int*)realloc(event_flags, sizeof(int) * (flag+1)); // reallocate the size of the array event_flagsflag = i; // printf("FLAG CREATED!
%i\n ", i); printf("EVENT FLAG %i %i\n",flag, event_flagsflag ); flag++; run = false; } if (!run) { counter++; if (counter > 20100) { // hardcode minimum size for now. Counter = 0; run=true; } } } printf("\n\n\n NUMBER OF EVENTS --- %d\n", flag); for (int I = 0; I threshold && run) { // ** is this realloc working? // event_flags = (int*)realloc(event_flags, sizeof(int) * (flag+1)); *event_flags = (int*)realloc(*event_flags, sizeof(int) * (flag+1)); // reallocate the size of the array *event_flagsflag = i; // printf("FLAG CREATED!
%i\n ", i); printf("EVENT FLAG %i %i\n",flag, *event_flagsflag ); flag++; run = false; } And now I'm getting an error that looks like this. Any ideas? C arrays pointers argument-passing link|improve this question edited Mar 10 '11 at 13:01 asked Mar 10 '11 at 12:25Eric Brotto1,571626 93% accept rate.
Without that, it's hard to see what you're doing wrong. As it stands you don't initialise the data that you've allocated, so it will likely contain random garbage. – Tim Martin Mar 10 '11 at 12:28 That shouldn't happen.
Is it literally just the return of apply_threshold which breaks it - i.e. There's no other operation between the two prints? Then you may have accidentally corrupted the value of event_flags in event_extractor's stack frame by overrunning a buffer elsewhere.
What OS/compiler - can you run valgrind? – Rup Mar 10 '11 at 12:28 We really need to see how you are referencing the int pointer in each of the functions otherwise you are just passing a pointer around. – Lazarus Mar 10 '11 at 12:29 @Tim Martin & @Rup : What's the betting that apply_threshold has event_flags++ in there somewhere to iterate over the values.
– Lazarus Mar 10 '11 at 12:31 I believe I've posted all the relevant code. Please let me know if it needs more clarification. – Eric Brotto Mar 10 '11 at 12:35.
If so, you need to let the caller get the updated pointer back. Something like: int apply_threshold (int *audio_samples, unsigned int size_of_audio, int **event_flags) { *event_flags = realloc ... } ... int number_of_flags = apply_threshold (lopass_samples, length, &event_flags); EDIT: In response to updated question: event_flags = (int*)realloc(event_flags, sizeof(int) * (flag+1)); // reallocate the size of the array This changes the local copy of the event_flags pointer. Caller will not see the change.
Use the method I described above. EDIT2: More detailed sample. Void foo(int * v) { v = 0; // The local copy of main's myvar is now 0.
Main's actual myvar is unchanged } void bar(int ** v) { *v = 0; // Main's myvar is now 0, we have a pointer to it and can modify it. } int main() { int * myvar = (int *) malloc(1); // Allocate 1 byte and make myvar point at this byte. Foo(myvar); // Call foo, passing a *copy of* myvar, which also points at the allocated byte bar(&myvar); // Call bar, passing a *pointer to* myvar, which again points to the allocated byte } EDIT3: In response to new question.
Is your "length" the number of ints or number of bytes? You're treating it as number of ints, which could cause your error if it really is number of bytes.
Erik. I believe I'm reallocating, but I may be doing it wrong. – Eric Brotto Mar 10 '11 at 12:37 @Eric Brotto: You are reallocating.
See my sample, apply_threshold should use int **event_flags, work with *event_flags, and the function should be called with &event_flags. – Erik Mar 10 '11 at 12:38 @Erik. This seems to work.
But now at my loop just after the realloc I'm getting: Program received signal: “EXC_BAD_ACCESS”. Previous frame inner to this frame (gdb could not unwind past this frame). Any ideas?
– Eric Brotto Mar 10 '11 at 12:45 @Eric Brotto, I think your problem is that realloc might "increase the memory in the same place" or "give you a bigger chunk elsewhere after copying" if the second one happens your code won't work, if the first happens it will. – Arkaitz Jimenez Mar 10 '11 at 12:48 @Eric Brotto: Update with your current code, or ask a new question – Erik Mar 10 '11 at 12:48.
You need to pass a pointer to pointer or a reference to pointer. A realloc might be moving your memory to another position and the caller won't notice. Realloc() changes the size of the memory block pointed to by ptr to size bytes.
The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to mal?
Loc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.