• Forum
  • Lounge
  • Codeblocks creating problems (Pointer ar

 
Codeblocks creating problems (Pointer arithmetic)

Am I the only one who is facing Code Blocks crashes frequently. I have reinstalled it many times . But still , the problem occurs sometimes.

Here is an example :
1
2
char *p = "Hello";
*p = 'M'; //this line crashes the program(Not Code Blocks ). 

This program is perfectly OK. But still , the program crashes.

Another:
Sometimes when I click on workspace , the background is faded and then Code Blocks crashes.

Is anyone facing the same problems ?
If you have any suggestions , please help .
Thanks

char *p = "Hello";
warning: deprecated conversion from string constant to ‘char*’
Last edited on
I know, it gives a warning but this should not crash the program . I am downloading CodeBlocks and change the settings. Let's see what happens.
thanks for replying.
"Hello" is a string literal in C++. A string literal is not to be changed.

Trying to change it, as this code above does, is undefined1. In your case, it seems that the result is a crash.

This program is perfectly OK.

To summarise, that is not true.


-------

1. ISO C++11 Standard, Section 2.14.5.12 : "... The effect of attempting to modify a string literal is undefined."
Last edited on
Try this: const char *p = "Hello";
closed account (3hM2Nwbp)
Another:
Sometimes when I click on workspace , the background is faded and then Code Blocks crashes.


I experienced this issue back when I used code blocks. When I went to report the bug, the only response that I got was along the lines of "you have the source, fix it". If this bug is still around, it's most likely not a trivial fix...either that or the maintainers do not feel that it's a serious enough bug to fix.
Last edited on
C::B hasn't crashed in ages for me (not counting one particular bug that happens after a few hours).
It's likely that the bug was fixed a long time ago. You just need to make sure that you always have the latest version.
You just need to make sure that you always have the latest version.

Translation: Get the nightly builds from the forums. The so-called "stable" releases are updated very, very infrequently.
I have installed the latest version. Code Blocks 10.05.
Still the same problem .
So I tried the same code on different machines.

On Win7:
Using CodeBlocks : Program crashes.
Using notepad++ with g++ : Program still crashes.
Using TurboC++ : Displays expected output.(Mello)

On WinXP:
Using Codeblocks : Displays blank output.

Actual code tried :
1
2
3
4
5
6
7
#include<iostream>
using namespace std ;
int main() {
char *p = "Hello";
*p='M';
cout << p ;
}


@Catfish2
this gives compilation error: assignment of read only location '*p'.

@ Raman009: That's the point!

Do you understand that "Hello" is a string literal and you only store its memory address in a pointer?

1
2
3
char s[] = "Hello"; // char array with content `Hello`
char *p1 = "Hello"; // pointer to string literal constant `"Hello"`, and so it should be:
const char *p2 = "Hello";


Good old C language has "compound literals", but a C++ compiler may or may not support them (because after all, C and C++ are two different languages):

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main(void)
{
	char *p = (char[]){"Hello"};

	*p = 'Y';
	printf("%s\n", p);
}

Code::bocks 10.05 was released two years ago, there's a new version coming out "soon", but until then you need to use the nightly builds

http://forums.codeblocks.org/index.php?&topic=16619.0

that's the newest one so far, just copy/pasta the files into your CB 10.05 directory.


there's an unofficial installer for the latest CB nightlies, really kewl :D

http://forums.codeblocks.org/index.php/topic,13234.0.html
Last edited on
To put in simple terms, this:
1
2
3
char *p = "Hello";
*p = 'M';
std::cout <<"Hello"; //Guess the output. 
can be compared to doing this:
1
2
3
int *p=&1024;
*p=42;
std::cout <<1024; //Guess the output. 
The only difference is that the latter doesn't even compile.

PS: This isn't pointer arithmetic.
Last edited on
Topic archived. No new replies allowed.