Release version crash!!! Only changed a declaration

  • Thread starter Thread starter dotNeter
  • Start date Start date
D

dotNeter

My source code declared two types of string,

static char* str = "hello, world";
static char str1[] = "hello, world";

The only difference between them is the memory allocation while
compiling time.
And the first one can't be modified through the pointer, whereas the
second one can.

The debug version works well but the release one must crash, at the
time that it trigered an operation which need the content of string.

If I modified the declaration char* to char[], even release version is
okay.
I can't exactly know why and how to solve it, but it was supposed to be
a segmentation error, namely, likely to be a pointer error.

Can someone give a hint?
 
static char* str = "hello, world";
this will only allocate a pointer which will be pointing to a global string
literal;
static char str1[] = "hello, world";
This will allocate a local buffer and initialize it with the specified text.
the buffer itself is not a global string literal. this is important because
here you are allowed to
change the contents, while you are not allowed to change a global string
literal.

One of the possible reasons that you have problems is that some function
is messing with the global string literal before you try to read it.
Check all accesses to the string literal to see what is happening.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
dotNeter said:
My source code declared two types of string,

static char* str = "hello, world";
static char str1[] = "hello, world";

The only difference between them is the memory allocation while
compiling time.
And the first one can't be modified through the pointer, whereas the
second one can.

The debug version works well but the release one must crash, at the
time that it trigered an operation which need the content of string.

If I modified the declaration char* to char[], even release version is
okay.
I can't exactly know why and how to solve it, but it was supposed to be
a segmentation error, namely, likely to be a pointer error.

Can someone give a hint?

dotNeter:

What if you do

static const char* str = "hello, world";

? I'm guessing that you will have a compile error somewhere. Assigning
string literal to char* is deprecated, and only allowed for backward
compatibility with C.

David Wilkinson
 
David:

I quite understood what you said. This is an obvious C-style
declaration.

static const char* str = "hello, world";
It was okay, no compiling error.

The most unknown thing is I don't know the compiler optimized what.

For the usage of those strings, I'm sure the program wasn't intended
to modify them. They are read-only strings.

David said:
dotNeter said:
My source code declared two types of string,

static char* str = "hello, world";
static char str1[] = "hello, world";

The only difference between them is the memory allocation while
compiling time.
And the first one can't be modified through the pointer, whereas the
second one can.

The debug version works well but the release one must crash, at the
time that it trigered an operation which need the content of string.

If I modified the declaration char* to char[], even release version is
okay.
I can't exactly know why and how to solve it, but it was supposed to be
a segmentation error, namely, likely to be a pointer error.

Can someone give a hint?

dotNeter:

What if you do

static const char* str = "hello, world";

? I'm guessing that you will have a compile error somewhere. Assigning
string literal to char* is deprecated, and only allowed for backward
compatibility with C.

David Wilkinson
 
Back
Top