English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 14 January 2021, 00:06   #1
leemmcc
Registered User

 
Join Date: Nov 2020
Location: Barnsley
Posts: 8
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!
leemmcc is offline  
Old 14 January 2021, 00:11   #2
jotd
This cat is no more
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 49
Posts: 5,089
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)
jotd is online now  
Old 14 January 2021, 00:15   #3
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,183
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);
thomas is offline  
Old 14 January 2021, 23:24   #4
leemmcc
Registered User

 
Join Date: Nov 2020
Location: Barnsley
Posts: 8
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...

Thanks again jotd and thomas.

Last edited by leemmcc; 15 January 2021 at 00:02.
leemmcc is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why printf in hook function freezes everything? Sim085 Coders. C/C++ 5 29 June 2020 20:39
How to create a 'Array of structs' in AMOS? thyslo Coders. AMOS 7 11 May 2020 13:18
Integers vs floats (FFP/Sing/Doub) + printf() guy lateur Coders. Asm / Hardware 63 18 July 2017 18:57

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 16:20.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.06096 seconds with 15 queries