English Amiga Board

English Amiga Board (http://eab.abime.net/index.php)
-   Coders. C/C++ (http://eab.abime.net/forumdisplay.php?f=118)
-   -   Printf, STRPTR, and structs (http://eab.abime.net/showthread.php?t=105384)

leemmcc 13 January 2021 23:06

Printf, STRPTR, and structs
 
He all, I've been a developer for years but first time using c in Amiga 68k. I'm using gcc.

I've got a struct and an pointer array of said struct.

Code:

#define MAXFILECOUNT 255

struct File {
        STRPTR fileName;
        LONG size;
};

struct File *files[MAXFILECOUNT];

And I've got a function to read a folder of files.

Code:

int clear(struct FileInfoBlock *fib, BPTR lock)
{
    if (fib != NULL){ FreeDosObject(DOS_FIB, fib); }
    if (lock != NULL){ UnLock(lock); }

    return RETURN_ERROR;
}

int setFiles(STRPTR path)
{
    struct File *fl;

    struct FileInfoBlock *fib = NULL;
    BPTR lock = Lock(path, ACCESS_READ);

        int a = 0;

    /**
    * Ensure no process already has a lock on the path
    */
    if (lock)
    {

        /**
        * Create instance of struct FileInfoBlock
        */
        fib = (struct FileInfoBlock *)AllocDosObject(DOS_FIB, NULL);
        if (!fib)
        {
            Printf("Unable to allocate file information block.\n");
            return clear(fib, lock);
        }
        else
        {

            /**
            * Ensure we can examine the path
            */
            if (Examine(lock, fib))
            {

                /**
                * Get each file in path, ignoring other directories
                */
                int idx = 0;
                while (ExNext(lock, fib))
                {
                    if (!(fib->fib_DirEntryType > 0))
                    {
                                                Printf("%20s\n", fib->fib_FileName);

                        fl = (struct File*) malloc(sizeof(struct File));
                                                fl->fileName = (STRPTR)fib->fib_FileName;
                                                fl->size = fib->fib_Size;

                        files[idx] = (struct File*)fl;
                        idx++;
                    }
                }

                clear(fib, lock);

            }
            else
            {
                Printf("Unable to examine the specified path.\n");
                return clear(fib, lock);
            }
           
        }
    }
    else
    {
        Printf("Unable to acquire a read lock to the specified path.\n");
        return clear(fib, lock);
    }

       
        // Temp code
        for (a = 0; a < MAXFILECOUNT; a++)
        {
                if (files[a]->size > 0){
                        Printf("Filename %s\n", files[a]->fileName);
                        Printf("Filesize %ld\n", files[a]->size);
                }
                else
                {
                        break;
                }
        }

    return RETURN_OK;
}

For each file I want to create a pointer of struct File and add it to the files array.

I get no complaint from the compiler whatsoever BUT when I Printf the added files (in the Temp code at the bottom of the listing), the printed Filesize is correct but Filename is corrupted.

Clearly I'm doing something wrong here. Any advice would be helpful.

Thanks!

jotd 13 January 2021 23:11

Code:

fl->fileName = (STRPTR)fib->fib_FileName;
that copies the pointer, which may be used for something else in the meantime. You have to duplicate the string.

change your structure so fileName is a char fileName[108] then you can strcpy(fl->fileName,(STRPTR)fib->fib_FileName)

or malloc the exact string size (+1) and same string copy (needs deallocation in the end)

thomas 13 January 2021 23:15

The C language does not know strings. You cannot assign strings using the = operator in C.

A STRPTR is a pointer to an array of characters. By assigning fileName to fib_FileName you let the pointer point to a part of the memory you allocated using AllocDosObjects before. Later you free that memory so that fileName now points to memory which is no longer valid.

What you have to do is

fl->fileName = malloc(strlen(fib->fib_FileName)+1);
strcpy (fl->fileName,fib->fib_FileName);

leemmcc 14 January 2021 22:24

Of course that's what it is! Thanks so much.

This is what happens we you spend 20 years writing strongly typed languages with their own garbage collection and memory management... :D

Thanks again jotd and thomas.


All times are GMT +2. The time now is 00:39.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.

Page generated in 0.04439 seconds with 11 queries