?
Solved

returning a ref

Posted on 2003-03-09
10
Medium Priority
?
176 Views
Last Modified: 2011-04-14
Hi!


I created a struct. Now, I want to create a function that will return a pointer to my newly created struct. my question is how to do so? Can I just create a local struct in my function and return it with the & operator?

Here is my simplified struct:
typedef struct {
     char *subject[MAX_SUBJECT];
     char *body[MAX_BODY][LINE_WIDTH];
} creative;

here is my function's prototype:
creative *Initialize_Creative(const char *filename);


so in the body of this function, could I do like:
{
    creative to_return;
    // initialize it from the file

    return &to_return;
}


I don't think this could work, cuz when the function ends, the scope of my struct should go out. So what's the right way to do this?

Thanks a lot,

HiGiJJ!
0
Comment
Question by:higijj
[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
  • 5
  • 4
10 Comments
 
LVL 2

Author Comment

by:higijj
ID: 8098578
I have been told alocating the data on the heap using the new operator would do the job.

so

{
creative to_return = new creative;

// initialize it from the file

return to_return;
}

would do the job :)
0
 
LVL 1

Expert Comment

by:codez80
ID: 8098769
it certainly will Hiqijj, but I thought this was "C" section rather than C++

regards,

codez80
0
 
LVL 2

Author Comment

by:higijj
ID: 8098887
well isn't the above C ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Accepted Solution

by:
Kocil earned 200 total points
ID: 8098922
this is the C version

creative* sub() {
  creative to_return = (creative*) malloc(sizeof(creative));

  /* initialize it from the file */

  return to_return;
}

On the caller, don't forget to free it after use

main()
{
   creative* mycreative;

   mycreative = sub();

   /* use it as you like */

   /* then you MUST do this */
   free(mycreative);
}

0
 
LVL 2

Author Comment

by:higijj
ID: 8098927
ok

so New is basically just a warper of malloc ?!

0
 
LVL 5

Expert Comment

by:Kocil
ID: 8098928
Eh, and your struct needs a little bit correction.
Don't use * on the subject and body, if you mean them as array of char.

typedef struct {
    char subject[MAX_SUBJECT];
    char body[MAX_BODY][LINE_WIDTH];
} creative;
0
 
LVL 2

Author Comment

by:higijj
ID: 8098935
yteah

I modified it so it looks like

typedef struct {
char *subject;
char *body[LINE_WIDTH];
} creative;
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8098939
No, new is not JUST a wrapper of malloc.
It is a special C++ operator to create a new object, and it does:
1. automatically calculate the object size (malloc need explicit size)
2. allocate the memory (this is what malloc do).
3. Call the object constructor (malloc won't do this)
0
 
LVL 2

Author Comment

by:higijj
ID: 8098947
Thanks for the help/explanation!!

Helped me a lot!

HiGiJJ
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8098956
Then you should also malloc some memory for the subject and body one by one.

creative* initialize()
{
   creative* c = malloc(sizeof(creative));

   char init_subject[] = "SAMPLE";  

   c->subject = malloc(strlength(init_subject+1));
   strcpy(c->subject, init_subject);
   
   return c;
}


Consequently, you have to free them also 1 by one;
void freeCreative(creative *c)
{
   /* free from the last to the first */

   free(c->body[LAST]);
   ...
   free(c->subject);
   free(c);
}

0

Featured Post

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!

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

765 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