• C

function/variable isolation out of scope


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();
   }

volvickderoseAsked:
Who is Participating?
 
imladrisCommented:
Seeing as that hint seems to have clarified things, this converts it to an answer. Please grade it as appropriate.
0
 
fremsleyCommented:
- 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
 
volvickderoseAuthor Commented:
Adjusted points from 50 to 100
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
volvickderoseAuthor Commented:
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
 
volvickderoseAuthor Commented:

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
 
imladrisCommented:
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
 
volvickderoseAuthor Commented:
ok work when removing malloc from the function

thanks
0
 
imladrisCommented:
Is there anything else you needed clarified before you graded the question?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.