problems building a vector, working with arduino Uno

Good morning all. I'm new and my English is not so good.
:frowning: :frowning:
The problem I have is this. I need to create a vector containing integers in a particular order, for example: 3,4,3,4,3 and back to another function. The problem that arises is that apparently the return pointer is corrupted in some way. This is the code:

int * calculador_de_diagonales::funcion_superior_vector(int round_floor, int round_ceil, int valor_resto_dem_resto, int denominador){
    int i=0,r=1;
    int *vec_v = (int*)malloc(denominador);
//    memset(vec, 1, denominador);
//    int vec_v[denominador];
    for(i=0;i<denominador;i++){
        if(((i+1)%2 == 0)&&(r <= valor_resto_dem_resto)){
            vec_v[i]=round_ceil;
            r++;
        }else{
            vec_v[i]=round_floor;
        }
    }
    for(i=0;i<denominador;i++){
        Serial.print(vec_v[i]);/*Serial.print("(");Serial.print(i);Serial.print(")");*/Serial.print(",");
    }
    Serial.println("");
//    int vec[10] = {0,1,2,3,4,5,6,7,8,9};
    return vec_v;
}

before returning the code, see what contains the vector and all good, but to see him out of the function does not contain what I want, but pure garbage.

This is the vector that I want: 4,3,4,4,4,
This is the vector that appears. 2,2,2,2,2 :frowning:

    int *vec_v = (int*)malloc(denominador);

The argument for the malloc() call is the number of bytes to allocate. An int is two bytes.

You are writing beyond the end of the array that you allocate, since you assumed that the argument was the number of elements to allocate space for.

then how can I fix ... Sorry for my ignorance. Please. How would you do that? =(
I used this and not fix the things. What do I do? ... I'm really lost.

int *vec_v = (int*)malloc(denominador*sizeof(int));

the way I'm returning the vector is:

int * fun(int tam){
    int vect[tam];
    int *p;
    p = vect;
    
    /*some stuff with vect*/
    return p;
}

after that, in the call to the function is

int *n = fun(9);

and I check what's inside the vector.

    for(i=0;i<denominador;i++){
        Serial.print(n[i]);Serial.print(",");
    }
    Serial.println("");

and this is what happens. (clik to better view)

What do I do? ... I'm really lost.

That is the correct method of allocating storage space for n ints, so you aren't too lost.

the way I'm returning the vector is:

wrong. The variable vect is local to the function. It goes away when the function ends. Returning a pointer to memory location that a function was using is not going to work. The pointer still points to the memory location, but some other function can overwrite that space at any time.

(clik to better view)

Whatever I'm supposed to click isn't making it past the proxy server I'm using.

does not matter. it was just a view of what you print.

(clik to better view)

and I realized the problem of memory. Now, How to solve it?

It can not be a static variable because their size varies. I can not pass by reference because either know the size before entering.
Could you give me an example of a how to do it?

And really txs for answering my post, i feel better talking to you in this with this problem. It's for a thesis and am really worried.
Txs :slight_smile:

It can not be a static variable because their size varies.

If there is a maximum size, make a static (or global) array of the maximum size.

I can not pass by reference because either know the size before entering.

You must know the size. You pass it to the function.

Could you give me an example of a how to do it?

If the array really must be created in the function, and the size is passed in, you can use malloc() to allocate memory. If you do, you MUST free() the memory when you are done with it and you must be aware that the Arduino has very little memory to go around and you must be aware of the potential problems of fragmenting memory when using malloc().

i dont know the size of the vector because is a serial entrance.
This is what am i doing... look at it... http://www.youtube.com/watch?v=wsfeWsBlN3g
for now it is just drawing in square letters but i am upgrating the programation for a better movement.

i think i will solve it by passing the vector by reference. Any problem with that? :slight_smile:

If there is a maximum size, make a static (or global) array of the maximum size.

The maximun size cant be stored because is to long for the arduino RAM.

If the array really must be created in the function, and the size is passed in, you can use malloc() to allocate memory. If you do, you MUST free() the memory when you are done with it and you must be aware that the Arduino has very little memory to go around and you must be aware of the potential problems of fragmenting memory when using malloc().

I know that using malloc() i need to free the memory

on the other hand. i dont know if passing de vector by reference there is a problem in memory. Does there?

I managed to pass it by reference using a little more memory.

look at the project link. :slight_smile: am proud of it. probably is not so big deal but i like it

The maximun size cant be stored because is to long for the arduino RAM.

Then, you won't be able to store the vector, anyway.

i think i will solve it by passing the vector by reference. Any problem with that?

I'm not sure how you can, if the vector isn't a fixed size.

That's a really nice machine you have, and you should be proud of it. But, you really should look into a different approach to programming it's motions. GRBL is a standard for driving CNC machines, which is what you have. All that the machine needs to know is the next point to go to. Given that the machine moves in 2D, and the pen is either up or down, there are G codes to define where to go with the pen down, and M codes to define where to go with the pen up. The whole instruction can be 20 bytes or less.

There are any number of free GRBL converters that take a variety of input formats, and generate the M and G codes and write them to the serial port. Or, you can write your own, based on the format of the data that you have.

The maximun size cant be stored because is to long for the arduino RAM.

Well not so strict.

Then, you won't be able to store the vector, anyway.

...GRBL is a standard for driving CNC machines...

GRBL understand that it is the software that goes on your PC to send data to arduino. But I'm doing in the arduino a standard interface for any GRBL or software that send the data to some specific form. So I use MATLAB for testing. But it works with Octave and even a cell phone.

What I do in the Arduino CHIP is a standard for setting up a CNC machine free hardware and free software.

All the work I have done from mechanics to electronics and programming. It's too big for one person in a graduation thesis. So do not even connect with GRBL. But I thank you your participation.

Ill post if i solve the vector problem with this wait.

Oh... and in my university ask me to make my own code. :slight_smile: