/* partition2.c */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
#define ITERATIONS 10
int rand_in_range(int, int);
int * partition(int, int);
int main(void)
{
int m=8, n=3, i;
int *p;
/* all declarations in main need to be north of here */
/* seed timer */
srand(time(NULL));
/* call partition and examine returns */
printf("set has %d elements and %d partitions\n", m, n);
for(i = 0; i < ITERATIONS; i++)
{
p = malloc((n)*sizeof(int));
p = partition(m,n);
printf("m in n chunks: %d %d %d\n", p[0], p[1], p[2]);
/* free p; */
}
return 0;
}
int rand_in_range(int m, int n)
{
/*seed srand in main */
/* [m, n] is range */
int roll_again_threshold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n);
offset = m;
num_results = n - m + 1;
if (num_results == 1) {
return m;
}
roll_again_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold/num_results;
do {
result = rand();
} while (result >= roll_again_threshold);
result /= divisor;
return offset + result;
}
int * partition(int m, int n)
{
int top_range, i, p;
int *q;
/* end declarations */
/* if n>m bomb out */
if (n > m) return NULL;
top_range = m - n;
q = malloc((n)*sizeof(int));
for (i=0; i<(n-1); i++)
{
p=rand_in_range(0, top_range);
q[i] = p + 1;
top_range = top_range - p;
}
q[n-1]=top_range + 1;
return q;
}
/* end source */
I ask my compiler to carve out memory in main and in a subroutine. The
adage is that if one asks an OS for memory, he must give it back. I
would think that the requested memory in the subroutine would disappear
when the return is made. It would also seem that I've created a memory
leak in main, but when I add a free where it is now commented out, I get
an error. Happy for any hints. frank 6 1845
# I ask my compiler to carve out memory in main and in a subroutine. The
# adage is that if one asks an OS for memory, he must give it back. I
# would think that the requested memory in the subroutine would disappear
# when the return is made. It would also seem that I've created a memory
What makes heap memory like malloc so useful is precisely that the
memory is not automatically released when a procedure returns. The
normal C malloc does not garbage collect, so it is your responsibility
to keep a pointer to each mallocked block until you free it, and not
to use the pointer after free.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no respect for people with no shopping agenda.
Frank Silvermann wrote: /* partition2.c */
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) #define ITERATIONS 10
int rand_in_range(int, int); int * partition(int, int);
int main(void) { int m=8, n=3, i; int *p; /* all declarations in main need to be north of here */ /* seed timer */ srand(time(NULL)); /* call partition and examine returns */ printf("set has %d elements and %d partitions\n", m, n); for(i = 0; i < ITERATIONS; i++) { p = malloc((n)*sizeof(int)); p = partition(m,n); printf("m in n chunks: %d %d %d\n", p[0], p[1], p[2]); /* free p; */ } return 0; }
int rand_in_range(int m, int n) { /*seed srand in main */ /* [m, n] is range */ int roll_again_threshold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n); offset = m; num_results = n - m + 1;
if (num_results == 1) { return m; } roll_again_threshold = RAND_MAX - RAND_MAX%num_results; divisor = roll_again_threshold/num_results;
do { result = rand(); } while (result >= roll_again_threshold); result /= divisor; return offset + result; }
int * partition(int m, int n) { int top_range, i, p; int *q; /* end declarations */ /* if n>m bomb out */ if (n > m) return NULL; top_range = m - n; q = malloc((n)*sizeof(int)); for (i=0; i<(n-1); i++) { p=rand_in_range(0, top_range); q[i] = p + 1; top_range = top_range - p; } q[n-1]=top_range + 1; return q; } /* end source */ I ask my compiler to carve out memory in main and in a subroutine. The adage is that if one asks an OS for memory, he must give it back. I would think that the requested memory in the subroutine would disappear when the return is made. It would also seem that I've created a memory leak in main, but when I add a free where it is now commented out, I get an error. Happy for any hints. frank
it should be
free(p);
Frank Silvermann wrote: { p = malloc((n)*sizeof(int));
Allocate some memory [1] ...
p = partition(m,n);
.... and then immediately make it inaccessible.
Easy fix: remove the mallocation.
/* free p; */
free( p );
--
Chris "seeker" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/
Chris Dollin wrote: Frank Silvermann wrote:
{ p = malloc((n)*sizeof(int));
Allocate some memory [1] ...
p = partition(m,n);
.... and then immediately make it inaccessible.
Easy fix: remove the mallocation.
/* free p; */
free( p );
Thanks all for replies. Apparently the parens matter. Here is my new
version of main, with everything else unchanged from upthread:
int main(void)
{
int m=8, n=3, i, j;
int *p;
/* all declarations in main need to be north of here */
/* seed srand with time */
srand(time(NULL));
/* call partition and examine returns */
printf("set has %d elements and %d partitions\n", m, n);
for(i = 0; i < ITERATIONS; i++)
{
p = malloc((n)*sizeof(int));
p = partition(m,n);
if (p = NULL)
{
printf("failure\n");
}
printf("m in n chunks: ");
/*for (j = 0;j < n; j++)
{
printf(" %d", p[j]);
}
printf("\n");*/
free (p);
}
return 0;
}
/*end source */
Couple of questions: 1) Do I have a memory leak?
2) The output is now commented out. It compiles but causes my OS to
hang. I could send the error report and wait for the Unnamed Vendor to
get back to me, but I'm not sure when hell is to freeze over. Anyways,
besides the fact that the commented-out source doesn't produce behavior
in the ensuing executable that I like, it looks childish, in particular
in a language that was designed to hop around arrays. So this question
is, what is an elegant way in C to print a smallish array of ints? frank
Frank Silvermann <in*****@invalid.net> writes: Chris Dollin wrote: Frank Silvermann wrote:
{ p = malloc((n)*sizeof(int)); Allocate some memory [1] ...
p = partition(m,n); .... and then immediately make it inaccessible. Easy fix: remove the mallocation.
/* free p; */ free( p ); Thanks all for replies. Apparently the parens matter.
Of course they do. free is a function, the parentheses are part of
the syntax of a function call. (Note that return is a special kind of
statement, *not* a function; its syntax is "return;" or
"return expression;", so no parentheses are required. Parentheses are
allowed because an expression may be enclosed in parentheses:
"return (expression);" is legal, but "return ();" is not.)
Here is my new version of main, with everything else unchanged from upthread:
[snip] int *p;
[snip] p = malloc((n)*sizeof(int)); p = partition(m,n);
[snip]
You have a memory leak. You assign the result of malloc() to p, and
you then immediately assign another value to p, losing any reference
to the allocated memory.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... Frank Silvermann <in*****@invalid.net> writes: Chris Dollin wrote: Frank Silvermann wrote:
{ p = malloc((n)*sizeof(int)); Allocate some memory [1] ...
p = partition(m,n); .... and then immediately make it inaccessible. Easy fix: remove the mallocation.
/* free p; */ free( p ); Thanks all for replies. Apparently the parens matter.
Of course they do. free is a function, the parentheses are part of the syntax of a function call. (Note that return is a special kind of statement, *not* a function; its syntax is "return;" or "return expression;", so no parentheses are required. Parentheses are allowed because an expression may be enclosed in parentheses: "return (expression);" is legal, but "return ();" is not.)
Why do people put whitespace between the ultimate 'e' and '(' ? Here is my new version of main, with everything else unchanged from upthread: [snip] int *p; [snip] >> p = partition(m,n);
[snip]
You have a memory leak. You assign the result of malloc() to p, and you then immediately assign another value to p, losing any reference to the allocated memory.
I believe that if I remove:
p = malloc((n)*sizeof(int));
I might be better off. Furthermore, I suspect that the reason the ensuing
/*for (j = 0;j < n; j++)
{
printf(" %d", p[j]);
}
printf("\n");*/
was in trouble was that I had lost any reference to the allocated memory.
frank
----------
I'm just sitting here doing time--john lennon, listening to Yoko practice This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: disco |
last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th
edition - p. 672. There is no erata on this problem at the publisher's
website.
1) Is it a violation of copyright laws to...
|
by: f.oppedisano |
last post by:
Hi,
i would like to allocate two structures making only one malloc call.
So i do prt=malloc(sizeof(struct1)+sizeof(struct2));
After this operation i make two pointers one to the first struct...
|
by: Rodrigo Dominguez |
last post by:
there are sometimes that I use third party libraries, I use some functions
that returns char * or structs, etc. sometimes the memory that is returned
by those libraries, when I try to free this...
|
by: Fernando Cacciola |
last post by:
Help me out here please:
While watching Brad Abraham's MSDN TV talk about the Dispose pattern,
refering to:
public virtual void Dispose ( bool disposing )
{
if ( disposing )
{
<-- WHAT...
|
by: Atul Sureka |
last post by:
Hi,
I want to free the object memory in C# - like we do using 'delete' keyword
in C++.
Lets say I have an object of some class and I want to explicitly free the
memory.
C# do not have any free...
|
by: Amogh |
last post by:
Hi,
My question is related to setting freed pointers to NULL.
After freeing a pointer:
1) Should the freeing routine also be responsible for setting the
pointer to null?
2) Or, should the...
|
by: karthikbalaguru |
last post by:
Hi,
Will 'free' return the memory Immediately to the OS ?
Thx in advans,
Karthik Balaguru
|
by: david |
last post by:
I will past only two segments from the code it should be enough to see
what I did wrong, I think I know there I made a mistake, but how to
fix it I can not tell. This why I need help from you all....
|
by: vivek |
last post by:
Hello,
I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.
When i free all the contents of the main structure, it...
|
by: Andreas Eibach |
last post by:
Hi again,
one of the other big woes I'm having...
typedef struct perBlockStru /* (structure) Long words per block
*/
{
unsigned long *lword;
} lwperBlockStru_t;
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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...
| |