473,473 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dy Memory allocation

3 New Member
(1) How to correct the below dynamic mem allocation of 4 integer pointers

int *p[4];
p = (int* [])malloc(4*sizeof(int*));



(2)
int x = 10, y = 30, z=40;
p[0] = &x;

Quest:
Pointer p[0] (in heap memory) contains the address of x which is allocated in the data memory/segment. So is it possible to hold data memory/segment address in a heap memory pointer.
Sep 30 '06 #1
4 1837
D_C
293 Contributor
1. Malloc allocates a void pointer, and you have to cast it. You are allocating memory for 4 data of type (int*), yet casting it to type (int* []). It would make sense if those two data types agree, would it not?

That being said, int* and int[] cause the same code to be generated for parameter passing. Likely, it's the case that int* and int[] are equivalent data types. Therefore, cast it to int pointer or int array, but not both. Since you already account for the four entries in the array, 4*sizeof(int*), you had better allocate memory for the integer pointers then.

int* p[4] = (int*)malloc(4*sizeof(int*));

2. All pointers are stored in the heap. An int pointer can point to an int whether it is stored on the stack, the heap, or data memory.
Sep 30 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
1. Malloc allocates a void pointer, and you have to cast it. You are allocating memory for 4 data of type (int*), yet casting it to type (int* []). It would make sense if those two data types agree, would it not?

That being said, int* and int[] cause the same code to be generated for parameter passing. Likely, it's the case that int* and int[] are equivalent data types. Therefore, cast it to int pointer or int array, but not both. Since you already account for the four entries in the array, 4*sizeof(int*), you had better allocate memory for the integer pointers then.

int* p[4] = (int*)malloc(4*sizeof(int*));

2. All pointers are stored in the heap. An int pointer can point to an int whether it is stored on the stack, the heap, or data memory.
1. Sorry but not quite right for the variable

int *p[4];

then p[0] has type int * and p takes the type for of a pointer to the type of 1 of it's entries.

i.e. in int s[4]; s[0] has type int, s has type int *

so p has type int **

There for the allocation should be

int *p[4];
p = (int **)malloc(4*sizeof(int*));

or you will get a warning for error on different pointer types.

2. Again not quite right but in your defense right on a large majority of platforms. I recently worked on an embedded platform where you had to specify if your pointers where pointing a RAM (stack, heap, data segment) or ROM (consts). An given pointer could only point at 1 of these 2 types.
Sep 30 '06 #3
bruce prasad
3 New Member
As per your advice for the creation of dynamic allocation of an array of 4 int pointers as follows

int *p[4];
p = (int **)malloc(4*sizeof(int*));
I am getting the following error "Modifiable lvalue required for assignment operator" Please correct me.
Oct 10 '06 #4
D_C
293 Contributor
Actually, after testing it on my computer, it wouldn't let me mix array and pointer notation, it said ANSI forbids it. I had to change it to
Expand|Select|Wrap|Line Numbers
  1. int** p;
for the other line to compile. I should mention that each compiler is different. Some compilers are more open-minded than more strict ones.
Oct 10 '06 #5

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

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
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...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
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
tracyyun
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.