Deletion of Dynamically Allocated Memory

Jishan

I have been given a task, where I need to create the string_copy function Note that the function body and prototypes have been given by the source and that needs to be maintained. The portions written by me are after the comment write your code here.

#include <iostream>
using namespace std;
int string_length(const char* string_c);
char* string_copy(const char* string_c);

int main()
{
    const char* string_c = "This is a string and is a long one so that we can create memory leaks when it is copied and not deleted";

    // write your code here
    int length = string_length(string_c);
    cout << "Copied String: " << string_copy(string_c) << endl;
    return 0;
}

int string_length(const char* string) {
    int length = 0;
    for (const char* ptr = string; *ptr != '\0'; ++ptr) {
        ++length;
    }
    return length;
}

char* string_copy(const char* string) {
    // we need to add 1 because of ’\0’
    char* result = new char[string_length(string) + 1];

    // write your code here (remember zero-termination !)
    int i;
    for (i = 0; string[i] != '\0'; ++i)
    {
        result[i] = string[i];
    }
    result[i] = '\0';
    return result;
}

Now task tells me

that it is very important that any memory allocated with e=new TYPE is released later with delete e (and a=new TYPE[size] with delete [] a) else this will lead to an error.

It is not exactly clear if error means compile/runtime error or error as in my task did not meet the requirement error.

My question is, in this code how do I delete the intermediate dynamically created result array? If I delete result, won't it fail the purpose of the task? Then how am I to respect the quotation above or maybe simulate memory leak as given in the long string constant?

Thanks.

EDIT: Why the negative votes? Please at least explain the reason! I am not asking any solution or something, but mere suggestion if I am missing some point or not!

David Schwartz

The caller of string_copy would be responsible for releasing the memory when it's done with it by calling delete[] on it.

This is, by the way, a terrible way to write C++ code. You should be using std::string or std::vector<char> or something like that.

Here's why:

int length = string_length(string_c);
char* copy = string_copy(string_c);
cout << "Copied String: " << copy << endl;
delete[] copy;
return 0;

Yuck.

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Initializing Dynamically allocated integer

From Dev

How do I correctly manage the memory of dynamically allocated objects?

From Dev

Does free() remove the data stored in the dynamically allocated memory?

From Dev

What is the scope of free() in dynamically allocated memory?

From Dev

Why two operators for deleting dynamically allocated memory?

From Dev

Dynamically allocated memory for char and for double or int takes the same amout?

From Dev

Allocate more memory for dynamically allocated array

From Dev

how to avoid memory leak in dynamically allocated widget

From Dev

How to dynamically calculate the size of a dynamically allocated memory

From Dev

How to know pointer is pointing to dynamically or static allocated memory

From Dev

Access value of a pointer in dynamically allocated memory

From Dev

Is it possible to partially free dynamically-allocated memory on a POSIX system?

From Dev

Delphi 7 refcount error when copying a record to dynamically allocated memory

From Dev

Is accessing statically or dynamically allocated memory faster?

From Dev

Using both dynamically-allocated and statically-allocated shared memory

From Dev

Why does freeing the dynamically allocated memory create issue here?

From Dev

dynamically allocated memory with garbage characters

From Dev

Dynamically allocated memory has to be deleted in C++?

From Dev

Initializing structures with dynamically allocated memory

From Dev

C++ delete problems dynamically allocated memory

From Dev

free dynamically allocated memory in QT

From Dev

free with dynamically allocated memory

From Dev

What is the scope of free() in dynamically allocated memory?

From Dev

Dynamically allocated list of dynamically allocated structs is overwriting

From Dev

Getting input into dynamically allocated memory then returning the pointer

From Dev

How to dynamically calculate the size of a dynamically allocated memory

From Dev

How quickly dealocate map which contains dynamically allocated memory as a value?

From Dev

Issue With Freeing Dynamically Allocated Memory In C++

From Dev

Dynamically Allocated Memory Constructor

Related Related

  1. 1

    Initializing Dynamically allocated integer

  2. 2

    How do I correctly manage the memory of dynamically allocated objects?

  3. 3

    Does free() remove the data stored in the dynamically allocated memory?

  4. 4

    What is the scope of free() in dynamically allocated memory?

  5. 5

    Why two operators for deleting dynamically allocated memory?

  6. 6

    Dynamically allocated memory for char and for double or int takes the same amout?

  7. 7

    Allocate more memory for dynamically allocated array

  8. 8

    how to avoid memory leak in dynamically allocated widget

  9. 9

    How to dynamically calculate the size of a dynamically allocated memory

  10. 10

    How to know pointer is pointing to dynamically or static allocated memory

  11. 11

    Access value of a pointer in dynamically allocated memory

  12. 12

    Is it possible to partially free dynamically-allocated memory on a POSIX system?

  13. 13

    Delphi 7 refcount error when copying a record to dynamically allocated memory

  14. 14

    Is accessing statically or dynamically allocated memory faster?

  15. 15

    Using both dynamically-allocated and statically-allocated shared memory

  16. 16

    Why does freeing the dynamically allocated memory create issue here?

  17. 17

    dynamically allocated memory with garbage characters

  18. 18

    Dynamically allocated memory has to be deleted in C++?

  19. 19

    Initializing structures with dynamically allocated memory

  20. 20

    C++ delete problems dynamically allocated memory

  21. 21

    free dynamically allocated memory in QT

  22. 22

    free with dynamically allocated memory

  23. 23

    What is the scope of free() in dynamically allocated memory?

  24. 24

    Dynamically allocated list of dynamically allocated structs is overwriting

  25. 25

    Getting input into dynamically allocated memory then returning the pointer

  26. 26

    How to dynamically calculate the size of a dynamically allocated memory

  27. 27

    How quickly dealocate map which contains dynamically allocated memory as a value?

  28. 28

    Issue With Freeing Dynamically Allocated Memory In C++

  29. 29

    Dynamically Allocated Memory Constructor

HotTag

Archive