Question about odd syntax: char* c = {"Text"}; | | |
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 | | | | re: Question about odd syntax: char* c = {"Text"};
Bruce Adcock wrote:[color=blue]
> 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.[/color]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: Question about odd syntax: char* c = {"Text"};
Bruce Adcock <bruce.adcock@gmail.com> wrote:[color=blue]
> 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)]";[/color]
This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
[color=blue]
> strcpy (temp_pointer, temp_literal);
> const char* c = temp_pointer;[/color]
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";
?
--
Marcus Kwok
Replace 'invalid' with 'net' to reply | | | | re: Question about odd syntax: char* c = {"Text"};
Victor Bazarov posted:
[color=blue]
> The Standard says
>
> "If T is a scalar type, then a declaration of the form
> T x = { a };
> is equivalent to
> T x = a;
> "[/color]
Didn't know that... it might come in handy for templates.
-Tomás | | | | re: Question about odd syntax: char* c = {"Text"};
Marcus Kwok wrote:[color=blue]
> Bruce Adcock <bruce.adcock@gmail.com> wrote:[color=green]
>> 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)]";[/color]
>
> This is obviously not real code because of the syntax error, but be
> aware that in real code you need strlen() + 1.
>[color=green]
>> strcpy (temp_pointer, temp_literal);
>> const char* c = temp_pointer;[/color]
>
> 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";
>
> ?[/color]
'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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: Question about odd syntax: char* c = {"Text"};
Marcus Kwok wrote:[color=blue]
> Bruce Adcock <bruce.adcock@gmail.com> wrote:[color=green]
>> 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)]";[/color]
>
> This is obviously not real code because of the syntax error, but be
> aware that in real code you need strlen() + 1.
>[color=green]
>> strcpy (temp_pointer, temp_literal);
>> const char* c = temp_pointer;[/color]
>
> 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";
>
> ?
>[/color]
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 | | | | re: Question about odd syntax: char* c = {"Text"};
Bruce Adcock wrote:[color=blue]
> char temp_literal[] = "<?xml";
> char* temp_pointer = new char[strlen (temp_literal)]";
> strcpy (temp_pointer, temp_literal);
> const char* c = temp_pointer;
>[/color]
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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|