-3

Hello stackoverflow so my question is: I want to make a function to generate the array of structures but i get an error whenever i finish inserting the values what is the problem? like so

  struct INFO
{
    char name[20]; // creating the strucure
    int age;

};
void generateArr(INFO *p); // a function to generate the array
void readArr(INFO *p);    // a function to read the array
int main()
{

    INFO *ptr =new INFO; // a pointer that points to the structure
    generateArr(ptr);  // calling the functions
    readArr(ptr);
    delete[]ptr; // deallocating memory

}
void generateArr(INFO *p)
{
    p = new INFO [3]; // generating three INFO structures

 }
void readArr(INFO *p)
{
    for (int i = 0; i < 3; i++)
    {

        cin >> p[i].name>> p[i].age; // inputting the elements
        cout << endl;
    }

}

I tried to make the code as clear as possible, ask me if anything is ambiguous.

  • what error do you get? – tinkertime Jan 02 '17 at 15:12
  • You're only assigning to the parameter in `generateArr`. It has as much effect on the outside world as `void f(int x) { x = 0; }`. – molbdnilo Jan 02 '17 at 15:17
  • You allocate memory for `ptr` in main and try to overwrite the pointer in `generateArr`. That would be a leak if it worked, but it does not because it is passed by value. That's also the reason your program crashes, `ptr` only has memory for 1 element in `readArr`. – Unimportant Jan 02 '17 at 15:17
  • 3
    Please stop trying to learn C++ by trial and error, it will get you nowhere. Learn it systematically from a good book instead. – Baum mit Augen Jan 02 '17 at 15:17
  • heap corruption occured you wrote to memory after ..... sorry haven't memorized the entire message and im currently working on something else. @yankee2905 – Arey M Salih Jan 02 '17 at 15:18
  • sorry i dont quite get your answer. @molbdnilo – Arey M Salih Jan 02 '17 at 15:20
  • that's exactly what im doing, and i bloody hate c++ i have to study it though because of college i do love working with c#. any good books recommendation though? @BaummitAugen – Arey M Salih Jan 02 '17 at 15:21
  • If you have `void f(int x) { x = 0; } int main() { int y = 100; f(y); std::cout << y; }`, do you expect to see `0` or `100`? – molbdnilo Jan 02 '17 at 15:21
  • right, but how to fix that @Unimportant – Arey M Salih Jan 02 '17 at 15:23
  • @AreyMSalih https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Baum mit Augen Jan 02 '17 at 15:23
  • i got it @molbdnilo im overwriting it , but dindnt know how to pass the pointer without intilizing it. – Arey M Salih Jan 02 '17 at 15:24
  • @AreyMSalih That's not my point at all; the point is that you're *not* overwriting it. (The `int` program will print `100`.) And you can always initialise a pointer to the null pointer. – molbdnilo Jan 02 '17 at 15:30
  • I'm so tired today i had a two hour long exam, and tomorrow is OOP exam i got no time to rest their main concern is your marks not how much you know, sorry for not understanding your point earlier. @molbdnilo – Arey M Salih Jan 02 '17 at 15:36

3 Answers3

0

This function :

void generateArr(INFO *p)
{
    p = new INFO [3]; // generating three INFO structures

 }

is not working as you expect it does. It assigns allocated memory to local 'p` parameter, which is not returned to a main. To fix it change p to reference:

void generateArr(INFO *&p)

[edit]

but since you already assigned ptr in main with INFO *ptr =new INFO;, you will get a memory leak this way. So you should remove that line.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

In generateArr(INFO *p), you allocate an array which address is stored in local variable p; on return from the function, any local variable (such as p) is lost, as long as address of the allocated array.

You should get rid of function generateArr(INFO *p), which is useless, and allocate your array into main(), this way :

int main()
{
    INFO *ptr =new INFO[3];
    readArr(ptr);
    delete[]ptr; // deallocating memory
}
shrike
  • 4,449
  • 2
  • 22
  • 38
0

Rewrite the functions at least the following way

INFO * generateArr( size_t n )
{
    return new INFO [n]; // generating three INFO structures
}

void readArr(INFO *p, size_t n )
{
    for (int i = 0; i < n; i++)
    {

        cin >> p[i].name>> p[i].age; // inputting the elements
        cout << endl;
    }
}

and in main call them like

const size_t N = 3;

INFO *ptr = generateArr( N ); // a pointer that points to the structure
readArr( ptr, N );
delete []ptr; // deallocating memory

As for your code then in this statement

INFO *ptr =new INFO; // a pointer that points to the structure

there is allocated only one object of the structure type however it is deleted using operator delete [] instead of the operator delete.

delete[]ptr; // deallocating memory

And inside this function

void generateArr(INFO *p)
{
    p = new INFO [3]; // generating three INFO structures
}

there is a memory leak because variable p is a local variable of the function that was initialized by the value of the function's argument and that will be destroyed after exiting the function. As result the address of the dynamically allocated memory will be lost.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335