* signed/unsigned char
@ 2005-07-20 14:46 Milena Constantino Caires
2005-07-20 14:55 ` Eljay Love-Jensen
2005-07-20 14:59 ` random
0 siblings, 2 replies; 3+ messages in thread
From: Milena Constantino Caires @ 2005-07-20 14:46 UTC (permalink / raw)
To: gcc-help
I would like to know why the following statements do not generate a
error during compiling but they do generate segmentation fault and
core dump when the program is running:
char* c = new char(1024);
or
u_char* c = new u_char(1024);
I know that the correct form of this memory allocation is "[1024]" and not
"(1024)" but sometimes it occurs by mistake.
The compiler is not supposed to complain about it???
Thanks in advance,
Milena
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: signed/unsigned char
2005-07-20 14:46 signed/unsigned char Milena Constantino Caires
@ 2005-07-20 14:55 ` Eljay Love-Jensen
2005-07-20 14:59 ` random
1 sibling, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2005-07-20 14:55 UTC (permalink / raw)
To: Milena Constantino Caires, gcc-help
Hi Milena,
> char* c = new char(1024);
This allocates a single character, initialized with the value 1024. (Since 1024 is bigger than what a char can hold on most systems, it will get sliced.)
>I know that the correct form of this memory allocation is "[1024]" and not "(1024)" but sometimes it occurs by mistake.
Both forms are correct.
You can allocate an array of char.
You can allocate a single char.
>The compiler is not supposed to complain about it???
No, there is nothing to complain about. You are allowed to allocate a single object.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: signed/unsigned char
2005-07-20 14:46 signed/unsigned char Milena Constantino Caires
2005-07-20 14:55 ` Eljay Love-Jensen
@ 2005-07-20 14:59 ` random
1 sibling, 0 replies; 3+ messages in thread
From: random @ 2005-07-20 14:59 UTC (permalink / raw)
To: Milena Constantino Caires; +Cc: gcc-help
Milena Constantino Caires wrote:
>I would like to know why the following statements do not generate a
>error during compiling but they do generate segmentation fault and
>core dump when the program is running:
>
>
>char* c = new char(1024);
>
>or
>
>u_char* c = new u_char(1024);
>
>I know that the correct form of this memory allocation is "[1024]" and not
>"(1024)" but sometimes it occurs by mistake.
>
>The compiler is not supposed to complain about it???
>
>
>
Because what you have written is that you want a new char whose value is
initalised to 1024. For example write:
To convince yourself this is true, try for example:
#include<iostream>
int main(void) {
char* c = new char(65);
std::cout << *c << std::endl;
}
Which should almost certainly print A, unless you are on a very strange
machine (65 is the ASCII value for A).
I'll admit I'm slightly suprised that the compiler doesn't warn you that
1024 is out of range for a char (on the other hand, the code is still
valid I believe if the char is unsigned).
Unfortunatly I don't know of a good way to catch these kinds of errors,
other than of course a tool like valgrind.
Chris
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-07-20 14:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-20 14:46 signed/unsigned char Milena Constantino Caires
2005-07-20 14:55 ` Eljay Love-Jensen
2005-07-20 14:59 ` random
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).