Question about odd syntax: char* c = {"Text"};

B

Bruce Adcock

Hi,

I have run across the some strange syntax, to which I do not
understand the meaning:

const char* xmlHeader = {"<?xml"};

(specifically, this can be found in "tinyxmlparse.cpp", Line 828, from
the "tinyxml" project on Sourceforge at
<http://sourceforge.net/projects/tinyxml/>). I should note that this
does compile with gcc v4.0.1.

Originally, I thought this syntax would only make sense if we were
dealing with char*[] or char**. My second interpretation was that this
notation essentially dynamically allocates an array of chars,
initialized with "<?xml". However, this does not seem to hold, based on
the following test.

#include <iostream>

using namespace std;

int main ()
{
const char* a = "Test 1 has address ";
char* b = new char[12];
const char* c = {"Test 3 has address "};
char d[] = "Test 4 has address ";
char e[12];

cout << "a: " << a << ((const void *) a) << endl;
cout << "b: Test 2 has address " << ((const void *) b) << endl;
cout << "c: " << c << ((const void *) c) << endl;
cout << "d: " << c << ((const void *) d) << endl;
cout << "e: Test 5 has address " << ((const void *) e) << endl;

// delete [] c;
// delete c;
}

In my particular instance, I get

a: Test 1 has address 0x2eb0
b: Test 2 has address 0x500150
c: Test 3 has address 0x2ec4
d: Test 3 has address 0xbfffed84
e: Test 5 has address 0xbfffed98

This implies to me that the {}s do not actually do anything, as both a
and c do not appear to be pointing at something on the heap (like d and
e) or on the stack (like b). That can be further supported by
uncommenting either one of the delete's at the end, which gives me

malloc: *** Deallocation of a pointer not malloced: 0x2ec4; This
could be a double free(), or free() called with the middle of an
allocated block; Try setting environment variable MallocHelp to see
tools to help debug

My question is, does this notation actually serve any purpose? And while
I am at it, is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;

Thank you for any help,
Bruce
 
V

Victor Bazarov

Bruce said:
I have run across the some strange syntax, to which I do not
understand the meaning:

const char* xmlHeader = {"<?xml"};

(specifically, this can be found in "tinyxmlparse.cpp", Line 828, from
the "tinyxml" project on Sourceforge at
<http://sourceforge.net/projects/tinyxml/>). I should note that this
does compile with gcc v4.0.1.

The Standard says

"If T is a scalar type, then a declaration of the form
T x = { a };
is equivalent to
T x = a;
"

HTH

V
 
M

Marcus Kwok

Bruce Adcock said:
is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";

This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;

Since you have a const char*, this indicates that you do not intend to
change the chars that are pointed to, so why not just

const char* c = "<?xml";

?
 
T

Tomás

Victor Bazarov posted:

The Standard says

"If T is a scalar type, then a declaration of the form
T x = { a };
is equivalent to
T x = a;
"


Didn't know that... it might come in handy for templates.


-Tomás
 
V

Victor Bazarov

Marcus said:
Bruce Adcock said:
is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";

This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;

Since you have a const char*, this indicates that you do not intend to
change the chars that are pointed to, so why not just

const char* c = "<?xml";

?

'c' itself is probably intended to stay as is, as well, so

const char* const c =

is better, and if that's so, then

const char c[] =

is the best, IMO.

V
 
B

Bruce Adcock

Marcus said:
Bruce Adcock said:
is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";

This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;

Since you have a const char*, this indicates that you do not intend to
change the chars that are pointed to, so why not just

const char* c = "<?xml";

?

Hi,

Sorry about the errors (and thanks also to Victor Bazarov for the
quick reply). The second question mainly came about after trying to
understand the intent of the original code, and is more theoretical than
anything in particular I am trying to do right now. I _would_ however be
concerned about intermixing pointers to literals with pointers to
pointers to dynamic arrays, especially when it comes to using delete [].

Thanks a lot,
Bruce
 
S

SuperKoko

Bruce said:
char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;
Note that you should allocate 1 byte more...

Yes, there is a "special" notation for that... The wonderful concept of
procedural programming is more than 40 years old...
char* DuplicateString(const char* source) {
std::size_t size=strlen(source);
char* const buffer=new char[size+1]
memcpy(buffer, source, size+1); // copy all bytes of the string,
including the null terminator.
return buffer;
}

Now, you just have to do:

char* NewString=DuplicateString("hello world");
// And don't forget to release the memory (and catch the potential
std::bad_alloc exception)
delete[] NewString;

Of course, it is even better to use more modern technologies such as
std::string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top