-2

How to free memory with something like this (Visual Studio 2008 - Win32/console):

I can include only: iostream

#include <iostream>

void data_t(char *test[])
{
    test[0] = new char[];
    test[1] = new char[];
    test[0] = "Test1";
    test[1] = "Test2";
}

int main()
{
    char *test[2];
    data_t(test);
    cout<<test[0]<<"\n";
    cout<<test[1]<<"\n";
    delete[] test[0];//Debug assertion failed! - The program '[7884] Zadanie_4_sortowanie.exe: Native' has exited with code 3 (0x3).
    delete[] test[1];
}

What i do wrong?

4
  • 4
    What is new char[] intended to mean? It is not legal C++.
    – jalf
    Feb 17, 2013 at 23:15
  • It's sad that MSVS accepts this sort of thing. I wonder what it does with it? Feb 17, 2013 at 23:19
  • 1
    The assigment operator on character pointers does not copy data, it just changes it just assigns the pointer. So, assuming you fixed new[]s to be, say, new[6] so that those two lines worked you immediately leak that memory by redirecting both pointers to point to string literals. The error occurs because you can only delete[] something that you allocated with new[] and those pointers currently point to the literals. Short version, use std:string if you can and understand c string manipulation if you must use null terminated character buffers. Feb 17, 2013 at 23:22
  • Im sure the array index is a typo. YOU CAN'T FREE STRING LITERALS. AHHHH. MUST... USE.... STRCPY... TO MAKE... PROFESSOR... FEEL... GOOD.. ABOUT BEING... SO.. OLD...AS... TO ... KNOW..... C
    – Bingo
    Feb 17, 2013 at 23:26

3 Answers 3

5

test[0] contains a pointer to the static string "Test1", which cannot be deallocated. Use strcpy to copy C strings.

1
  • 2
    Yes. The real issue. Considering the code is claimed to complie, the issue of neglecting the size of the array is probably a typo.
    – Bingo
    Feb 17, 2013 at 23:21
5

char[] is an incomplete type, and cannot be used in a new expression. You need to actually decide on an array size, like:

char * p = new char[200];

Then you can say delete[] p; when you're done.

You will need something like strcpy to copy data into the char array. The assigment you wrote only overwrites the pointer, thus losing track of the dynamic allocation (i.e. leaking). (In fact, you might not need dynamic allocation at all if you just want fixed strings, so just remove the lines with new and delete in them.)


What you really want, though, is a std::array<std::string, 2>, though:

#include <array>
#include <string>
#include <iostream>

std::array<std::string, 2> test =  { "Test1", "Test2" };
std::cout << test[0] << "\n" << test[1] << "\n";

Or pass it by reference:

void populate(std::array<std::string, 2> & a)
{
    a[0] = "Test1";
    a[1] = "Test2";
}

int main()
{
    std::array<std::string, 2> test;
    populate(test);
    // print ...
}
8
  • 2
    I would also mention that data_t() leaks memory, and doing delete[] p on a string literal is likely to cause troubles
    – Andy Prowl
    Feb 17, 2013 at 23:13
  • But i need to free memory ? i have memory leak with that code?
    – wcale
    Feb 17, 2013 at 23:15
  • 2
    @wcale: Your code shouldn't even compile... But yes, you would be leaking memory if this worked, because you're losing the pointer returned by the new expression.
    – Kerrek SB
    Feb 17, 2013 at 23:15
  • 2
    @wcale: When you will fix your code to make it compile (by adding a concrete size to new char[]), you will get a memory leak, because when doing test[0] = "Test1" and test[1]="Test2" you are reassigning test[0] and test[1] without releasing the memory they previously pointed to. And then when doing delete test[0] you will be trying to deallocate a string literal, which is Undefined Behavior. Same for delete test[1].
    – Andy Prowl
    Feb 17, 2013 at 23:18
  • teacher of my girlfriend says that " it can be used only "iostream" - this is database with quicksort... btw code above compiled very well, but in debug mode i have error. And this memory leak. This is my last part of this application.
    – wcale
    Feb 17, 2013 at 23:22
1

Since "Test1" and "Test2" are used by your program at runtime (they are printed by cout), the compiler has to save them somewhere. It does this by putting them both into your executable.

Since they are both in your executable already, there is no reason to allocate any new memory. So you can remove the first two lines in data_t.

If you do this you will get a compiler error, though this error should have already been there, which will complain that you are trying to assign a string literal ("Test1", "Test2") to a non const array.

The problem here is that strings saved by the compiler into the executable are NOT to be modified. You are only printing them, but data_t doesn't know that. To fix the problem you should use const char * instead of char *

If you intend to modify these strings, you'll need to allocate new memory and copy the strings into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.