Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i am posting here to receive some help from some senior coders out there about the memory occupied by a c++ or c programme.
the situation was as follows.

I'm doing this problem: http://www.codechef.com/problems/RECIPE I have the solution, but, the memory usage is coming out to be 2.0 MB, which, apparently, is too much considering some ppl have done it using less 0.00...MB memory . I don't understand how My programme is any different than the others..

Can someone please explain how was the programme given below is so different from mine that it uses such less memory..
Some tips on how to decrease memory usage would be appreciated..
(If u want to run the programme please refer to the question before)


SOMEONES CODE (0.00M memory occupied)
C
#include<stdio.h>

int get_gcd(int a, int b)
{
int c = a%b;
if(c == 0)
	return b;
else
	return get_gcd(b, c);
}

main()
{
int T;
int ing[50];
scanf("%d", &T);

while(T-- > 0)
{
	int N, i, min = 1001, second_min, fact, denom;
	
	scanf("%d", &N);
	
	for(i = 0; i < N; i++)
	{
		scanf("%d", &ing[i]);
	
		if(min > ing[i])
		{
			second_min = min;
			min = ing[i];
		}
		else if(second_min > ing[i])
			second_min = ing[i];
	}

	fact = get_gcd(second_min, min);

	while(fact != 1)
	{
		for(i = 0; i < N; i++)
		{
			if(ing[i]%fact)
				break;
		}

		if(i == N)
			break;
		fact--;
		while(min%fact || second_min%fact)
			fact--;
	}
		
	if(fact != 1)
	{
		for(i = 0; i < N-1; i++)
			printf("%d ", ing[i]/fact);
		printf("%d", ing[i]/fact);
	}
	else
	{
		for(i = 0; i < N-1; i++)
			printf("%d ", ing[i]);
		printf("%d", ing[i]);
	}
	printf("\n");
}

return 0;
}

MY C CODE (2.0M memory occupied)
C
#include<stdio.h>
int main()
{
    int T,N,i[50],j,p,r;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        for(j=0;j<N;j++)
        {
    	    scanf("%d",&i[j]);
        }
        k:printf("");
        int min=i[0];
        for(j=0;j<N;j++)
        {
    	    if(min>i[j])
    	    {
    		    min=i[j];
    	    }
        }
        for(r=2;r<=min;r++)
        {
    	    p=0;
    	    for(j=0;j<N;j++)
    	    {
    		    if(i[j]%r==0)
    		    {
    		    p++;	
    		    }
            }  
            if(p==N)
            {
        	    goto t;
    	    }
        }
    	    for(j=0;j<N;j++)
            {
    	       printf("%d ",i[j]);
            }
            goto p;
        t:for(j=0;j<N;j++)
        {
    	    i[j]=i[j]/r;
        }
        goto k;
        p:printf("");
    }
}
Posted
Updated 14-Jul-15 3:28am
v2
Comments
[no name] 14-Jul-15 9:37am    
What do you mean by memory occupied and how are you measuring it? How does one get a value of 0?
Jochen Arndt 14-Jul-15 10:08am    
Do you talk about the size of the executables?
These can differ for various reasons:
- Operating system (e.g. Windows vs. Linux)
- Dynamic or static linking of (standard) libraries
- Debug or release builds
Sergey Alexandrovich Kryukov 14-Jul-15 11:17am    
Where did you get those 0.00M and 2.0M? If you know it from something like Process Manager, you should not trust it.
—SA
Richard MacCutchan 14-Jul-15 11:41am    
2.0Mb is nothing in today's computers.
chandanadhikari 15-Jul-15 3:16am    
dear fellow CPians,
i went to the page mentioned in question at
http://www.codechef.com/problems/RECIPE

this is a programming competition site. They have a list wherein they display the memory footprint and execution runtime for the submitted solutions. Its hard to say how they arrive at these figures.I think these are the figures the OP is worried about. (please excuse me if you already saw this info on the page )

Hi, Tejas_S_B :: Can't tell you where your solution is eating memory without using an ide and profiler. But i think the other implementation is way more cleaner. Better try a solution without 'goto'.

just suggesting because a secondary intention of such exercise would be to test your program design skills also .

1 solution

The amount of memory you use depend on the compiler and linker you use, the settings, the system libraries involved, and last but not least the OS itself.

As an example, if you use any kind of IO in Windows, you're attaching a huge chunk of library to your executable. You can try that by comparing two trivial programs: one with an empty main function and one that just prints "hello world!" (you might want to add some code to pause the execution to make sure you can measure the memory use, e. g. system("Pause"); )

If you need to fit your application into a very restricted amount of memory (for embedded computing?) then you should check the documentation of your OS: it may be possible to strip the libraries you use from functions that you don't need, or there may be other, less memory hungry implementations for the same library.


[edit]
I just saw the comment by chandanadhikari above. Apparently my above solution does not address the actual issue at all, as this puts the question into an entirely different light. So here is my updated solution:

The site in question uses it's own on-site IDE for measuring time and memory, here: http://www.codechef.com/ide[^]
I've just copied your code there and it said it is using 3.144KB of memory, not 2MB. So, no worries, regarding memory your program is fine. It is only your own IDE that packs unneeded system libraries on top of that (yet another reason not to develop and run high profile software on Windows...)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900