473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ 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*sizeo f(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 1843
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*siz eof(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*siz eof(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
8184
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 allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new...
4
6758
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 = Linux galahad 2.4.19-64GB-SMP #1 SMP /etc/sysctl.conf kernel.shmmax=268435456
74
4597
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 seems to be setting a NULL pointer to the end of the list, and here we know that the allocated memory has been exhausted. All good. When this is...
62
17656
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
66
3564
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 (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
24
19041
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7949
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to...
34
2543
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? Contrary to some people that will start crying to that &@@""#~ programmer that wrote this sh!!!! you keep your cool and you do the following:
14
3813
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 this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
66
3648
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
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. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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...
0
7752
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 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...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
701
bsmnconsultancy
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.