Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resizing an fvec_t #230

Closed
Lord-Kamina opened this issue Jan 19, 2019 · 4 comments
Closed

Resizing an fvec_t #230

Lord-Kamina opened this issue Jan 19, 2019 · 4 comments
Labels

Comments

@Lord-Kamina
Copy link

Is it at all recommended to do this? I've seen examples of code that just changes the "length" value for an fvec_t but AFAICT, without really resizing the array, that could only result in aubio trying to use memory containing something else?

@piem
Copy link
Member

piem commented Jan 22, 2019

hi @Lord-Kamina

Yes, fvec_t is one of the few public structures (that is one declared in the .h not the .c), so you can create it on the stack quickly and fill it with whichever values suits you.

Here is an example of creating an fvec_t on the stack and pointing its members to the second half of the second row of a matrix:

  fmat_t *m = new_fmat(2, 16); // allocate a new matrix                          
  fvect_t v;                   // created on the stack                           
  v.length = 8;                // set its length                                 
  v.data = m->data[1] + 8;     // point to start of data                         
  // &v is now ready to use as any other vector                                  
  fvec_ones(&v);               // fill our 'vector'                              
  fmat_print(m);               // the content of m has changed               
  // clean-up                                                                    
  del_fmat(m);                 // nothing else to delete 

Of course you will want to make sure read/write out-of-bounds accesses and double free are prevented when writing this kind of code.

Let us know if this answers your question!

cheers, piem

@Lord-Kamina
Copy link
Author

hi @Lord-Kamina ...

@piem thanks, but not really.

What I want to know is if for example doing something like:

fvec_t* vec = new_fvec(4);
vec->length = 8;
vec[5] = 1.0;

Is safe to do?
From what I saw of the implementation, I would assume not; but I'd like to be sure.

Sent with GitHawk

@piem
Copy link
Member

piem commented Jan 23, 2019

hi @Lord-Kamina

No, that program should fail, since vec was allocated with a memory large enough to fit only 4 elements, no more. You could shrink a vector instead of trying to make it larger, but calling del_fvec(vec) would then cause a memory leak, unless the length member is set back to the original value.

You are probably best to allocate all the memory required for your program at start time, then use the example provided above.

cheers, piem

@piem piem added the question label Feb 26, 2019
@piem
Copy link
Member

piem commented Feb 26, 2019

Hi @Lord-Kamina

Closing this issue now, I hope it answered your questions.

best, piem

@piem piem closed this as completed Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants