Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# function/variable isolation out of scope

Posted on 2000-04-25
Medium Priority
217 Views

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
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
• 4
• 3

LVL 2

Expert Comment

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

ID: 2751620
Adjusted points from 50 to 100
0

Author Comment

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

Author Comment

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

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

ID: 2752151
ok work when removing malloc from the function

thanks
0

LVL 16

Accepted Solution

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

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

## Featured Post

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â€¦
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 how to use strings and some functions related to them in the C programming language.
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll