OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:57 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Some help on application dev
PostPosted: Sun Aug 23, 2020 1:08 pm 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
I'm writing an application with needs a dynamic, linked list of medium-sized [roughly 256 or 264 bytes] entries and a few rather big memory blocks [of varying size upto 4K each].
I'm writing in C on Linux.

Should I use malloc()? Or is there in C a way to allocate variables dynamically (like in C++)?

I thought of a linked list containing entries with this struct:
Code:
struct i_file{
void *next;
char filename[256];
};


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Sun Aug 23, 2020 1:55 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Use malloc(), and also define your link pointer as "struct i_file *next" instead of "void *next".

Note that malloc() does not initialize the allocated memory's contents. Depending on what you're going to put in that memory, you might want to use calloc() instead.

(Or just use C++.)


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Sun Aug 23, 2020 3:07 pm 
Offline
Member
Member

Joined: Thu Apr 30, 2020 6:03 am
Posts: 55
Talking about malloc, free, ...

Actually these functions are nothing more than calls to get memory from the heap, it is an area
that is assigned from the kernel itself to the application generally after the bss,
it is not even initialized, that is, when it is used, a failure occurs
of page, the kernel assigns it a physical page and that's when this memory really exists
physically.
I give you an example in "C" they are usually called malloc, free, ... and they are in the stdlib.h library, this
it is a way of maintaining the standard. Already in c ++ the new and delete operators are used.

I give you an example from the beginning:

In c you can define it in the stdio.h library or wherever you decide, the only important thing here is to know
the kernel call that returns the start of the heap, is usually called with sbrk () as standard
and you can implement your own malloc, free or whatever name you decide.


there is a place where you define these functions to be used in c ++
Code:
// For new operator example int * val = new int;
void * operator new (u32 len) {
    malloc (len);
}

// For new operator example int * val = new int [10]
void * operator new [] (u32 len) {
    return :: operator new (len);
}

// for delete (val) operators

void operator delete (void * ptr) {
    free (ptr);
}

// for delete operators (val [5])
void operator delete [] (void * ptr) {
    :: operator delete (ptr);
}

I give you an example when you create for example in c ++
int * new = new int [20]
actually c ++ ends up calling malloc

If you need a deeper detail I can help you, I really had to do all the
implementations to implement the libraries my kernel uses.

I hope these words serve you and help you. Regards


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 2:54 am 
Offline
Member
Member

Joined: Thu Jul 03, 2014 5:18 am
Posts: 84
Location: The Netherlands
Quote:
Should I use malloc()?

Yes.

Quote:
Or is there in C a way to allocate variables dynamically (like in C++)?

malloc is the way to allocate memory in C.

_________________
My blog: http://www.rivencove.com/


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 6:24 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
dseller wrote:
malloc is the way to allocate memory in C.
Yes. Except when you only need to allocate local memory, then you can also use alloca. The difference is,
- malloc allocates memory in the dynbss section, so you can return the allocated buffer to the caller, while
- alloca just decreases the stack pointer, thus being much faster O(1), but the buffer gets lost when the function returns to the caller.

This also means that malloc buffers has to be explicitly freed, while alloca buffers are automatically freed in the function epilogue.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 7:35 am 
Offline
Member
Member

Joined: Thu Apr 30, 2020 6:03 am
Posts: 55
Yes you can use malloc, always remember to free the memory when you are not using it to avoid an increase in the consumption of RAM memory by your application.


Top
 Profile  
 
 Post subject: [SOLVED] Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 9:14 am 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
Thanks to all of you. With your help I managed to solve my issue.

And the fact that the memory has to be freed, is indeed an inportant information. (I did it anyway but I thought it was just good style and not neccessary!)

Greetings
Peter


Top
 Profile  
 
 Post subject: Re: [SOLVED] Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 11:32 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 401
PeterX wrote:
Thanks to all of you. With your help I managed to solve my issue.

And the fact that the memory has to be freed, is indeed an inportant information. (I did it anyway but I thought it was just good style and not neccessary!)

Greetings
Peter


Or using GC:

https://en.wikipedia.org/wiki/Boehm_garbage_collector

To anyone that scoffs at the idea of GC in C, if you can do the GC at times you're otherwise idle, you can potentially improve performance (as you're not longer incurring a cost for free()), as well as making your code safer.


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 11:35 am 
Offline
Member
Member

Joined: Thu Apr 30, 2020 6:03 am
Posts: 55
Let me tell you something else, really when you free up memory it does not mean that the occupied RAM decreases, but when you request memory again this freed area can be used by your application without the need to increase the heap. In other words, the heap cannot be reduced, but expanded. When your application is terminated by the kernel then everything is released.
Regards


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 2:28 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
mkfree wrote:
really when you free up memory it does not mean that the occupied RAM decreases,

That depends on the heap allocator implementation. If you have a system call like mmap(), it's entirely possible for your allocator to release freed memory back to the OS. Whether it's a good idea to do that is another matter: you probably want to avoid releasing memory that's just going to be allocated again.


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 3:32 pm 
Offline
Member
Member

Joined: Thu Apr 30, 2020 6:03 am
Posts: 55
Octocontrabass,
100% agree.


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 3:59 pm 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
Octocontrabass wrote:
mkfree wrote:
really when you free up memory it does not mean that the occupied RAM decreases,

That depends on the heap allocator implementation. If you have a system call like mmap(), it's entirely possible for your allocator to release freed memory back to the OS. Whether it's a good idea to do that is another matter: you probably want to avoid releasing memory that's just going to be allocated again.

In the case of my application the memory is not freed and then allocated again.
I don't understand yet what mmap() does. I guess it is because I still don't understand paging fully. (Okay, I have some idea about paging, but I don't comprehend it in detail.)

And does mmap() exist on every generic OS? Is it part of the C standard library?


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 4:08 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
mmap is not in the standard C library. I believe it is a part of POSIX. It is a system call, and allows you to map files and memory, can figure out a virtual address to allocate or have you specify one, it can set readable and writeable attributes, plus more. It is a very good function, but it allocates in pages. On x86, that means 4K blocks of memory. It is generally better to use malloc, and when you need granular control over the memory or when mapping a file or when you know the virtual address you want to use, then mmap is your friend. Just note that is not portable. I believe Windows uses VirtualAlloc or MapViewOfFile instead.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 4:12 pm 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
nexos wrote:
mmap is not in the standard C library. I believe it is a part of POSIX. It is a system call, and allows you to map files and memory, can figure out a virtual address to allocate or have you specify one, it can set readable and writeable attributes, plus more. It is a very good function, but it allocates in pages. On x86, that means 4K blocks of memory. It is generally better to use malloc, and when you need granular control over the memory or when mapping a file or when you know the virtual address you want to use, then mmap is your friend. Just note that is not portable. I believe Windows uses VirtualAlloc or MapViewOfFile instead.

Well, it's bad that it is not portable to Windows. But it sounds cool.
Does it mean, I can map a file to memory without reading the bytes in one-by-one? That would come very handy for my project.


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Mon Aug 24, 2020 4:41 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Yes, you can! It uses demand paging, so it reads the file data on demand to save memory. Info about demand paging can be found at https://en.wikipedia.org/wiki/Demand_paging.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 31 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group