Rockbox Technical Forums

Rockbox Development => Starting Development and Compiling => Topic started by: iR0x on January 05, 2007, 07:01:16 AM

Title: Pointers are killing me...Help needed.
Post by: iR0x on January 05, 2007, 07:01:16 AM
Hi I'm trying to make some kind of a dynamic array by making the following function
automatically build a new , bigger array as I add an element to it. Problem is, it doesn't
seem to work : I have no experience with pointers.

Code: [Select]
struct m_file {
char *fullpath;
char *name;
int isLast;
};


void addFile(struct m_file *list,struct m_file file)
{
int i = 0;
int atEnd = 0;

if (&list != NULL)
{
for(i = 0; atEnd == 0; i++)
{
if (list[i].isLast == 1)
atEnd = 1;
}

list[i].isLast = 0;
file.isLast = 1;

struct m_file newlist[i+2];
newlist[i+1] = file;
list = &newlist;
} else {
struct m_file newlist[1];
newlist[0] = file;
list = &newlist;
}
}

//***************************
// In some function :

struct m_file lst[1];
struct m_file test;
test.fullpath = "testPathblabla";
test.name = "nameuh";
addFile(lst,test);
Title: Re: Pointers are killing me...Help needed.
Post by: GodEater on January 05, 2007, 09:10:44 AM
Yeah - that's never going to work.

You can't do that with arrays - they're assigned memory statically - there's no such thing as a dynamic one (well not in C anyway).

So what your code is doing is basically trying to write over memory that doesn't belong to it. I'd be amazed if that even compiles to be honest.
Title: Re: Pointers are killing me...Help needed.
Post by: iR0x on January 05, 2007, 01:16:04 PM
First of, thanks for taking time to look into this GodEater!
It does compile but when I try to read out anything it just doesn't make any sense..

Im used to programming Java/PHP but those languages are object oriented so I'm kinda lost
in C...especially since most of the code I find when I google "C code" won't work...

Is there anything like the javadoc or the php reference for the rockbox api?
And I mean something different than just plugin.h ... because when you are new to C and
new to rockbox just knowing which method takes what still doesn't make much sense - at least
not to me...

The most important thing to me would be being able to work with something like ArrayLists in Java
which if I understand right, is usually done in C using linked lists, but even that won't work since
there's no malloc - well its what I understood so far.

People have told me to use the plugin_get_buffer (if I recal right - I don't have the sources right here)
but I can't seem to be able to give it any sense towards achieving a "dynamic" array - or
anything that would have the same functionality.

Could anyone help me out? Lets say I want "something" (array) to store structs, but that I don't
know how many there will be... Thanks in advance! :)

PS : I am trying to build a plugin.
Title: Re: Pointers are killing me...Help needed.
Post by: Rincewind on January 05, 2007, 02:05:15 PM
just make a huuuge static array?  ;D
Title: Re: Pointers are killing me...Help needed.
Post by: NicolasP on January 05, 2007, 02:06:47 PM
AFAIK, however you do it, you'll always have to decide of a maximum number of items for your "dynamic array" (because there are memory constraints). So you might as well just create a static array of that max size. Especially if you're not too familiar with C, I'd say it's probably the easiest way.
Title: Re: Pointers are killing me...Help needed.
Post by: iR0x on January 05, 2007, 03:19:18 PM
Well I guess so, but suppose one would have music playing in the background -
wouldn't the device "lock up" or become slow as hell?

PS: I so much appreciate being helped out so quicly - thanks guys.
Title: Re: Pointers are killing me...Help needed.
Post by: GodEater on January 05, 2007, 03:40:46 PM
Well I guess so, but suppose one would have music playing in the background -
wouldn't the device "lock up" or become slow as hell?

I believe the plugin buffer is designed to be both seperate from the audio buffer, and optimised as much as possible for each target to leave a large enough audio buffer so that what you're describing doesn't happen.
Title: Re: Pointers are killing me...Help needed.
Post by: iR0x on January 05, 2007, 03:43:00 PM
just make a huuuge static array?  ;D

Well I guess thats what I will be doing then  :P
Title: Re: Pointers are killing me...Help needed.
Post by: saratoga on January 05, 2007, 03:57:49 PM
just make a huuuge static array?  ;D

Well I guess thats what I will be doing then  :P

That should be pretty safe.  You could just make an array of pointers to file structs, and be only out 4 bytes per entry.  So even with a 1000 files as your max, you'd only have <= 4kb of RAM wasted.
Title: Re: Pointers are killing me...Help needed.
Post by: iR0x on January 05, 2007, 05:42:36 PM
Okay well thanks everyone... I'll do that.

Just to know..how much ram is there on iPod's 5g?
And if anyone knows...CPU speed?
Title: Re: Pointers are killing me...Help needed.
Post by: saratoga on January 05, 2007, 09:03:15 PM
Some 5Gs have 32MB, others have 64MB.  Theres also 96k of zero cycle IRAM.  CPU is by PP and I think runs at 60 or 75MHz.  They're 2x ARM7TDMI cores.  See here:

http://www.ipodlinux.org/PP5020
Title: Re: Pointers are killing me...Help needed.
Post by: Sentertainment on January 07, 2007, 10:10:42 PM
I haven't done too much C, mainly a bit of C++....anyways, I was sure there had to be some way.
I've found a method for it, but not exactly the same as in C++

http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.2.2.html
http://www.cs.cf.ac.uk/Dave/C/node11.html
http://www.fftw.org/fftw3_doc/Dynamic-Arrays-in-C.html
Title: Re: Pointers are killing me...Help needed.
Post by: bluebrother on January 08, 2007, 03:13:55 AM
The method you mentioned uses malloc. While it is possible to do this using dynamic memory Rockbox doesn't have malloc. This has been discussed over and over again (also in a thread of you), and malloc / dynamic memory isn't wanted in Rockbox. Which means this methods don't help for coding Rockbox.

Speaking of plain C, I'm pretty sure quite some of the people posting on this thread earlier would have mentioned malloc.
Title: Re: Pointers are killing me...Help needed.
Post by: lowlight on January 08, 2007, 11:46:41 AM
You can fake dynamic memory allocation in plugins by using plugin_get_buffer (to use the remainder of the plugin buffer) or plugin_get_audio_buffer (to use the larger playback buffer, but stop playback).
Title: Re: Pointers are killing me...Help needed.
Post by: GodEater on January 08, 2007, 02:02:58 PM
I'm assuming that the plugin_get_buffer() and plugin_get_audio_buffer() calls return only the one pointer to the start of that area of memory though? It's not like calling malloc() where you get a new pointer to the next bit of free memory each time ?
Title: Re: Pointers are killing me...Help needed.
Post by: lowlight on January 08, 2007, 04:29:01 PM
That is my understanding (without actually writing a plugin ;)). But once you get the pointer to the free mem and know how much is there it is easy to do your own bookkeeping to know where the free memory starts and how much is left (hence "you can *fake* dynamic memory allocation...").