Thread: About using memcpy on arrays:

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    Question About using memcpy on arrays:

    Hello everybody!
    This is my first post, hope it complies with the standards.

    I'm working on locating the origin of the sound using libfreenect which is a library that enables communication between the Kinect and the computer. Have set it and its working perfectly, it streams the data on the following code:

    Code:
    if(num_samples < c->max_samples - c->current_idx) {        memcpy(&(c->buffers[0][c->current_idx]), mic1, num_samples*sizeof(int32_t));
            memcpy(&(c->buffers[1][c->current_idx]), mic2, num_samples*sizeof(int32_t));
            memcpy(&(c->buffers[2][c->current_idx]), mic3, num_samples*sizeof(int32_t));
            memcpy(&(c->buffers[3][c->current_idx]), mic4, num_samples*sizeof(int32_t));
        } else {
            int first = c->max_samples - c->current_idx;
            int left = num_samples - first;
            memcpy(&(c->buffers[0][c->current_idx]), mic1, first*sizeof(int32_t));
            memcpy(&(c->buffers[1][c->current_idx]), mic2, first*sizeof(int32_t));
            memcpy(&(c->buffers[2][c->current_idx]), mic3, first*sizeof(int32_t));
            memcpy(&(c->buffers[3][c->current_idx]), mic4, first*sizeof(int32_t));
            memcpy(c->buffers[0], &mic1[first], left*sizeof(int32_t));
            memcpy(c->buffers[1], &mic2[first], left*sizeof(int32_t));
            memcpy(c->buffers[2], &mic3[first], left*sizeof(int32_t));
            memcpy(c->buffers[3], &mic4[first], left*sizeof(int32_t));
        }
    and I added the following to try to copy the data to two int arrays called xn and yn:

    Code:
    memcpy(&xn, mic1, first*sizeof(int32_t));    //Here I'll copy the samples from the two mics.
    
    memcpy(&yn, mic4, first*sizeof(int32_t));
    I attach the modified code below. What I'm trying to do after that is applying a crosscorrelation algorithm to calculate the Azimuth, I tried it with fake samples and it seemed to work fine but I'm stuck trying to apply it to the Kinect.

    It compiles and shows no errors but once I execute it I get core dumped and sometimes even Kernel Panic (I'm working on Ubuntu 12.10 OS).

    Thanks everybody for their attention!
    Attached Files Attached Files
    • File Type: c m.c (9.0 KB, 311 views)

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    memcpy() takes a void * pointer as its first argument. &xn is actually a pointer-to-pointer in this usage. When you pass an array to a function, it devolves to a pointer. IE you just pass memcpy(xn, ...) not memcpy(&xn, ...)

    Your compiler should be warning you about this, also.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    Thanks for the reply!

    Quote Originally Posted by nonpuz View Post
    memcpy() takes a void * pointer as its first argument. &xn is actually a pointer-to-pointer in this usage. When you pass an array to a function, it devolves to a pointer. IE you just pass memcpy(xn, ...) not memcpy(&xn, ...)

    Your compiler should be warning you about this, also.
    Thanks for your quick reply! I fixed that problem although checked twice and the compiler (I'm using gcc) doesn't warn me about that, only about a couple unused variables that aren't part of the program right now.

    Made it up to the memcpy part by reducing the array sizes (they were 99999999 long, now they're 100000) but still get the core dumped problem on the cross-correlation. I guess I'm running out of memory but still not sure. If anyone knows about it, i'd be very grateful.

    Here's the modified code and what I'm getting from execution.
    Attachment 12434

    Thanks!
    Attached Files Attached Files
    • File Type: c m.c (9.1 KB, 279 views)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int xn[100000], yn[100000], rxy[100000], rxy2[100000];
    You might want to look up what the default stack size is for threads on your machine.
    Because this little lot is approximately 1.6MBytes.

    If your stack size is too small, you just get segfaults.

    > memcpy(yn, mic4, num_samples*sizeof(int32_t));
    Would it not be better to make yn also of type int32_t ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Hey, Arthur91. I have signed up here recently too. I don't have too many tips to give but I also noticed that you were passing a pointer-to-pointer in your memcpy(). I notice there are comments in two different languages - Are you augmenting this c file with your own code? If so, did it compile before you started? I'd like to know what code worked, and what code you have added, if so. That will help to know what to focus on.
    Also, as Salem has mentioned I'm a little concerned about overflow on your array declarations xn[], yn[], rxy[], and rxy2[].
    Since you seem to be giving these arrays the same dummy placeholder size (just for testing, right?) I would refer to them using a #define statement, something like #define MAX_ARRAY_SIZE 100000. This way, you can change all instances of this later instead of going to each piece of code and changing it as you see it.
    Also, this code can be separated into multiple c files and a header file which would be easier to order in our heads when we are trying to review your code.
    I'll keep looking and see if I can find anything. Good luck!

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    Got it to work:

    Quote Originally Posted by Salem View Post
    > int xn[100000], yn[100000], rxy[100000], rxy2[100000];
    You might want to look up what the default stack size is for threads on your machine.
    Because this little lot is approximately 1.6MBytes.

    If your stack size is too small, you just get segfaults.

    > memcpy(yn, mic4, num_samples*sizeof(int32_t));
    Would it not be better to make yn also of type int32_t ?

    After a couple Kernel Panics finally managed to make it work. It still isn't giving me the angle but at least it no longer crashes.

    I gave the value Imax/2 to M and N that on the fake test where a fixed number of samples but here where variable and as such guess gave me the crashes :P

    Will now proceed to check how to get something different from NAN on the Terminal.

    Attachment 12435

    Thanks to everybody for their feedback, and I guess this thread can be closed now but otherwise I may upload the working Azimuth example here once I manage to get the angle in the algorithm.

    Here's the semi-functional code (will check how to get the Azimuth).


    Edit: Hi Derek! Well yeah this is only for testing purposes so I tried to keep it as simple as I could and sorry about the language, my main language is Spanish and since I'm working on a collaborative robotics project with an all-Spanish group I had to document the code that way. I modified the main libfreenect example that works with the microphone array in order to include my own Azimuth calculating algorithm. I can attach it separately if you want to check it apart.

    Thanks for the #DEFINE idea, I certainly want to simplify the code once I manage to get it working!
    Attached Files Attached Files
    • File Type: c m.c (9.2 KB, 304 views)
    Last edited by Arthur91; 01-13-2013 at 02:21 PM. Reason: Prevent spam on second reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy
    By ueg1990 in forum C Programming
    Replies: 3
    Last Post: 07-05-2012, 04:39 AM
  2. Help memcpy!
    By Kozay Novel in forum C Programming
    Replies: 9
    Last Post: 07-04-2012, 08:46 PM
  3. memcpy()
    By Moni in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2006, 05:31 AM
  4. Arrays of Strings. With functions/memcpy?
    By Kleid-0 in forum C Programming
    Replies: 7
    Last Post: 01-11-2005, 07:56 PM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM

Tags for this Thread