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

Trouble understanding "Double" Pointers.

I am a student and I am having trouble understanding Dynamic Memory Allocation and the usage of Double Pointers.

Expand|Select|Wrap|Line Numbers
  1. int **A
  2.  
  3. A=(int **)malloc(r1*(sizeof(int*)));
  4.  
  5. for(i=0;i<r1;i++)
  6. A[i]=(int *)malloc(sizeof(int)*c1)
  7.  
In the above snippet r1 and c1 refer to the number of rows and columns of a matrix A.

My question : What does the malloc function do here? I know malloc() is used to allocate memory during runtime and the argument(r1*(sizeof(int*)) is supposed to the size of the memory allocated. Does the argument mean r1 x size of int (ie) r1 x 2 (where 2 is the size of an integer in the memory)?

And what does the coding inside the for loop do? Why is each element being allocated new space again?
May 28 '10 #1
3 1841
Banfa
9,065 Expert Mod 8TB
In line 3 the r1*(sizeof(int*) is not r1 * the size of int. That is an int *, integer pointer not an int so it is r1 * the size of an integer pointer.

Line 3 allocates an array of pointers to integer.

The loop then allocates an array of c1 integers (c1 * sizeof(int))) to each one of the pointers allowing you yo use the point A in a similar fashion to a double indexed array.

However there are some differences, memory footprint is one. An actual double indexed array has all its memory in a contiguous block, this is not true for this method of allocation using malloc because each of the arrays in the loop is allocated separately and you then have no guarantee that they will be contiguous.
May 28 '10 #2
That helps! Thank You!
May 28 '10 #3
donbock
2,426 Expert 2GB
Take a look at Arrays Revealed.

You should always check the value returned by malloc to confirm it is not NULL. That is, to make sure malloc succeeded.
May 28 '10 #4

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

Similar topics

2
by: Arnaud | last post by:
Hi, I would like to protect myself against these two PHPSESSID I have sometimes in the url... I'm coding a session-based script which will be upload on a shared server. I can't control if this...
2
by: Boštjan Jerko | last post by:
Hello! I have a list of lists (e.g. ,,]) and would like to do index on the first element of the lists. Can it be done? B.
2
by: Uma Abhyankar | last post by:
Hello, We are facing precision issues with addition, multiplication or division of "double" Issue1: ##### The output of 0.1 + 0.2 is not 0.3 It is 0.30000000000000004 How do we tackle this??
2
by: Sooha Park Lee | last post by:
I would like to compare two "double" numbers. To overcome round-off error, I am using the following strategy: rounding each number and try to convert it into interger by multiplying 10E6. One...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
6
jasjas
by: jasjas | last post by:
my Q is like this, i want to design a calculation which use the number user enter to "/" by 100. so , offcouse the result mayb in double or integer. after that my calculation just wanna user...
4
by: vinnie | last post by:
When i try to populate a dropdownlist menu, using a for cicle works perfect: i get a list of int from ie 1 to 10. How an populate my ddlist if working on the code i wish to get not longer a series...
19
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
16
by: linlinfan320 | last post by:
why it print wrong result? I can't find the wrong place. #include<stdio.h> #include<math.h> double distance(double a ,double b,double c,double d); int main() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.