-
#include<stdio.h>
-
#include<stdlib.h>
-
-
-
-
-
int **transpose(int **x,int m,int n);
-
-
-
-
main()
-
{
-
int nrows=2,ncolumns=2,i,j,k=0;
-
-
-
//memory allocation for array x
-
int **array;
-
array = malloc(nrows * sizeof(int *));
-
if(array == NULL)
-
{
-
printf("out of memory\n");
-
return 0;
-
}
-
-
-
for(i = 0; i < nrows; i++)
-
{
-
array[i] = malloc(ncolumns * sizeof(int));
-
if(array[i] == NULL)
-
{
-
printf("out of memory\n");
-
return 0;
-
}
-
}
-
-
-
-
-
-
-
//define x
-
printf("x=\n");
-
for(i=0;i<2;i++)
-
{
-
printf("\n");
-
for(j=0;j<2;j++)
-
{
-
k=k+5;
-
array[i][j]=i+j+k;
-
printf("%d\t",array[i][j]);
-
}
-
}
-
printf("\n");
-
-
-
-
-
-
-
//memory allocation for x_transpose, (storing the transpose returned by function)
-
-
int **x_transpose;
-
x_transpose = malloc(nrows * sizeof(int *));
-
if(x_transpose == NULL)
-
{
-
printf("out of memory\n");
-
return 0;
-
}
-
-
-
for(i = 0; i < nrows; i++)
-
{
-
x_transpose[i] = malloc(ncolumns * sizeof(int));
-
if(x_transpose[i] == NULL)
-
{
-
printf("out of memory\n");
-
return 0;
-
}
-
}
-
-
-
-
-
-
-
-
//call function
-
x_transpose= transpose(array,nrows,ncolumns);
-
-
-
-
-
-
-
//display transpose
-
printf("x_transpose=\n");
-
for(i=0;i<2;i++)
-
{
-
printf("\n");
-
for(j=0;j<2;j++)
-
{
-
printf("%d\t",x_transpose[i][j]);
-
}
-
}
-
}
-
-
-
-
-
-
-
-
-
-
//function_transpose
-
-
int **transpose(int **x,int m,int n)
-
{
-
int nrows=n,ncolumns=m,i,j;
-
-
//memory allocation for y,to store transpose
-
int **y;
-
y = malloc(nrows * sizeof(int *));
-
if(y == NULL)
-
{
-
printf("out of memory\n");
-
return 0;
-
}
-
for(i = 0; i < nrows; i++)
-
{
-
y[i] = malloc(ncolumns * sizeof(int));
-
if(y[i] == NULL)
-
{
-
printf("out of memory\n");
-
return 0;
-
}
-
}
-
-
-
-
-
-
for(i=0;i<m;i++)
-
for(j=0;j<n;j++)
-
{
-
y[i][j]=x[j][i];
-
}
-
return y;
-
}
-
i allocated memory for storing y(transpose of matrix) inside the function. also i allocated memory(for x_transpose) for storing the same result in main program. can reuse the same memory in main also???
4 3515
YOu have what appears to be serious memory issues.
This code: - int **x_transpose;
-
x_transpose = malloc(nrows * sizeof(int *));
-
etc...
-
-
//call function
-
x_transpose= transpose(array,nrows,ncolumns);
It appears the address allocation of x_transpose is overwritten by the transpose function.
Also your allocated memory is never freed.
As far reuse goes any allocated memory is your to use as often as you wish until you free it. The real trick is keeping all of the addresses of your allocations in scope. If you lose an address you lose the allocation.
1.thanks.. that helps. so i need not have to allocate memory for x_transpose, and just need to declare as
????
2. i am afraid i did not get the sentence,
"As far reuse goes any allocated memory is your to use as often as you wish until you free it. The real trick is keeping all of the addresses of your allocations in scope. If you lose an address you lose the allocation. "
3. where should i free the memory allocated inside a function? is it at the end of the function or at the end of main program???
Answer to question 1:
Yes. That's correct. The only additional thing I would do is set the pointer to 0. By convention any non-zero pointer is assumed to contain a valid address. 0 is widely recognized as "no address inside", that is, a null pointer.
Answer to question 2:
When you use malloc, you receve a pointer to the memory allocated by malloc. If you lose that pointer you lose your allocation: - void Func()
-
{
-
-
int* ptr = malloc(sizeof(int) * 20);
-
}
When Func() returns ptr is deleted. The allocated memory is still there and it is still yours. Only now there is no way to access it. That allocation will stay there until the program completes and it permanently reduces the amount of free memory available. Hence this is called a memory leak--the free memory just drains away.
You must either return ptr or you must free the allocation while still inside Func().
Answer to question 3:
You free memory when your program no longer will access it. That may be inside the function or it may be in
main(). One of the problems in freeing allocated memory is knowing when to do it. There are some techniques that would take too much room to go into here. I suggest additional reading or Googleing.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ralph Krausse |
last post by:
How can I dynamically allocate memory in VB? I wish to do this....
Dim szData as String * 250
I can't allocate memory like this cause I am not sure how much to alloate.
I want to do something like this..
dim iLen as Integer
dim szData as String
|
by: csnerd |
last post by:
I have a really simple question.
What is the difference between allocating memory following way:
#1
int main(int argc,char* argv)
{
char str)+1];
return 0;
}
|
by: Tony Johansson |
last post by:
Hello Experts!!
When you instansiate varaibles(object) you can do so in four different scops
which are.
Within a block which is called Local or block scope .
Within a function which is called function scope.
Whithin a class which is called class scope.
Outside a function which is called global or file scope.
Now to my question when you allocate memory dynamically you can't say
|
by: kapilk |
last post by:
I have a pointer to which has been allocated some memory now how do i
find out the number of Bytes allocated?
Thanks
|
by: akywy |
last post by:
Two questions about the following code sample:
--- code begins ---
//class IPv4 is defined elsewhere
list<IPv4> ip_list;
for (int i=1; i<=9; i++) {
char addr;
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);
| |
by: Jinkuzo |
last post by:
Have been working on this project for a week, unfortunately missed the lesson on it and have been struggling to figure it out.
These are the instructions and the point at which I'm up to.
So essentially we don't know the size of the array and the user doesn't specify size, it's all done on the fly.
Point I'm up to:
|
by: tom |
last post by:
why delete the dynamically allocated memory twice causes an error, see
the code below:
int _tmain(int argc, _TCHAR* argv)
{
int *pi = new int(12);
cout<<*pi;
delete pi;
delete pi;
}
|
by: sdkumar00 |
last post by:
How can we avoid/detect multiple free calls for a dynamically allocated memory.
void example() {
int *a;
a = (int *)malloc(10);
free(a);
free(a); //how can we detect/avoid multiple free calls
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |