Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Adding GetCount/NumBytes to Buffer?
Adding GetCount/NumBytes to Buffer? [message #61670] Sun, 04 May 2025 14:49 Go to next message
luoganda is currently offline  luoganda
Messages: 214
Registered: November 2016
Experienced Member
Since Buffer always has an info of NumItems allocated
and that NumItems(allocated) info is always needed by the app,
could be somthing like this added to Buffer?

Buffer buff;
...allocating it..
buff.GetCount() //<=sinceIt'sATemplate

and maybe
buff.NumBytes() //<=sinceIt'sNeededSometimes
Re: Adding GetCount/NumBytes to Buffer? [message #61672 is a reply to message #61670] Wed, 07 May 2025 15:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14260
Registered: November 2005
Ultimate Member
luoganda wrote on Sun, 04 May 2025 14:49
Since Buffer always has an info of NumItems allocated


Actually, it does not Smile

If you need GetCount, use Vector...

Mirek
Re: Adding GetCount/NumBytes to Buffer? [message #61679 is a reply to message #61670] Tue, 13 May 2025 13:23 Go to previous messageGo to next message
luoganda is currently offline  luoganda
Messages: 214
Registered: November 2016
Experienced Member
Updated:
Checked size of Vector<int>
and it's not that big compared to Buffer - i thought it's bigger, that's why proposal.
Down message is now there for info(eg also hint for UnifiedSizeOf in c/c++).
GetCount could still be added though(1stMessage).


First message version:
Quote:
Since Buffer always has an info of NumItems allocated

i mostly meant that it has because it knows where it allocated it(so it can query size on that) and/or also
when specifying either Buffer(size_t size) or Alloc(size_t size).
With that i mean - Buffer size can not be done with one of this two functions,
so because it has an unified alloc protocol - info is there and could be easily used for this,
since Buffer is smaller compared to Vector and more apropriate in some situations.
If Buffer is used elsewhere in func or down the code - it would be more apropriate
to have that NumBytes() to not make a messy code to pass extra numBytes to func or something else.

So, two solutions are here for adding GetCount:
1) without adding additional int num to Buffer and querying size dynamically(nonTrivial-butStillOk)
2) adding int num to Buffer and just set it whenever Buffer New is called


~~~~~~~
The second version of "Buffer always has an info of NumItems/NumBytes..."
i meant that it has an underlying info about it.

But then i reserched a little and found out that it has and it doesn't.
This should be there in c/c++(as an option since it could bloat compiler work - not end compiled code),
and would probably not be so hard to implement.

int anInt;
void *ptr=malloc(...)

//now, somewhere else in the code,
//how to check if it was allocated on a stack or on a heap?
//because when allocated on the stack one could simply do sizeof(anInt)
//and when on heap - one of heap size functions(in case of upp something else)
//I guess this could be a kind of Rtti info.


I meant if one had that info, NumBytes would be already there.
For now though - this is not so straightforward.

func(void*ptr){
  //unified sizeof
  bool stackBased=IsStackBased(ptr);
  bool heapBased=!stackBased;
  uint32 sz=UnifiedSizeOf(ptr);
  memset(ptr,0,sz);
}
I am not sure how this works,
when one allocates on the stack/heap - it probabaly has different segments of address space,
so it could be also known by that.

[Updated on: Tue, 13 May 2025 13:40]

Report message to a moderator

Re: Adding GetCount/NumBytes to Buffer? [message #61680 is a reply to message #61679] Tue, 13 May 2025 14:04 Go to previous message
mirek is currently offline  mirek
Messages: 14260
Registered: November 2005
Ultimate Member
FYI: U++ allocator does have means to return the size of allocated block, unfortunately that can be bigger than the size requested by MemoryAlloc, so GetCount implemented as GetMemoryBlockSize(ptr) / sizeof(T) would often return bigger than correct number. So that realistically leaves storing size as second member variable (+8 bytes), which so far was not worth it.
Previous Topic: Stable Sort algorithm
Next Topic: How do I override the Button class Tip handling function
Goto Forum:
  


Current Time: Sun Jun 01 03:20:10 CEST 2025

Total time taken to generate the page: 0.02341 seconds