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 6012285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:22:54+00:00 2026-05-23T02:22:54+00:00

int a[1000]; int *c; void foo(void) { int b[1000]; memcpy(b, someConstArray, 1000); c =

  • 0
int a[1000];
int *c;

void foo(void)
{
     int b[1000];
     memcpy(b, someConstArray, 1000);
     c = (int *)malloc(sizeof(b));
     memcpy(c, b, 1000);
}

void bar(void)
{
     memcpy(a, someConstArray, 1000);
     c = (int *)malloc(sizeof(a));
     memcpy(c, a, 1000);
}

I know this has been asked quite a number of times, but I need to know what the performance difference between auto allocation vs static allocation, e.g. a vs b, in relative to each other. Does declaring the a this way take performance hit because of locality?

Compile in gcc for embedded systems.

PS: I am aware that it is a redundant and useless function. The main question is how does the variable allocation affects performance

  • 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-05-23T02:22:55+00:00Added an answer on May 23, 2026 at 2:22 am

    In char* foo(void) you return an invalid pointer, because b ceases to exist when you exit the function.

    Both allocations have the same performance , it’s just the way you use them is incorrect.

    b is allocated on the stack when the function foo is being called (the performance is of simply changing the value of the stack pointer). It’s deallocated when foo is done (by changing the stack pointer, again).

    a is allocated somewhere (global, I’m guessing, or on stack in some other context), and there’s no performance hit there as well.

    If you need to allocate memory within your function that should be given to the caller (as in your foo) – then the allocation should be dynamic – using malloc (or new if C++). Then there’s indeed a performance hit, depending on the relevant memory manager performance.

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

Sidebar

Related Questions

void test() { int buf[1000]; //populate buf foo(buf);//is this correct? Can we pass buf
I have a pictures table that has the following columns: PICTURE_ID int IDENTITY(1000,1) NOT
I just translated this program, #include <stdio.h> int dam[1000][1000]; int main (int argc, const
I have the following code: template<int k> void foo() { } int main(int argc,
My code is like this: [Serializable] [StructLayout(LayoutKind.Sequential,Pack=1)] struct Foo { public byte Bar; public
#include <stdio.h> const int str[1000] = {0}; int main(void) { printf(arr is %d\n, str[0]);
I have the following Int lists: t1 = [1000, 1001, 1002, 1003, 1004] t2
Example int *ptr; *ptr = 1000; can I catch memory access violation exception using
int d; d = some_string.IndexOf(something,1000); I want indexOf to search some_string , starting at
int x = n / 3; // <-- make this faster // for instance

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.