![]() |
|
#1
|
|||
|
|||
Giving garbage value as the result of productI am new to pointers. I am trying to perform this multiplication, but this is giving garbage value. Can anyone please tell me my flaws. My code is posted below
CPP / C++ / C Code:
I am new to pointers, so please explain my fault if possible. Thanks in advance. Last edited by admin : 17-Jul-2008 at 09:35.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
Several comments:
Consider the following simplified allocation of a two-dimensional array in two different ways: CPP / C++ / C Code:
Lastly, don't underestimate the value of formating. It can help you keep life straight, & given that most programming in the corporate world is collaborative, it makes life easier for the next person who has to figure out what you meant. |
|
#3
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
First of all, you simply must get familiar with the feature of the language that connects pointer notation with array notation. One fundamental and unbelievably important notational definition is the following: If a is a pointer data type and if b is a integer data type then the notation Code:
Code:
Not "nearly the same as" or "just about the same as," but exactly the same as. You can use the two notations interchangeably. Note that this is not (that's not) saying that pointers are the same as arrays; this is just notation. Suppose that the variable x is a "pointer to pointer" data type. Then *x is a pointer data type. Suppose y is an integer data type, then, from the previous definition, the notation Code:
Code:
Note that: if x is a pointer-to-pointer, then *x is a pointer. Then an application of the array-pointer notational equivalence says that if i and j are integer data types and x is a pointer-to-pointer data type, the notation Code:
Code:
Now, for people familiar with matrix notation from math class, I think they are more likely to appreciate matrix notation in C, although pointer notation and mixed array-pointer notation that you used is perfectly valid. Your program declares pointers-to-pointers and allocates storage so that they can be used to store matrix elements. I would use matrix notation for the manipulations. Try writing your transpose loop with something like CPP / C++ / C Code:
CPP / C++ / C Code:
Now, try it with the product calculation. Here's the general approach. You customize it for your application: Suppose you have an m x q matrix, x and a q x n matrix y Then the product is an m x n matrix. Now, let's illustrate using matrix notation in a loop to do matrix multiplication in C. Suppose we want to multiply matrix x by matrix y. I'll call x the "left matrix" and y will be the "right matrix." The product will be named z I use the following variables: 1. Let lrows be the number of rows if the left matrix. 2. Let lcols be the number of columns in the left matrix. This must be equal to rows in the right matrix, otherwise the matrix product is not defined. 3. Let rcols be the number of columns in the right matrix. (I like to let the variable names be somewhat descriptive rather than using names like m, n, etc., so that I won't forget what each actually means.) Also, when making loops over rows and columns, I usually use i and j, since in math class, they typically talk about M(i,j) for the matrix element on the ith row and the jth column. Suppose you have already declared variables and allocated storage as required. Then, here's the code to multiply the matrices: CPP / C++ / C Code:
Regards, Dave Footnote: I hate to repeat myself, but I am not saying that matrices and pointers are interchangeablle. They are not. Never have been; never will be. The notation, however is interchangeable. Absolutely. Completely. Unequivocally. The notation x[i][j] can be used to access an element of a declared array or an element of a dynamically allocated array (using pointers-to-pointers). The code created by the compiler to access the elements of each kind of "array" is completely different for the two cases. If you write a function that works with "pointer-to-pointer" arguments, you must feed it an argument that is a "pointer-to-pointer," not the name of an array. If you write a function that takes the name of 2-D array as an argument, you can't feed it a "pointer-to-pointer." Notation inside either function can use pointer notation or array notation or mixed notation. The source code can look the same, but the code generated by the compiler is different. |
|
#4
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
It's gotta be CPP / C++ / C Code:
Code:
Try the following as your first loop: CPP / C++ / C Code:
Output: Code:
(Now change "ROWS" to "COLUMNS" in the index calculation expression and try it.) With i = 0 (the first conceptual "row") the first elements go from a[0] through a[COLUMNS-1] Then, with i = 1 (the next conceptual "row") the elements go from a[COLUMNS] through a[2*COLUMNS=1] Etc. So, we can see that the (i,j) element would be addressed by a[i*COLUMNS + j] Of course, this is how C lays out a 2-D declared array in consecutive memory locations, and is useful in some applications using a pointer to an allocated block. There may be some advantages, in certain applications, to the single-block method of your first example, but the disadvantage is that it's not always obvious when even experienced programmers absent-mindedly get it wrong. It's that kind of bug that isn't always obvious "at a glance." This points out the advantage of letting the compiler do the work by using 2-D array notation for "pointer-to-pointer" dynamically allocated arrays as you show in your second example. The Original Poster did the work of allocating the 2-D arrays but didn't glom onto the convenience of the 2-D array notation that you showed. Regards, Dave Footnote: The fact that the C compiler calculates the address of the (i,j) element of a 2-D declared array by the formula i*COLUMNS+j means that for functions with parameters that are declared arrays, you have to tell it the size of the second dimension but not the first when you define the function. In C, the compiler has to know how to access the elements at compile time, not run time. |
|
#5
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
The point is that the heapspace is being utilized row-major. Quote:
Quote:
CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
I am declaring the variable as pointer to pointer but using it in form of mixed array pointer notation. Will the compiler then behave differently? |
|
#7
|
|||
|
|||
Re: Giving garbage value as the result of product@ Ocicat
You said to free the allocation of heap. There comes question in my mind,that.. Suppose in case of my program,i Would want to free the space used by **dm and **dm1,then should i do it after the multiplication is over???Otherwise,before execution I will lose the address location. One more question,Does use of macros in case of large programs(say variable numbering 1000) makes the program slow???If it does what is the other way to input the variable dyanamically.I dont want a static allocation.Is user input the second option? Thanks in advance Winner |
|
#8
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
Quote:
On the platform I am using, sizeof(double) shows that a double consumes 8 bytes. Therefore, a 1000 * 1000 matrix means there are 1,000,000 matrix elements allocated from the heap. At 8 bytes per element, you are asking the program to allocate 8 * 1,000,000 / (1024 * 1024) = greater than 7.5 MB just for this matrix alone. Not knowing either what operating system you are using, how much RAM your system has installed, or what other applications are running at the moment, as you push the size of the matrix to extremes the operating system will have to begin paging memory contents to disk at some point. Yes, you will see performance degrade by allocating significant quantities of memory. |
|
#9
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
Here's the deal: You write the code. The compiler does whatever it has to in order to access the memory. Regards, Dave Footnote: The source code in the loops in my example would look the same if any (or all) of the variables x, y, and z had been declared as a 2-D array, but the compiler would treat it differently. The source code notation for accessing elements of declared arrays is the same as the source code notation for dynamically allocated "arrays." The compiler knows what to do. You simply have to believe in the equivalence of the notations that I showed. Last edited by davekw7x : 18-Jul-2008 at 08:59.
|
|
#10
|
|||
|
|||
Re: Giving garbage value as the result of productQuote:
Your code essentially does the following: CPP / C++ / C Code:
Later in your code, you specify in effect: CPP / C++ / C Code:
Last edited by ocicat : 18-Jul-2008 at 11:53.
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to choose an ecommerce product? | ramukumar | eCommerce / Merchant Account Forum | 1 | 12-Jun-2008 07:14 |
| Memory de-allocation during debugging | gaoanyu | C Programming Language | 12 | 19-Dec-2005 04:50 |
| Hex Result giving strange answers. | Rosdahale | C Programming Language | 6 | 07-Dec-2004 20:28 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 07:10 |
| [Review] SalesCart Pro | BobbyDouglas | Web Design Forum | 0 | 11-Mar-2004 12:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The