473,326 Members | 2,111 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,326 software developers and data experts.

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

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 1945
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
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
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
DeMan
1,806 1GB
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.....Since 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
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
1.pointer
2.reference
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().(example 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

1
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...
2
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...
13
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...
30
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 =...
6
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....
10
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:...
9
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,...
6
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...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.