Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8528637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:50:51+00:00 2026-06-11T08:50:51+00:00

Suppose the following function: float *dosomething(const float *src, const int N) { float *dst

  • 0

Suppose the following function:

float *dosomething(const float *src, const int N)
{
    float *dst = (float *)malloc(sizeof(float) * N);
    if(!dst)
    {
        printf("Cannot allocate memory\n");
        exit(EXIT_FAILURE);
    }
    for(int i = 0; i < N; i++)
           dst[i] = src[i] * 2;          
    return dst;
}

In this case we don’t need allocate memory previously if we want to use it right?

Now, just another case:

void dosomething(float *dst, const float *src, const int N)
{
    for(int i = 0; i < N; i++)
        dst[i] = src[i] * 2;
}

In the last case we need to allocate memory previously. So I share it and I’m wondering which is the best method for returning an array. Which of them provide more security to an user of the library or class? which method is most recommended? why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:50:52+00:00Added an answer on June 11, 2026 at 8:50 am

    What’s better practice or a better idea depends on what you’re actually trying to do.

    A function like char *strdup(const char *s) (POSIX) is implemented like the first case, it takes a string as an argument, allocates memory for another of the same length and then copies the source to the new piece of memory. It’s convenient and saves you from manually doing the common action of allocating a buffer for the copy of the string. You could assume this is simply like a call to malloc and then strcpy/memcpy.

    Then you’ve got a function like char *strcpy(char *dest, const char *src), which is like the second case, where you have control of where the string is going to be copied to. This way you’re not forced into having the string copied into a dynamically allocated, not of your choice, piece of memory.

    The first way might come in handy if you needed to create and initialise some sort of dynamic structure (list, tree, etc), but then again the second way also suffices and gives you control of what piece of memory is being used; you can use dynamically allocated memory on the heap, or local variables on the stack, etc.

    Personally, I would usually go the second way, because I have more control of what variable’s being initialised, and I’m not forced into having to use a newly malloc‘d piece of memory (what if I wanted my local variable to be initialised?). You could always then write a wrapper function that makes a call to malloc and then to your function using the newly allocated memory as the destination.

    It’s really up to you and your design and what you’re trying to achieve, there are no right and wrong ways and as long as you remember the allocated memory you shouldn’t have any problems. I wouldn’t say either of the two is more “secure.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have the following c function: int MyFunction( const float arr[] )
I have the following function: int do_something(...,const float writeArray[],...){ printf(%d,*writeArray); return 0; } how
Consider the following function: void f(const char* str); Suppose I want to generate a
Suppose I have a function void myFun(int*) In C++ what exactly does the following
suppose we have following function: void someFunction(int * araye){ for (int i=0;i<5;i++) cout <<araye[i]<<'
Suppose I have the following: class dataClass { public: int someData; float moreData; void
Suppose I have the following function: void foo(std::vector<int> vec, int n); If I call
Suppose I have the following code: function DoSomething:Boolean; var obj : TMyObject; i :
Suppose I've the following function: function mul() { return array_reduce(func_get_args(), '*'); } Is is
Suppose you have the following function foo . When I'm running a for loop,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.