#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Durham, UK
    Posts
    68
    Rep Power
    12

    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!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Please, coud you show the code? It would make help you easier.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Durham, UK
    Posts
    68
    Rep Power
    12
    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).

    Code:

    #include <stdio.h>

    FILE *file;
    int values[4];
    int *pointer, intsize, i, n;

    main()
    {
    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]);
    }

    fclose(file);

    file = fopen("values.txt", "r");

    pointer = (int *) calloc(4, intsize);

    fread(pointer, intsize, 4, file);

    for(i=0;i<=4;i++)
    {
    printf("\n\nPointer address %i is %i", i, (pointer + i));
    printf("\nValue %i is %i", i, *(pointer + i));
    }
    fclose(file);
    }
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Durham, UK
    Posts
    68
    Rep Power
    12

    oops.....


    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 :)
  10. #6
  11. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    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.

    Frank Zappa
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Durham, UK
    Posts
    68
    Rep Power
    12
    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:

IMN logo majestic logo threadwatch logo seochat tools logo