GP32 Memory Alloc Stuff


Arne

Still Fresh
Joined
Oct 18, 2004
Messages
11
Hi there,

Recently wanted to dig myself into filedialogs ... It works perfectly well if I just Output informations gathered by GpDirEnumList as long as the memory alloc'd for GPDIRENTRY in the same function is used ... I get random output if I use the following:

Code:
#include "defines.h"
#include <stdlib.h>
#include "gpdef.h"
#include "gpstdlib.h"
#include "gpgraphic.h"
#include "gpstdio.h"
#include "gpfont.h"
#include "gpstdio.h"


/* Converts 8bit rgb values to a GP32 palette value */
#define GP_RGB24(r,g,b) (((((r>>3))&0x1f)<<11)|((((g>>3))&0x1f)<<6)|((((b>>3))&0x1f)<<1))

/* Global variables */
GPDRAWSURFACE gpDraw[2];

struct PathInfo
{

    char name[16];
	bool directory;

};

short PrintFileInfo (char*, PathInfo*);

/* Sets a single GP32 palette entry */
void GpSetPaletteEntry ( u8 i, u8 r, u8 g, u8 b )
{
    GP_PALETTEENTRY entry = GP_RGB24(r,g,b);
    GpPaletteEntryChange ( i, 1, &entry, 0 );
}

void GpMain (void * arg)
{
    int * t;
	
    GpFatInit();
	
    /* Initialize graphics */
    GpGraphicModeSet(8, t);
    GpLcdSurfaceGet(&gpDraw[0], 0);
    GpLcdSurfaceGet(&gpDraw[1], 1);
    GpSurfaceSet(&gpDraw[0]);
    GpLcdEnable();

    GpSurfaceFlip(&gpDraw[0]);
    GpSetPaletteEntry ( 0, 0,0,0 );
    GpSetPaletteEntry ( 1, 255,0,0 );
    GpSetPaletteEntry ( 2, 255,255,255 );
	
    PathInfo* info;
	
    short count = PrintFileInfo ("gp:\\game", info);

     for (int i = 0; i < (int) count; i++) 
    {
	
      GpTextOut (NULL, &gpDraw[0], 20, i * 10, info[i].name, 0);
	
     }

}

short PrintFileInfo (char* path, PathInfo* info)
{

  	GpRelativePathSet(path);
	
	ERR_CODE error;
ulong list_count = 0;

    error = GpDirEnumNum ("\\", &list_count);
	
	if (error != SM_OK)
	{
	
     return -1;
	
	}
	
	if (!list_count)
	{
	
  return -2;
	
	}
	
	GPDIRENTRY* filenames = new GPDIRENTRY[(int) list_count];
	
	ulong count;
	
	GpDirEnumList ("\\", 0, list_count, filenames, &count);
	
	GPFILEATTR attr;
	
	info = new PathInfo[(int) count];
	
	for (int i = 0; i < (int) count; i++)
	{
	
     error = GpFileAttr (filenames[i].name, &attr);
  
  info[i].directory = false;
  
  if (error == SM_OK && (attr.attr & 0x10) != 0)
  {

            info[i].directory = true;
  
  }
  
  gm_strcpy (info[i].name, filenames[i].name);
	
	}
	
	return (short) count;

}

Any help appreciated ;)
 
Arne posted on Oct 26 2004 at 08:04 PM said:
short PrintFileInfo (char*, PathInfo*);

Methinks you misunderstand pointer arguments. :)

In this function declaration, you are passing a pointer to a PathInfo struct, you are not passing the variable that contains the pointer (which would be called passing by reference).

The quick fix is to do:

Code:
short PrintFileInfo (char *str, PathInfo &info);

Then anything you do to info inside the function will affect the variable passed to the function. However this is messy and not good coding practice.

Far better to do:

Code:
struct PathInfo *PrintFileInfo (char *str);

struct PathInfo *info;

info = PrintFileInfo("gp:\\game");

struct PathInfo *PrintFileInfo (char *str)
{
struct PathInfo *info;

info = new struct PathInfo[10];

return info;
}
 
Last edited by a moderator:
RobertJ posted on Oct 27 2004 at 01:24 PM said:
Code:
short PrintFileInfo (char *str, PathInfo &info);
Then anything you do to info inside the function will affect the variable passed to the function. However this is messy and not good coding practice.
depends.. passing references around helps you ensure ownership (there's exactly one non-refering variable, and the owner of that variable is the owner of that data structure) - with pointers it's unfortunately pretty common to create an object (datastructure, whatever you want to call it) in one place, then destroy it in some other class (subsystem, ...)

it takes more discipline and design to work entirely with stack objects (vs. heap objects), and - depending on architecture - they might be slower or otherwise restricted. they are _not_ generally a bad idea though.
 
Last edited by a moderator:
Ok first of all thanks for your reply, but my code is not different from yours:

Im passing a NULL Pointer with the type PathInfo to PrintFileInfo which allocates an array of structs. If you declare a variable like

foo* bar;

no memory is allocated for foo but 4 a 4 byte pointer.

It seems to me like the memory alloc'd in PrintFileInfo for "info" (which shall gather Directory not File Infos ... ;) ) is getting free'd when it returns ...
 
Back
Top