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

Memory Leak where?

Can someone help me out here? I have a bunch of test statements in the
program and my test printf("Do I have trouble here after
GetNumbers()?\n"); does not display rather I get garbage. Please help.

#include <stdio.h>
#include <system.h>

//prototypes
char DisplayTitle(void);
void GetNumbers(int *parrNumb);
int FindMax(int *parrNumb);
int FindMin(int *parrNumb);
void DisplayNumbers(int min, int max);

int main(void)
{
char quitNow='a'; /* to determine if uer wants to quit */
int min=0; /* minimum number in array */
int max=0; /* maximum number in array */
int arrNumb[10]; /* user input into array */
int *parrNumb; /* pointer to array */
int i=0; /* used as a counter in array */

for (i=0; i<10; i++) /* initialize array to 0's */
arrNumb[i] = 0;

//display the title of the program
quitNow = DisplayTitle();

if (quitNow=='Q' || quitNow=='q')
{
return 0;
}

//printf("Do I have trouble here before GetNumbers()?\n");
//Get all 10 numbers from user
GetNumbers(arrNumb[10]);

printf("Do I have trouble here after GetNumbers()?\n");

//Find minimum number in array
min = FindMin(arrNumb[10]);
printf("\nmin is: %i",min);
//Find maximum number in array
max = FindMax(arrNumb[10]);

printf("\nmin is: %i",min);
printf("\nmax is: %i",max);
printf("Do I have trouble here before DisplayNumbers()?\n");
//display the output
DisplayNumbers(min, max);
printf("Do I have trouble here after DisplayNumbers()?\n");

fflush(stdin);
printf("\nPress any key to end.");
getche();
return 0;
}

/* DisplayTitle Function
Input : Non
Process: Displays Title and Instructions
Output : None
*/
char DisplayTitle(void)
{
char quitNow; /* does user want to exit program */

clrscr();
printf("Array Program\n");
printf("-----------------------------------------------------------------\n");
printf("Application will ask for 10 numbers and then return the
largest\nand smallest numbers.\n");
printf("Enter (Q) to Quit Application or press any other key to
continue.\n");
printf("-----------------------------------------------------------------\n");
scanf("%c",&quitNow);
fflush(stdin);
return quitNow;
}
/* GetNumbers Function
Input : Array of integers
Process: Get user's input (up to 10 times)
Output : None. Values returned through pointer
*/
void GetNumbers(int *parrNumb)
{
int getnumb=0; /* user choice from menu passed to main */
int count; /* used to determine when 10 numbers added */

for(count=0;count<10;count++)
{
printf("Please enter a number.\n");
scanf("%d",parrNumb);
fflush(stdin);
printf("GetNumber is : %d\n",*parrNumb);
printf("INDEX IS %i\n",count);

*parrNumb++;

getnumb=0; /*reinitialize*/

}

}
/* FindMax Function
Input : Pointer to array
Process: Finds maximum number in array
Output : Returns maximum number
*/
int FindMax(int *parrNumb)
{
int maxNumb=0; /* used to store maximum number of array */
int i=0; /* used as a counter for the array */

maxNumb = *parrNumb;

for (i=1; i<10; i++) /* initialize array to 0's */
{
*parrNumb++; /* go to next index */

if (*parrNumb>maxNumb)
{
maxNumb = *parrNumb;
}

}

return maxNumb;
}
/* FindMin Function
Input : Pointer to array
Process: Finds minimum number in array
Output : Returns minimum number
*/
int FindMin(int *parrNumb)
{
int minNumb=0; /* used to store minimum number of array */
int i=0; /* used as a counter for the array */

minNumb = *parrNumb; /* assign first index to minNumb */

for (i=1; i<10; i++) /* initialize array to 0's */
{
*parrNumb++; /* go to next index */

if (*parrNumb<minNumb)
{
minNumb = *parrNumb;
}

}

return minNumb;
}
/* DisplayNumbers Function
Input : Minimum number in array and maximum number in array
Process: Displays minimum and maximum numbers
Output : None
*/
void DisplayNumbers(int min, int max)
{
//clrscr();
printf("\nThe minimum number in the array is %i.",min);
printf("\nThe maximum number in the array is %i.",max);
}
Nov 14 '05 #1
6 1503

MathewLovesC wrote:
Can someone help me out here? I have a bunch of test statements in the program and my test printf("Do I have trouble here after
GetNumbers()?\n"); does not display rather I get garbage. Please help.

<main() snipped!>
/* GetNumbers Function
Input : Array of integers
Process: Get user's input (up to 10 times)
Output : None. Values returned through pointer
*/
void GetNumbers(int *parrNumb)
{
int getnumb=0; /* user choice from menu passed to main */
int count; /* used to determine when 10 numbers added */

for(count=0;count<10;count++)
{
printf("Please enter a number.\n");
scanf("%d",parrNumb);
fflush(stdin);
printf("GetNumber is : %d\n",*parrNumb);
printf("INDEX IS %i\n",count);

*parrNumb++;
This statement is NOT moving the pointer parrNumb to the next address.
Rather, it is incrementing the integer value of *parrNumb by one. For
pointer arithmetic, simply use parrNumb++.

getnumb=0; /*reinitialize*/

}

}


The remaining functions (<snipped!>) are all showing the same error.
Fix these and The program should behave a little sensibly.

-Jason

Nov 14 '05 #2

MathewLovesC wrote:
Can someone help me out here? I have a bunch of test statements in the program and my test printf("Do I have trouble here after
GetNumbers()?\n"); does not display rather I get garbage. Please help.

<snip>
void GetNumbers(int *parrNumb);
<snip>
int main(void)
{
char quitNow='a'; /* to determine if uer wants to quit */
int min=0; /* minimum number in array */
int max=0; /* maximum number in array */
int arrNumb[10]; /* user input into array */
<snip>
GetNumbers(arrNumb[10]);


Here you are passing the value array element 10 (which, btw, is just
_past_ the boundary of this array) as the pointer argument to your
function. You might see a warning similar to "passing arg 1 of
`GetNumbers' makes pointer from integer without a cast" from your
compiler.

What you really want to do is simply type GetNumbers(arrNumb).

Previously I stated that *parrNumb++ was incorrect. I was only
partially correct. The ++ is higher precedence that the *, so this
actually _was_ working correctly. However, there is no need to
dereference this since you aren't doing anything with the value.

-Jason

Nov 14 '05 #3
sa**********@hotmail.com (MathewLovesC) writes:
Can someone help me out here? I have a bunch of test statements in the
program and my test printf("Do I have trouble here after
GetNumbers()?\n"); does not display rather I get garbage. Please help.

#include <stdio.h>
#include <system.h>
There is no standard header called <system.h>. There may be a
system-specific one but I don't see any strong need to use any
non-portable constructs in your code.

[...] void GetNumbers(int *parrNumb); [...]
int main(void)
{ [...] int arrNumb[10]; /* user input into array */ [...] GetNumbers(arrNumb[10]);
arrNumb[10] is an expression of type int; you're passing it as an
argument of type int*. You're also accessing an element past the end
of the array. You probably want

GetNumbers(arrNumb);

which passes the base address of your array.
printf("Do I have trouble here after GetNumbers()?\n");

//Find minimum number in array
min = FindMin(arrNumb[10]);
See above.
printf("\nmin is: %i",min);
//Find maximum number in array
max = FindMax(arrNumb[10]);
See above.

[snip]
fflush(stdin);
This invokes undefined behavior. See question 12.26 in the C FAQ,
<http://www.eskimo.com/~scs/C-faq/top.html>. You do this several
times; I won't point out the other instances.
printf("\nPress any key to end.");
This would be a good place for fflush(stdout); otherwise the output
may not appear until you print a newline character (stdout may be
line-buffered).
getche();
No such function in standard C. The program should end on its own
even if you don't wait for user input. If you have some reason to
wait for user input (say, because the OS will close the window
containing your output), you can do it with a standard function like
getchar() (though that probably won't return until you enter a
newline).

[snip]
clrscr();
This is a non-standard function.

Why do you want to clear the screen anyway? It's up to you, but if I
run your program I might have important information on my screen; if
you erase it without a very good reason I'll be annoyed.

[snip]
void DisplayNumbers(int min, int max)
{
//clrscr();
printf("\nThe minimum number in the array is %i.",min);
printf("\nThe maximum number in the array is %i.",max);
}


The "%i" format is equivalent to the "%d" format. I'm not sure why
they both exist. I always use "%d" rather than "%i" myself.

I notice your printfs tend to have the newline at the beginning rather
than at the end. Why?

The errors I've pointed out probably aren't the only ones in your
program.

Several of these errors should have been caught by your compiler, at
least as warnings. If they weren't, you should increase the warning
level on your compiler. If they were, you really should pay attention
to what your compiler tells you.

--
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.
Nov 14 '05 #4

MathewLovesC wrote:
Can someone help me out here? I have a bunch of test statements in the program and my test printf("Do I have trouble here after
GetNumbers()?\n"); does not display rather I get garbage. Please help.
#include <stdio.h>
#include <system.h>

//prototypes
char DisplayTitle(void);
void GetNumbers(int *parrNumb);
int FindMax(int *parrNumb);
int FindMin(int *parrNumb);
void DisplayNumbers(int min, int max);

int main(void)
{
char quitNow='a'; /* to determine if uer wants to quit */
int min=0; /* minimum number in array */
int max=0; /* maximum number in array */
int arrNumb[10]; /* user input into array */
int *parrNumb; /* pointer to array */
int i=0; /* used as a counter in array */

for (i=0; i<10; i++) /* initialize array to 0's */
arrNumb[i] = 0;

//display the title of the program
quitNow = DisplayTitle();

if (quitNow=='Q' || quitNow=='q')
{
return 0;
}

//printf("Do I have trouble here before GetNumbers()?\n");
//Get all 10 numbers from user
GetNumbers(arrNumb[10]);

printf("Do I have trouble here after GetNumbers()?\n");

//Find minimum number in array
min = FindMin(arrNumb[10]);
printf("\nmin is: %i",min);
//Find maximum number in array
max = FindMax(arrNumb[10]);

printf("\nmin is: %i",min);
printf("\nmax is: %i",max);
printf("Do I have trouble here before DisplayNumbers()?\n");
//display the output
DisplayNumbers(min, max);
printf("Do I have trouble here after DisplayNumbers()?\n");

fflush(stdin);
printf("\nPress any key to end.");
getche();
return 0;
}

/* DisplayTitle Function
Input : Non
Process: Displays Title and Instructions
Output : None
*/
char DisplayTitle(void)
{
char quitNow; /* does user want to exit program */

clrscr();
printf("Array Program\n");
printf("-----------------------------------------------------------------\n"); printf("Application will ask for 10 numbers and then return the
largest\nand smallest numbers.\n");
printf("Enter (Q) to Quit Application or press any other key to
continue.\n");
printf("-----------------------------------------------------------------\n"); scanf("%c",&quitNow);
fflush(stdin);
return quitNow;
}
/* GetNumbers Function
Input : Array of integers
Process: Get user's input (up to 10 times)
Output : None. Values returned through pointer
*/
void GetNumbers(int *parrNumb)
{
int getnumb=0; /* user choice from menu passed to main */
int count; /* used to determine when 10 numbers added */

for(count=0;count<10;count++)
{
printf("Please enter a number.\n");
scanf("%d",parrNumb);
fflush(stdin);

^^^^^^^^^^^^

Are you sure this is doing what's expected? Remove this statement.

<snip>

Nov 14 '05 #5
MathewLovesC wrote:

Can someone help me out here? I have a bunch of test statements in the
program and my test printf("Do I have trouble here after
GetNumbers()?\n"); does not display rather I get garbage. Please help.


Keith Thompson's remarks were on the money.
You almost had it. Try this:

/* BEGIN new.c */

#include <stdio.h>

char DisplayTitle(void);
void GetNumbers(int *parrNumb);
int FindMax(int *parrNumb);
int FindMin(int *parrNumb);
void DisplayNumbers(int min, int max);

int main(void)
{
char quitNow='a'; /* to determine if uer wants to quit */
int min=0; /* minimum number in array */
int max=0; /* maximum number in array */
int arrNumb[10]; /* user input into array */
int i=0; /* used as a counter in array */

for (i=0; i<10; i++) { /* initialize array to 0's */
arrNumb[i] = 0;
}
quitNow = DisplayTitle();

if (quitNow=='Q' || quitNow=='q') {
return 0;
}
GetNumbers(arrNumb);
min = FindMin(arrNumb);
printf("\nmin is: %i",min);
max = FindMax(arrNumb);
printf("\nmax is: %i",max);
DisplayNumbers(min, max);
return 0;
}

/* DisplayTitle Function
Input : Non
Process: Displays Title and Instructions
Output : None
*/
char DisplayTitle(void)
{
char quitNow; /* does user want to exit program */

printf("Array Program\n");

printf("-----------------------------------------------------------------\n");
printf("Application will ask for 10 numbers and then return the"
"largest\nand smallest numbers.\n");
printf("Enter (Q) to Quit Application or press any other key to"
"continue.\n");

printf("-----------------------------------------------------------------\n");
scanf("%c", &quitNow);
return quitNow;
}
/* GetNumbers Function
Input : Array of integers
Process: Get user's input (up to 10 times)
Output : None. Values returned through pointer
*/
void GetNumbers(int *parrNumb)
{
int getnumb=0; /* user choice from menu passed to main */
int count; /* used to determine when 10 numbers added */

for(count=0;count<10;count++) {
printf("Please enter a number.\n");
scanf("%d",parrNumb);
printf("GetNumber is : %d\n",*parrNumb);
printf("INDEX IS %i\n",count);
*parrNumb++;
getnumb=0; /*reinitialize*/
}
}
/* FindMax Function
Input : Pointer to array
Process: Finds maximum number in array
Output : Returns maximum number
*/
int FindMax(int *parrNumb)
{
int maxNumb=0; /* used to store maximum number of array */
int i=0; /* used as a counter for the array */

maxNumb = *parrNumb;

for (i=1; i<10; i++) /* initialize array to 0's */
{
*parrNumb++; /* go to next index */

if (*parrNumb>maxNumb)
{
maxNumb = *parrNumb;
}

}

return maxNumb;
}
/* FindMin Function
Input : Pointer to array
Process: Finds minimum number in array
Output : Returns minimum number
*/
int FindMin(int *parrNumb)
{
int minNumb=0; /* used to store minimum number of array */
int i=0; /* used as a counter for the array */

minNumb = *parrNumb; /* assign first index to minNumb */

for (i=1; i<10; i++) /* initialize array to 0's */
{
*parrNumb++; /* go to next index */

if (*parrNumb<minNumb)
{
minNumb = *parrNumb;
}

}

return minNumb;
}
/* DisplayNumbers Function
Input : Minimum number in array and maximum number in array
Process: Displays minimum and maximum numbers
Output : None
*/
void DisplayNumbers(int min, int max)
{
printf("\nThe minimum number in the array is %i.\n",min);
printf("The maximum number in the array is %i.\n",max);
}

/* END new.c */
--
pete
Nov 14 '05 #6
"Minti" <im*******@gmail.com> writes:
MathewLovesC wrote:

[snip about 100 lines]
fflush(stdin);

^^^^^^^^^^^^

Are you sure this is doing what's expected? Remove this statement.


There's no need to quote that much context to point out a single
self-contained error.

Also, fflush(stdin) does invoke undefined behavior, but realistically
it's unlikely to cause a memory leak.

--
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.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
4
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.