(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.
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.
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.
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.
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
for the other line to compile. I should mention that each compiler is different. Some compilers are more open-minded than more strict ones.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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:
|
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
| |
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.
|
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...
|
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:
|
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
|
by: karthikbalaguru |
last post by:
Hi,
Will 'free' return the memory Immediately to the OS ?
Thx in advans,
Karthik Balaguru
|
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...
| |
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: 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: 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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |