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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:25:29+00:00 2026-06-04T20:25:29+00:00

I’d like to have an array of pointers to static data. For instance void

  • 0

I’d like to have an array of pointers to static data. For instance

  void func(...) {
     ...
     static int mysize = initial_size;
     static double* d[3] = {new double[mysize], new double[mysize], new double[mysize]};
     for(int i=0; i < 3; ++i) {
        if(cond) {
           //-re-allocate d if necessary.
           use d here;
        }
        else {
           use d here;  //-since d is static; it persists and so this is justified?
        }
     }
     //-Can I get away with not deleting d here??
  }

My reasoning is that since d is an array of pointers to static doubles; it is allocated once inside a function and so when everything goes out of scope, it is hopefully deleted? I somehow think not. This is probably wishful thinking and leads to memory leaks?

Perhaps I am better off using a static C++ vector here? I want to use a static here in order to re-use the previously computed and stored data in d, when some conditions are met (e.g. if some condition such as cond or its negation is satisfied). Hope this makes sense and thanks in advance for any ideas.

  • 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-04T20:25:30+00:00Added an answer on June 4, 2026 at 8:25 pm

    As @AdamLiss said above, you could leak memory at the re-allocate d if necessary step if you’re not careful to delete the existing arrays before re-allocating:

    void func(...) {
      static int mysize = initial_size;
      static double* d[3] = {new double[mysize], new double[mysize], new double[mysize]};
      for(int i=0; i < 3; ++i) {
        if(cond) {
          d[i] = new double[2*mysize];  // LEAK!
          use d here;
    

    Even if you remember to delete it like this:

    void func(...) {
      static int mysize = initial_size;
      static double* d[3] = {new double[mysize], new double[mysize], new double[mysize]};
      for(int i=0; i < 3; ++i) {
        if(cond) {
          delete[] d[i];
          d[i] = new double[2*mysize];
    

    there’s a bug, because the new allocation could throw an exception, leaving d[0] pointing to deleted memory, but no way to tell that, so when the function next gets called if d[0] is dereferenced it will be undefined behaviour.

    This would be OK:

    void func(...) {
      static int mysize = initial_size;
      static double* d[3] = {new double[mysize], new double[mysize], new double[mysize]};
      for(int i=0; i < 3; ++i) {
        if(cond) {
          double* tmp = new double[2*mysize];
          std::swap(tmp, d[i]);
          delete[] tmp;
    

    But you would avoid such issues if you used a vector to manage the dynamic memory:

    void func(...) {
      int mysize = initial_size;
      typedef std::vector<double> dvec;
      static dvec d[3] = {dvec(mysize), dvec(mysize), dvec(mysize)};
      for(int i=0; i < 3; ++i) {
        if(cond) {
          //-re-allocate d if necessary.
          d[i].resize(2*mysize);
          use d here;
        }
        else {
          use d here;
        }
      }
    }
    

    This also has the advantage of being able to query the existing size, via d[i].size() and not having to manually copy elements from the old array to the new one when reallocating

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.