473,803 Members | 3,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 getWaveletCoeff s(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;

getWaveletCoeff s(&ld, &hd, &filterlen);

But in main() ld and hd are still NULL, I even tried
getWaveletCoeff s(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 2407
Pushker Pradhan <pu*****@erc.ms state.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 getWaveletCoeff s(float *ld, float *hd, int *filterLen)
Change this to:
void getWaveletCoeff s(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*SZFLOA T);
*hd = malloc(2*SZFLOA T);

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; getWaveletCoeff s(&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
getWaveletCoeff s(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.hel sinki.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.ms state.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 getWaveletCoeff s(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 getWaveletCoeff s(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;

getWaveletCoeff s(&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'.
getWaveletCoeff s(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**********@no os.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 getWaveletCoeff s(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;

getWaveletCoeff s(&ld, &hd, &filterlen);

But in main() ld and hd are still NULL, I even tried
getWaveletCoeff s(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 getWaveletCoeff s(float **ld, float **hd, int *filterLen)
{
*ld = malloc(*filterL en * sizeof **ld);
*hd = malloc(*filterL en * sizeof **hd);
if (!*ld || !*hd) {
fputs("Who knows what to with this error?\n"
"Quiting.\n ", stderr);
exit(EXIT_FAILU RE);
}

(*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;
getWaveletCoeff s(&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
3870
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
2254
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> * f() { vector<int> * v = new vector<int>; return v; }
19
11194
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
3275
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 opening to the file. I fail to write a function that allocates memory for a 2d array and returns it to main. I was trying to pass a pointer to the array back to main, but main cannot access the data. The simplified code (no opening of file, but...
23
3619
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
4952
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 longjmp().Only via using asm? It is strange that I have searched the google groups and FAQs of this group and "googled" the internet but find none useful info. Thanks for any reply!
34
2990
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 the executable ? Is it possible ? Also, how does the compiler add inling to the program ? I know that whenever it sees"inline" in front of the procedure name, it inlines it. But if we give the -finline options, it inline all the procedures ? How
4
2290
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 array? -----The code is as followed------------------- #include <iostream.h>
13
13209
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.