473,379 Members | 1,216 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

return address of new memory

I have a function which should allocate memory, initialize it to some values
and return the address of the initialized memory to the calling function.
void getWaveletCoeffs(float *ld, float *hd, int *filterLen)
{
ld = (float*)malloc(2*SZFLOAT);
hd = (float*)malloc(2*SZFLOAT);

*ld++ = 0.7071;
*ld-- = 0.7071;

*hd++ = -0.7071;
*hd-- = 0.7071;

*filterLen = 2;
}

In main() I have:
float *ld = NULL;
float *hd = NULL;

getWaveletCoeffs(&ld, &hd, &filterlen);

But in main() ld and hd are still NULL, I even tried
getWaveletCoeffs(ld, hd, &filterlen);
But it returns address of some invalid memory. What is the correct way of
passing the args so that I get back the correct addresses?
--
Pushkar Pradhan
Nov 13 '05 #1
3 2380
Pushker Pradhan <pu*****@erc.msstate.edu> scribbled the following:
I have a function which should allocate memory, initialize it to some values
and return the address of the initialized memory to the calling function.
You are getting confused with levels of indirection. A good rule of
thumb: if you find yourself assigning something to a function parameter
(not to what the parameter points at), then you're on the wrong track.
void getWaveletCoeffs(float *ld, float *hd, int *filterLen)
Change this to:
void getWaveletCoeffs(float **ld, float **hd, int *filterLen)

Why? Because you wish to assign to *pointers* in main(). Therefore
this function needs to receive *pointers to pointers*.
If you wish to assign to "foo type", then your function must receive
"pointers to foo type". This goes for any meaning of "foo type".
{
ld = (float*)malloc(2*SZFLOAT);
hd = (float*)malloc(2*SZFLOAT);
Change these to:
*ld = malloc(2*SZFLOAT);
*hd = malloc(2*SZFLOAT);

Note that while your old code assigns to ld and hd themselves, mine
assigns to what they point at. (See the * operator?) This is what I
was talking about earlier.
*ld++ = 0.7071;
*ld-- = 0.7071; *hd++ = -0.7071;
*hd-- = 0.7071;
These also need to be changed, to something like:
**ld = 0.7071;
*(*ld+1) = 0.7071;
**hd = -0.7071;
*(*hd+1) = 0.7071;
If I understood your logic correctly.
*filterLen = 2;
} In main() I have:
float *ld = NULL;
float *hd = NULL; getWaveletCoeffs(&ld, &hd, &filterlen);
This call is OK, assuming filterlen in main() is an int.
But in main() ld and hd are still NULL, I even tried
getWaveletCoeffs(ld, hd, &filterlen);
This call is wrong. Use your original call.
But it returns address of some invalid memory. What is the correct way of
passing the args so that I get back the correct addresses?


Simply keep in mind the rule of thumb I gave: "Always assign to what
the parameters point to, never to the parameters themselves", and you
should get the hang of it.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo
Nov 13 '05 #2
In 'comp.lang.c', "Pushker Pradhan" <pu*****@erc.msstate.edu> wrote:
I have a function which should allocate memory, initialize it to some
values and return the address of the initialized memory to the calling
function. void getWaveletCoeffs(float *ld, float *hd, int *filterLen)
{
ld = (float*)malloc(2*SZFLOAT);
hd = (float*)malloc(2*SZFLOAT);
Modifying the value of a parameter is generally the sign of a design error.

If you want to 'return' a value, you have 2 choices :

- use the 'return' statement
- pass the address of the variable you want return a value to.

In your example, you want to return two values to two pointers to float (BTW,
why not double?). So you need to pass the address of two pointers to float,
via a pointer to the required type (that is actually 'pointer to loat'),
hence :

void getWaveletCoeffs(float **pp_ld, float *pp_hd, int *filterLen)
*ld++ = 0.7071;
Please avoid to have assignement and unary operator on the same instruction.
Your intentions are unclear, and the behaviour could be undefined. Better to
write the exactly like you want it, say:

*ld = 0.7071;
ld++;

and to leave the compiler makes its job of coding and micro-optimizaion.
*ld-- = 0.7071;
BTW, I see nothing wrong with:

ld[0] = 0.7071;
ld[1] = 0.7071;

better to avoid to modify the value returned by malloc(). 'Can hurt!
*hd++ = -0.7071;
*hd-- = 0.7071;

*filterLen = 2;
}

