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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:57:39+00:00 2026-06-04T18:57:39+00:00

I have a function like template <class Type> myFunc(Type** arrayToBeFilled); I call it like

  • 0

I have a function like

template <class Type>
myFunc(Type** arrayToBeFilled);

I call it like this:

double* array = NULL;
myFunc(&array);

And inside the function I do some reading and parsing numbers with strtod function:

//Here comes file opening, getting number of lines and number of doubles in every line

...
    char *inputString = new char[LONG_STRING_SIZE]; 
    char *pNext = NULL;
   (*arrayToBeFilled) = new Type[length*rowSize];

for (int i=0; i<length; i++)
    { 
        source.getline(inputString, LONG_STRING_SIZE);
        pNext = NULL;

     for (int j=0; j<rowSize; j++)
     {
         double d = strtod(inputString, &pNext);
        (*arrayToBeFilled)[i*rowSize+j] = d;
        inputString = pNext;
        pNext = NULL;
     }
    }

Variable d is just for check with debugger – and it’s just fine while running.
But after filling the array I try to print it (just for check)

for (int i=0; i<length; i++)

    {
        for (int j=0; j<rowSize; j++)
            {
                cout<<(*arrayToBeFilled)[i*rowSize+j]<<"  ";
            }
        cout<<"\n";
    } 

And here comes bad output – other numbers, sometimes heap corruption and so. I was printing it in and out of the function – the same results. And I can’t delete this array no or neither out the function – run time errors follow me!

  • 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-04T18:57:40+00:00Added an answer on June 4, 2026 at 6:57 pm

    Why do you use raw C arrays in C++? If you use STL classes like std::vector instead of raw new[], your code will become cleaner, simpler to read and maintain (for example, you don’t need explicit delete[] calls: the destructor will cleanup heap memory). In general, in modern C++ the rule is “if you are writing new or delete, you are doing it wrong” (with some exceptions).

    Note also that with C++11 move semantics, you can simply return the vector instead of using output reference/pointer arguments:

    template <typename Type>
    inline std::vector<Type> myFunc()
    {
        ...
    }
    

    Inside your function body, instead of your code

    (*arrayToBeFilled) = new Type[length*rowSize];
    

    just write:

    std::vector<Type> arrayToBeFilled(length*rowSize);
    

    and then simply return arrayToBeFilled; .

    (Note also that vector‘s can be nested together: you may also use vector<vector<Type>> to make a 2D array, but this is less efficient than a single vector<Type>, which more directly maps to your raw new[] call.)

    In addition, in the code you posted you create a raw C array on the heap with new char[LONG_STRING_SIZE] and assign the pointer to it to inputString; then you modify inputString with an assignment from pNext: but in doing so, you leak the initial array whose pointer was stored in inputString.

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

Sidebar

Related Questions

Suppose I have a function which looks like this: template <class In, class In2>
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
If I have one template class and template function like this template <class T>
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have a (free) function template that looks like this template <typename T> T
Let's say that we have a function like so template <class T, class T2>
I have a class defined like this: template< class TBoundingBox > class BoundingBoxPlaneCalculator {
suggest i have a template function like following: template<class T> void doSomething() { T
So I saw code like this: template<class T> T* safe_ptr_cast(Message& msg) { assert(msg.header()->size() ==

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.