473,567 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why to Allocate Memory when pointer in pointing to Datatype which has fixed length

6 New Member
When you are creating pointers which are only pointing to specific memory Location . For Eg when we create a Pointer based on int then int has a fixed memory allocated by default then what is the need to allocate using malloc()
for a variable based on "INT" type.
Mar 7 '07 #1
5 1950
9 New Member
your problem is really genuine one.
in simple case we do not have to use malloc( ) or "new" (c++ counterpart).
but for understanding the use of malloc( ) you must be aware of scope concept.
i suppose you are aware of it.
consider if we define a object in function it goes out of scope after returning from function.
if we wish to use that object after returning from function we have to allocate memory to it from free store ( using malloc( ) ).
then this object will be in scope after returning from function untill we destroy it explicitly (using destroy in c++).
for more detail refer chapter 6 bjarne stroustrup c++ programming language.
Mar 7 '07 #2
V Sudarshan
6 New Member
Can you be more Specific How it loses scope as you are not releasing the memory physically when a memory Location is allocated to that Variable , Do you mean the possibility of using the object again in another scope where it needs to have a separate Memory Area as it cannot reference to the Memory already allocated by the existing object due to abstraction.
Mar 7 '07 #3
V Sudarshan
6 New Member
Further to the Discussion let me know on what conditions you need to allocate Memory Physically dynamically using malloc(), is it only to meet the Dynamics of Pointers to release and reallocate Memory or use multiple Memory Allocations for Multiple Objects of Same instance, This has possible Memory Overloads and how should one be prepared to take care of Memory Resources which is not used . Further how to get the most LRU so that area of the memory can be deallocated and be allocated for some other useful Resource??
Mar 7 '07 #4
1,806 Top Contributor
Essentially, when you use a variable, it is placed on the stack. When whatever method created the variable finishes, it gets popped off the stack (along with any variables required by that method).

When you call malloc, you are saying "set me aside this much MEMORY (not on the stack). If memopry is available, you are returned a pointer to some part of that memory.....Sinc e this is basically a pointer to a physical location, you can use it from anywhere you pass the pointer (until you call free());

I'm not really sure what the question is about the int, basically you can choose what the data pointed to is interpreted as (which is particularly useful, if you plan to proceed through the mallocated (?) data).
If you call
int *i;
i = (int*)malloc(50 );

you have (provided there was memory available <if you test i is non-NULL you'll know it's all good>) been allocated enough memory to fill 50 int's. And if you say :

i++; //Move the pointer along to the next value

i will be moved one INT distance to where the next value should begin.....

Hope that diodn't get tioo confusing
Mar 7 '07 #5
9 New Member
ok let us talk about scope.
consider a function f().
execution start with main.
suppose there is a call to f() in main.
control goes to f().
at this point system allocate memory to object defined in f() plus make private copy of argument passed to f().
as soon as f() finish last statement all object defined in f() goes out of scope. system get memory back from f().
the way f() can modify objects of main is
in addition to it f() can return a single value of any data type which can assign to any object of main.
now if you want to use any object defined in f() in main you must allocate memory using malloc() to that object.

a example is implementation of link list in c.
we can define a function that create nodes on requirement and set values and we want to retain those nodes in main so we use malloc().(examp le from yashvant kanitkar
datastructure using c).
Mar 7 '07 #6

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

Similar topics

by: OlgaM | last post by:
Hello, i'm trying to initialize an array. The class List contains this in its private data members: ListNode<DATATYPE> *dataItems; In the constructor, i'm trying to allocate space for this array.
by: Patchwork | last post by:
Hi Everyone, I have a design-/implementation-related question for you all. In practical usage of C++, especially when processing image or audio data with 3rd party libraries, there often arises the situation in which a memory location has to be interpreted as a collection of values of a specific data type. For example, imagine an audio...
by: xuatla | last post by:
I encountered "segmentation fault" and I checked my code, found the following problem: I want to reallocate memory for an array. I defined the following function: int reallocateMemory( double *array, int newsize ) { if (array) delete array;
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
by: Peter Hickman | last post by:
I have a program that requires x strings all of y length. x will be in the range of 100-10000 whereas the strings will all be < 200 each. This does not need to be grown once it has been created. Should I allocate x strings of y length or should I allocate a single string x * y long? Which would be more efficient and / or portable? Thank...
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to...
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for...
by: Mahendra | last post by:
I have two cases - 1. When I have a pointer A pointing to a heap memory - What happens when I dereference the pointer A using free(A). It deallocated the heap memory the pointer was pointing to. Is that correct ? 2. If I have a pointer A pointing to a heap memory. Now I create another pointer B which points to same heap memory as...
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.