In main() I have:
float *ld = NULL;
float *hd = NULL;

getWaveletCoeffs(&ld, &hd, &filterlen);
This is what you should do after having fixed your interface and function
coding. Right now, its not conforming with you prototype. You should have
compile errors or at least warnings.
But in main() ld and hd are still NULL, I even tried
Sure? You never updated the value in the function. As I told you before
'Modifying the value of a parameter is generally the sign of a design error'.
getWaveletCoeffs(ld, hd, &filterlen);
But it returns address of some invalid memory. What is the correct way
of passing the args so that I get back the correct addresses?


--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3
Pushker Pradhan wrote:
I have a function which should allocate memory, initialize it to some values
and return the address of the initialized memory to the calling function.
void getWaveletCoeffs(float *ld, float *hd, int *filterLen)
{
ld = (float*)malloc(2*SZFLOAT);
hd = (float*)malloc(2*SZFLOAT);

*ld++ = 0.7071;
*ld-- = 0.7071;

*hd++ = -0.7071;
*hd-- = 0.7071;

*filterLen = 2;
}

In main() I have:
float *ld = NULL;
float *hd = NULL;

getWaveletCoeffs(&ld, &hd, &filterlen);

But in main() ld and hd are still NULL, I even tried
getWaveletCoeffs(ld, hd, &filterlen);
But it returns address of some invalid memory. What is the correct way of
passing the args so that I get back the correct addresses?


#include <stdio.h>
#include <stdlib.h>
#include <float.h>

void getWaveletCoeffs(float **ld, float **hd, int *filterLen)
{
*ld = malloc(*filterLen * sizeof **ld);
*hd = malloc(*filterLen * sizeof **hd);
if (!*ld || !*hd) {
fputs("Who knows what to with this error?\n"
"Quiting.\n", stderr);
exit(EXIT_FAILURE);
}

(*ld)[0] = 0.7071;
(*ld)[1] = 0.7071;
(*hd)[0] = -0.7071;
(*hd)[1] = 0.7071;
}

int main(void)
{
float *ld = NULL;
float *hd = NULL;
int filterlen = 2, n;
getWaveletCoeffs(&ld, &hd, &filterlen);
printf("[output]\n");
printf("ld: %p, hd: %p\n", (void *) ld, (void *) hd);
for (n = 0; n < filterlen; n++)
printf("%.*g %.*g\n", FLT_DIG, ld[n], FLT_DIG, hd[n]);
free(ld);
free(hd);
return 0;
}
[output]
ld: 20ab8, hd: 20ac8
0.7071 -0.7071
0.7071 0.7071

--
Martin Ambuhl

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: ~Gee | last post by:
Hi Folks! Please see the program below: 1 #include<iostream> 2 #include<list> 3 #include <unistd.h> 4 using namespace std; 5 int main() 6 { 7 {
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
19
by: Sergey Koveshnikov | last post by:
Hello, If my function return a pointer on a memory area, where do I free it? e.x.: char *foo() { char *p; p = malloc(10); strcpy(p, "something"); return(p); }
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
3
by: Cong Wang | last post by:
Hi,all! I found an interesting problem,it is that how to implement a C function which can be called once and return twice? Just like the POSIX function fork() or the library function...
34
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about...
4
by: | last post by:
The output is: 1234 After getline: 1234 After renew: 1234 After retnp: İİİİİİİİİİİİİİİİG After getp: İİİİİİİİİİİİİİİİG -----What happen after renew/retnp call?------- Why not return a true...
13
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.