473,810 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(vo id);
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(arrN umb[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(vo id)
{
char quitNow; /* does user want to exit program */

clrscr();
printf("Array Program\n");
printf("-----------------------------------------------------------------\n");
printf("Applica tion 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",&qui tNow);
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;cou nt<10;count++)
{
printf("Please enter a number.\n");
scanf("%d",parr Numb);
fflush(stdin);
printf("GetNumb er 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>maxN umb)
{
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<minN umb)
{
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 1525

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;cou nt<10;count++)
{
printf("Please enter a number.\n");
scanf("%d",parr Numb);
fflush(stdin);
printf("GetNumb er 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(arrN umb[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(arrN umb).

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**********@ho tmail.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(arrN umb[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(arrN umb);

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_Keit h) 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(vo id);
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(arrN umb[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(vo id)
{
char quitNow; /* does user want to exit program */

clrscr();
printf("Array Program\n");
printf("-----------------------------------------------------------------\n"); printf("Applica tion 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",&qui tNow);
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;cou nt<10;count++)
{
printf("Please enter a number.\n");
scanf("%d",parr Numb);
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(vo id);
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(arrN umb);
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(vo id)
{
char quitNow; /* does user want to exit program */

printf("Array Program\n");

printf("-----------------------------------------------------------------\n");
printf("Applica tion will ask for 10 numbers and then return the"
"largest\na nd 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;cou nt<10;count++) {
printf("Please enter a number.\n");
scanf("%d",parr Numb);
printf("GetNumb er 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>maxN umb)
{
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<minN umb)
{
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*******@gmai l.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_Keit h) 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
4676
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 -AA flag. In another news group I came across some interesting (ok scarey) information regarding memory leaks in the STL list<...> container. I have compiled and executed the following code and verified that this does in fact leak on my system.
32
3866
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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
4
2355
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 on, but I really need some help on this one. Ok, so I have this class defined (written by Randy Charles Morin, www.kbcafe.com) which detects memory leaks. It creates a memory check point in the constructor, and another in the destructor, and...
8
3417
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 ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
17
4819
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 easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
8
8559
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 each object within my JS app to help locate my problem? Thanks
7
6939
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 the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem is on the ManagementObjectSearch, upon we Dispose the object it seems that is not releasing the...
3
5332
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". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
17
2552
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. Thanks in advance,
22
9370
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 memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
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...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7664
isladogs
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...
0
6882
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();...
0
5550
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...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.