August 22nd, 2003, 05:54 AM
Dynamic memory allocation
I'm (pretty) new to C coding and I'm having trouble with dynamic memory allocation. What i want to do is read in a .wav file, then use information in the header to locate and extract the raw sample data which is then written to a .txt file.
My method is as follows -
setup a structure as per the RIFF header
use fread to load data into the structure
allocate memory for an array using calloc. The size of this memory is based on information in the RIFF header, and so cannot be determined at run-time
use fread to write sample data into the allocated memory
My problem is how to access the data in the memory i've allocated. I have one pointer to the start of the array, and obviously i can manipulate the pointer to get the address of the 'i'th element of the array, but i can't seem to access it. Something like:
*(pointer + i)
(ie. return the conents of address (pointer + i))
doesn't seem to work.
If anyone can offer any advice, I'd be very grateful!
August 22nd, 2003, 06:15 AM
Please, coud you show the code? It would make help you easier.
August 22nd, 2003, 06:22 AM
Sure -- check out the attached code.
It is a piece of prototype code i wrote to try to access memory as explained; so it just creates an array, writes said array to a text file, then reads in the data to a block of memory that i allocate. In this way it models the '.wav file' example.
However -- it does illustrate my problems; as i can't seem to access the data (the last few lines are trying to print data from the allocated memory to stdio).
int *pointer, intsize, i, n;
intsize = sizeof(int);
for(i = 0; i <= 4; i++)
values[i] = i*5;
file = fopen("values.txt", "w");
for(i = 0; i <= 4; i++)
fprintf(file, "\n%i", values[i]);
file = fopen("values.txt", "r");
pointer = (int *) calloc(4, intsize);
fread(pointer, intsize, 4, file);
printf("\n\nPointer address %i is %i", i, (pointer + i));
printf("\nValue %i is %i", i, *(pointer + i));
August 22nd, 2003, 09:24 AM
You are writing text, then reading binary! You would need to do an fwrite() in the first place.
Please enclose your code in "code" tags!
August 25th, 2003, 08:32 AM
uh, yup, that'd be the one!
sometimes you can't see the wood for trees, right? Ta for pointin that out, mitakeet.....
sorry bout the code quotes, too :)
August 26th, 2003, 04:38 AM
Is that a void main() I see?
Shame on you!
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
August 26th, 2003, 06:14 AM
Yes, TechNoFear, it is.
Sadly, I'm pretty new to coding; and I'm largely self taught (I got a little bit of priming in my engineering course, but not to a very great extent), so i didn't even know that void main() was a bad thing!
But I do now, so thanks :)
A question, tho -- if void main() is bad; and isn't in the ANSI standard, then why don't compilers simply enforce a return of int (for example) when none is specified (like in my code below).
Would this simply cause more problems? :confused: