Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

function/variable isolation out of scope

Posted on 2000-04-25
8
Medium Priority
?
223 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 300 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.
Suggested Courses

916 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