Rockbox Development > Starting Development and Compiling

Pointers are killing me...Help needed.

(1/4) > >>

iR0x:
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: ---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);

--- End code ---

GodEater:
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.

iR0x:
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.

Rincewind:
just make a huuuge static array?  ;D

NicolasP:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version