473,378 Members | 1,527 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,378 software developers and data experts.

What's the difference between using static and dynamic arrays

Hi, I want to use 2D array in a dynamic-memory-allocation way, so I define the following in C

int row=3,column=2, i, j;
double A[3][2]={ {1,2}, {3,4}, {5,6} };
double** AD=(double **)malloc(sizeof(double *)*row);
double *ptr=(double *)malloc(sizeof(double)*row*column);

for(i=0;i<row;i++)
{
AD[i]=ptr+i*column;
for(j=0;j<column;j++)
AD[i][j]=A[i][j];
}

Then, I think, AD and A have identical element values, and the same memory address layout. But when I call a function from an IBM library, using A as the argument will give me correct result, but using AD as the argument will give wrong result. Could anyone tell me about the difference of A and AD? I mean, in which situation do I have to use the first one?

Thanks very much in advance.

Alex
Jul 31 '07 #1
4 7134
JosAH
11,448 Expert 8TB
A picture tells more than a thousand words. Your A and AD structures are by
no means identical:

Expand|Select|Wrap|Line Numbers
  1.  
  2. A: +---+---+          AD: +---+        +---+---+---+---+---+---+  
  3.    |   |   |              | --|------> |   |   |   |   |   |   |
  4.    +---+---+              +---+        +---+---+---+---+---+---+
  5.    |   |   |              | --|------------------^       ^
  6.    +---+---+              +---+                          |
  7.    |   |   |              | --|--------------------------+
  8.    +---+---+              +---+
  9.  
The matrix on the left can be seen as three rows; each row has two elements
(doubles in your case). Here is the definition of a single row:

Expand|Select|Wrap|Line Numbers
  1. typedef double (row_t)[2];
  2.  
So if you allocate three rows:

Expand|Select|Wrap|Line Numbers
  1. row_t* ad= calloc(sizeof(row_t), 3);
  2.  
your ad structure is identical to your (statically allocated) a stucture now; it now
points to three rows adjacent in memory.

kind regards,

Jos
Jul 31 '07 #2
thanks for you reply.

Your picture did a great job. Instead of passing AD to the function, I should pass (AD)[0] to it. And now it works. Thanks
Jul 31 '07 #3
JosAH
11,448 Expert 8TB
thanks for you reply.

Your picture did a great job. Instead of passing AD to the function, I should pass (AD)[0] to it. And now it works. Thanks
I was just about to reply to your previous incarnation of your reply but I noticed
you edited it. For a two dimensional arrray which consists of a single block of
memory either build a 'dope' vector like you did and pass the very first row element
around, i.e. you have made the row pointers explicit pointers or cast a bit here
and there and define a row type like I did. My trick doesn't need a 'dope' vector,
your trick doesn't need the new type.

kind regards,

Jos
Jul 31 '07 #4
Thanks a lot Jos. you solved my problem
Jul 31 '07 #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...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
40
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.