Solved

function/variable isolation out of scope

Posted on 2000-04-25
8
193 Views
Last Modified: 2010-08-05

This is a simple problem I try to solve.  I forgot how to do that.  That will probably take me a while to figure out.  I may have made a mistake in memory allocation (malloc), but I don't see it.

The problem is, inside the function, the output variable gives good result (0 and 1), and outside the function, the output variable give wrong result (print memory location instead of 1 and 0).

Here is the code, you can try to compile it and see the problem

thanks

#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>


void deci2bin(int d, int size, int *b)
      {
   int i;

   for(i = 0; i < size; i++)
         {
      b[i] = 0;
      }
   b[size - 1] = d & 0x01;

    for (i = size - 2; i >= 0; i--)
          {
      d = d >> 1;
      b[i] = d & 0x01;
          }
      }


void gen01dat( long data_len, int *out_array)
      {
   long t,j;
   int y1[100];
   int b[100];
   int y2[100][8];
   int y3[800];
   int y4[800];
   out_array = (int*)malloc(800*sizeof(int));


    for (t = 0; t < data_len; t++)
          {
      out_array[t] = ceil(127*sin(0.5*t));
      }

        for (t =0;t<100;t++)
         {
      deci2bin(out_array[t],8,b);
         for (j=0;j<8;j++)
            {
         y2[t][j] = b[j];
         }
      }

   for (t=0;t<100;t++)
         {
      for (j=0;j<8;j++)
            {
         y3[8*t + j] = y2[t][j];
         }
      }

   for (t=0;t<800;t++)
         {
      out_array[t] = y3[t];
      }

   for (t=0;t<800;t++)
         {
      y4[t] = 1;
      if (out_array[t] < 0)
            {
         out_array[t] = -out_array[t];
         y4[t] = -1;
         }
      y4[t] = out_array[t] * y4[t];
      }

   for (t=0;t<800;t++)
         {
     out_array[t] = y4[t];
      }

   for(int i=0;i<20;i++)
         {
      cout<<out_array[i];
      }
      cout<<endl;

   free(out_array);
   }

void main()
      {
   long n;
   int out[20];

   gen01dat(n,out);
   for (int i=0;i<20;i++)
         {
      cout<<out[i];
      cout<<endl;
      }
   getch();
   }

0
Comment
Question by:volvickderose
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:fremsley
ID: 2750339
- in main(): n is not initialized when calling gen01dat()
 
- out_array is a local pointer in gen01dat() that is initialized with the address of out[] in main() but overwritten by the malloc() call. After that you have no reference to out[] within gen01dat().
0
 

Author Comment

by:volvickderose
ID: 2751620
Adjusted points from 50 to 100
0
 

Author Comment

by:volvickderose
ID: 2751621
So how to solve the problem.  I forgot to initialize n.  N can be treated as a constan.  Even when I do that, I still don't have the right answer

void main()
      {
   int *out_array = (int*)malloc(100*sizeof(int));

   gen01dat(20,out_array);
   for (int i=0;i<20;i++)
         {
      cout<<out_array[i];
      cout<<endl;
      }
   getch();
   }
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:volvickderose
ID: 2751653

Actually, I solve the problem by declaring out as a global variable and equate that variable to the out array in the function body.  So how to solve the problem without doing that.

here is what I did and it works fine, but I don't like it.

#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>

int out[800];

void deci2bin(int d, int size, int *b)
      {
   int i;

   for(i = 0; i < size; i++)
         {
      b[i] = 0;
      }
   b[size - 1] = d & 0x01;

    for (i = size - 2; i >= 0; i--)
          {
      d = d >> 1;
      b[i] = d & 0x01;
          }
      }


void gen01dat( long data_len, int *out_array)
      {
   long t,j;
   int b[100];
   int y2[100][8];
   int y3[800];
   int y4[800];
   out_array = (int*)malloc(800*sizeof(int));


    for (t = 0; t < data_len; t++)
          {
      out_array[t] = ceil(127*sin(0.5*t));
      }

        for (t =0;t<100;t++)
         {
      deci2bin(out_array[t],8,b);
         for (j=0;j<8;j++)
            {
         y2[t][j] = b[j];
         }
      }

   for (t=0;t<100;t++)
         {
      for (j=0;j<8;j++)
            {
         y3[8*t + j] = y2[t][j];
         }
      }

   for (t=0;t<800;t++)
         {
      out_array[t] = y3[t];
      }

   for (t=0;t<800;t++)
         {
      y4[t] = 1;
      if (out_array[t] < 0)
            {
         out_array[t] = -out_array[t];
         y4[t] = -1;
         }
      y4[t] = out_array[t] * y4[t];
      }

   for (t=0;t<800;t++)
         {
     out_array[t] = y4[t];
     out[t] = out_array[t];
      }
   /*
   for(int i=0;i<20;i++)
         {
      cout<<out_array[i];
      }
      cout<<endl; */

   free(out_array);
   }

void main()
      {

   //cout<<endl;
   gen01dat(800,out);
   for (int i=0;i<20;i++)
         {
      cout<<out[i];
      }
   getch();
   }

 
0
 
LVL 16

Expert Comment

by:imladris
ID: 2752058
I would have expected this:

void main()
{
   int *out_array = (int*)malloc(100*sizeof(int));

   gen01dat(20,out_array);
   for (int i=0;i<20;i++)
    {
      cout<<out_array[i];
      cout<<endl;
      }
   getch();
   }


to work, as long as you removed the malloc call inside gen01dat. It is not needed (and in fact detrimental) since out_array (and its corresponding argument) already point to a block of memory.

Conversly, I would expect it to still fail, even with a global declaration of out[800], as long as the malloc call is still in gen01dat.

Is malloc call still really there in gen01dat, or is the code posting inaccurate?

0
 

Author Comment

by:volvickderose
ID: 2752151
ok work when removing malloc from the function

thanks
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 2753160
Seeing as that hint seems to have clarified things, this converts it to an answer. Please grade it as appropriate.
0
 
LVL 16

Expert Comment

by:imladris
ID: 2759550
Is there anything else you needed clarified before you graded the question?
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question