Solved

function/variable isolation out of scope

Posted on 2000-04-25
8
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

617 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