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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:16:29+00:00 2026-05-22T21:16:29+00:00

I have an array of type T which I pass as a pointer parameter

  • 0

I have an array of type T which I pass as a pointer parameter to a function.
The problem is that I can’t write new data to this array properly, without getting memory violation at the second try.

In this code I read integers from a text file and pass them to the function (part of template class of type T), in order to append them to the array.

After I finish to append the integers, I want to use the same array back in the Main.

Does someone know what’s wrong with the code?

Thanks, Max

template<class T> int CFile<T>::read(T **apBuf, int aNum)
{
int readCounter = 0;

*apBuf = (T*)malloc(sizeof(T)*aNum);


for (int i = 0; i<aNum; i++)
{
    T var = read();

    if (var == NULL)
    {
        if (isEof)
        {

            return readCounter;
        }
        else
        {
            perror ("Error Reading File - Insufficient var type");
            return -1;
        }
    }
    else
    {   
        *apBuf[i] = var;

        readCounter++;
    }
}



return readCounter;

}
  • 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-22T21:16:30+00:00Added an answer on May 22, 2026 at 9:16 pm
    *apBuf[i] = var;
    

    This is parsed as if it was written:

    *(apBuf[i]) = var;
    

    This is obviously not what you want; apBuf is a pointer to a pointer to an array; you are treating it as a pointer to an array and you are dereferencing the ith element of it. What you really mean is:

    (*apBuf)[i] = var;
    

    *apBuf gives you “the object pointed to by apBuf,” which is the array; then you obtain the ith element of the array.

    That said, this is rather unusual: why not accumulate the data into a std::vector<T> and return that from the function? Then you don’t have to worry about the explicit dynamic memory management. (Also, is T always a pointer type? If not, then var == NULL makes no sense.)

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

Sidebar

Related Questions

I'm looking for a kind of array data-type that can easily have items added,
I have a method which returns an array of fixed type objects (let's say
I have a vector-like class that contains an array of objects of type T
I have a vector called actorVector which stores an array of objects of type
I have an array of type string which I want to re-use inside a
I have an object of type bar which has an Array of many foo
I have an array which stores a dictionary of Types: //The dictionary: Dictionary<CacheKey,Type> TypeLookup;
Here's an example of what I mean: I have an array: array('type'=>'text', 'class'=>'input', 'name'=>'username',
I have an array of different type objects and I use a BinaryWriter to
I have an array of any type T ( T[] ) and I want

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.