char and general mem allocation.

Hi, I was always wondering about this.

As far as I understand, using char *a = new char;

allocates one block of memory for a char, and not more.

Then, why, if I then do:

a = "some_string";

does it actually work. Shouldn't this give a buffer overflow?

In other words, if I have a text string (not an std::string, though), I do need to use char a[n] = "some_text", and above code, using just a single char, is not really valid, or, will, at least generate a memory leak, even though small. is that correct?
Last edited on
"some_string" is a const array placed in RO data part of the memory.

when:
a = "some_string";
you assign it's pointer to a, so a point to const data, not allocated memory with new char.

Also in that case you would lose address of newly allocated memory which in the end would result in memory leak.

1
2
3
4
5
6
char *a;

a = new char;
cout << hex << (uint32_t)a << endl;
a = "asda";
cout << hex << (uint32_t)a << endl;
Last edited on
Ok, so, then I have two questions.

a):

when I do:

1
2
3
4
5
6
7
8
9

char *a;

a = new char;

a = "a";

a = "b";


do, I need to place a

1
2

delete a;


in between the assignment of a to "a" and a to "b", in order to free up the memory used by the string literal "a", that was previously pointed to by char *a? Else: memory leak?

In other words: do I need to place a delete before EVERY new assignment of char *a? Or do I only need to use delete to free up memory allocated by new.


and, b):

then, I also take it, that, actually, using

1
2
char *a = "some_string";


is also valid, since I am allocating memory for one pointer (to anything, probably, but, in this case, a char), and this pointer can point to any char in memory, whether it is followed by more chars, or immediately has a null terminator in the next mem block?

Or, is it really only valid to assign a pointer to ONE SINGLE string literal to a pointer to a single char.

I.e., basically:

is
1
2

char *a = "abc";


just as valid as

 
char *a = "a";


or, is only the later valid?

Thanks for your help!! :)
Last edited on
in a) you are assigning constant variables "a" and "b" addresses to pointer. You should only add delete to memory allocated with new:
1
2
3
4
5
6
7
char *a;

a = new char;
delete a;

a = "a";
a = "b";


b)
1
2
char *a = new char;
a = "some_string";

is not valid, because new return pointer to newly allocated memory. If you will lose address to that memory you won't be able to delete it because you simply wont know where it is.

1
2
char *a = "asd";
char b[] = "asd2";

both are the same thing for compiler

ps: you might not understand what "" mean. "" is a string value with end-of-line \0 at the end. '' is a single byte char value.
1
2
3
4
5
char *a;
a = new char;
*a = 'b'; //assign 'a' char to memory pointed by a
delete a;
a = "a"; // assign array {'a','\0'}address to pointer a 
Last edited on
Ah, ok, that's what I thought, so, I don't need to delete every pointer I create, only the ones that I have pointing to memory allocated by "new".

Thanks, that was a great help! and, I think, one of the last things I was Really unclear on!

Topic archived. No new replies allowed.