473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Is memory allocated on stack or on heap?

Hi,
Is there any way to determine at run-time if block of memory allocated on stack or on heap? For example:

Expand|Select|Wrap|Line Numbers
  1. BYTE* pData = new BYTE[100];
  2. BYTE data[100];
  3. BYTE* pData1 = &data[0];
  4.  
  5. //Hot to determine that pData is on heap, and pData1 is 
  6. //on stack?
  7.  
UPDATE: As I understand there is no generic solution for this one, so I need solution for Windows platform. I know that there is _CrtIsValidHeapPointer (debug version only). Is there other ways how to do it?


Thank you!
Mar 8 '11 #1
6 3136
Banfa
9,065 Expert Mod 8TB
You should not rely on being able to tell how a piece of memory is allocated. If it matters then you should create a way to pass that information round your system, however it would be better to design the system so that you do not need to have to know this by either always allocating on the heap or always allocating on the stack.
Mar 9 '11 #2
Thanks Banfa,
Sure, it's not something that should be used in the system design, I was just curious, because recently saw C++ implementation of DataBuffer (something like this one: DataBuffer), and creators were trying to detect if attached block of memory is on heap or on stack, so they would know how to handle this memory further.
Anyhow, thank you very much for answering my question!
Mar 9 '11 #3
Banfa
9,065 Expert Mod 8TB
A DataBuffer class should manage the buffer memory itself. Since it should allocate the memory it knows how to deallocate it.
Mar 10 '11 #4
Yep, it it has functionality to attach existing data store, it mean that memory was already allocated before, and you don't know if you can do realloc on this memory, or not
Mar 10 '11 #5
Banfa
9,065 Expert Mod 8TB
Well for strict safety the memory buffer should allocate its own buffer and copy the data passed to it into its own buffer. The buffer passed should remain owned by the caller and be unchanged.

However there are times when you want to avoid that sort of copying, for example in an application that is solely responsible for transmitting data from a source to a destination repeatedly copying buffers can result in a serious reduction in through-put.

However a DataBuffer class should never be allowed to take ownership of a stack buffer because it can't properly do that. The ownership remains with the function that has the buffer on its stack because it will deallocate the buffer when the function exits.

What the class could have though to prevent multiple copying is a method to transfer ownership of a buffer allocated on the heap to it, you might call it something like takeOwnershipOfHeapBuffer to make it clear that once called the class has ownership and that it should only be called for a buffer allocated from the heap.

But the need for something like this should be determined by there being an actual need for it rather than by assuming it is required because of the risk of incorrect calling it introduces. That is, if possible, the DataBuffer class should be built using copying, then once the application is running if the performance is low the application should be profiled and then if then problem is with the DataBuffer classes copying something should be done to reduce the amount of copying done.

You should build your classes for safety and then only compromise that safety if the application is not meeting its performance criteria.
Mar 10 '11 #6
Banfa, thank you very much!
Great answer!
Mar 10 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Anders K. Jacobsen [DK] | last post by:
Hey. Is it possible somehow to persist the call stack, heap, program data...everything. Then at a later time load it again and continue work. The idea is actually to send it all over network in...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
16
by: Ankit Raizada | last post by:
Is there a way to know how much memory has being allocated(dynamically) to a pointer. Related to this, can we find out has a pointer already been freed? I am using VC++
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
2
by: tristan.chaplin | last post by:
I have an unmanaged dll that returns a pointer (that it allocates, I didn't allocate it with Marshal.AllocHGlobal), how do I free the memory when I'm finished with it? It doesn't seem to like me...
2
by: nguyenlh | last post by:
I'm studying about Memory managerment - stack memory and I can't understand the following problems : Who can help me >> - Role of Stack memory -How to store address in stack memory -Garbage...
13
by: pratap | last post by:
how could i find out how much memory is blocked(or has been allocated to a pointer) consider, int *p=new int; or int *p=new int; suppose i dont know the right hand side of the statement...
2
by: rathankar | last post by:
dear friends thanks for the mail sent today i have one more question: 1. since in perl arrays need not be declared, , what is the maximum limit of elements an array could have? 2. in java/ c/...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
6
by: Praetorian | last post by:
This is actually 2 questions: The first one: I have a function (FuncA) calling another (FuncB) with a set of parameters which also includes an int * initialized to NULL before the call. Now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.