Jump to content
  • Advertisement
Sign in to follow this  
xeddiex

Array and pointer question

This topic is 4976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Array names and Pointers are alike(?) Why can't you do this:
	char *name = "dog";
	name[0] = 'c';
	name[1] = 'a';
	name[2] = 't';
	name[3] = '\0';
        *Compiles fine but program crashes*

	//But can do this
	char name[4] = "dog";
	
	name[0] = 'c';
	name[1] = 'a';
	name[2] = 't';
	name[3] = '\0';

Also, True or Not: char *name = "dog";.. Allocates memory for name and then assigns the string "dog" so that means, later on I would have to free(name) because it was dynamically allocated. Or is it not dynamically allocated?

Share this post


Link to post
Share on other sites
Advertisement
No, you do not have to free the char *, simply because you did not use malloc(). It is not dynamically allocated, the compiler knows ahead of time the proper size and thus it is exactly the same as char [3]. This also applies to the null termination you are attempting. The compiler allocates enough space for "dog", but not for the null character, so yes, it will compile, but it will be a segmentation fault because you are reaching into memory that has not been allocated for use with that char *.

Hope that helped.

Share this post


Link to post
Share on other sites
In addition to what Alex said ...

Sytactically pointers and arrays are exactly the same. The only difference is that an array is a const pointer; you cannot reassign an array to point to a different location.

int* pointer;
int array[10];

pointer = array; /* okay */
array = pointer; /* ERROR!!! */

EDIT: After reviewing the original post, I realize my post has absolutely nothing to do with the question. Please ignore.

Share this post


Link to post
Share on other sites
char* is just a pointer...

new char[x]

reserves x memory at a pointer address dynamically. which you would have to

delete []

... later or you'll have a memory leak. Note the [] after delete - it's important when removing dynamic arrays.


When you do
char name[4] = "dog";

It's creating an automatic array of 4 and initalising each element with the string. Automatic variables delete themselves when they fall out of scope.



All array handles are pointers to memory - the first element of a contigous set of memory. Thus name+1 == 'o'

Share this post


Link to post
Share on other sites
The first example sets a pointer to your program's string table, which is arbitrarily stored (ie, you can't make any guarantees as to how or where they're stored; we usually expect it to be hard-coded at the top of the executable). You're overstepping the protected bounds of your own program, which is why you get the access violation.

Share this post


Link to post
Share on other sites
char *name means you create "an pointer to an char".

when you do:
char *name = "dog" you kind of create an array[] = { 'd', 'o', 'g' } and assign the adress of that array to name.

char name[4] = "dog"; is wrong .... but

char name[] = { 'd', 'o', 'g' }; is legal

char name[13] = { 0 }; will set 0 in every space of array.

using new and delete.

char *name = new char[ "dog" ];

delete [] name;

think thats right

Share this post


Link to post
Share on other sites
Quote:
Original post by DarkSlayer
think thats right
It's not. char *name = "dog"; doesn't create an array at all. It initializes a pointer to a static string literal. If you open up the binary in a hex editor, you'll actually see the string in there. char name[4] = "dog"; is correct; it initializes the elements of the array to 'd', 'o', 'g' and '\0'.

char name[] = { 'd', 'o', 'g' }; creates an undimensioned array, which is equivalent, believe it or not, to char *name = { 'd', 'o', 'g'}; and almost equivalent to char *name = "dog"; The difference? The former doesn't terminate in a null, meaning that you can't print the array as it will lead to an access violation.

There's a bad habit around here of not reading previous posters' comments, causing people to reiterate wrong answers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
char name[] = { 'd', 'o', 'g' }; creates an undimensioned array, which is equivalent, believe it or not, to char *name = { 'd', 'o', 'g'}; and almost equivalent to char *name = "dog"; The difference? The former doesn't terminate in a null, meaning that you can't print the array as it will lead to an access violation.

Actually, char name[] = { 'd', 'o', 'g' }; is equivalent to char name[3] = { 'd', 'o', 'g' };

Quote:
Original post by Oluseyi
There's a bad habit around here of not reading previous posters' comments, causing people to reiterate wrong answers.

Yeah, I notice people doing that a lot in other threads lately as well.

Edit: Also, technically he is correct that an array is created -- the type of a string literal in C++ is a const char array of the appropriate length. It's just that the array isn't managed by the scope of its use. As well, to DarkSlayer, char *name = new char[ "dog" ]; is not valid. You need to allocate the space for the array and copy the values in (or use a compiler extension).

Share this post


Link to post
Share on other sites
Quote:
The first example sets a pointer to your program's string table, which is arbitrarily stored (ie, you can't make any guarantees as to how or where they're stored; we usually expect it to be hard-coded at the top of the executable). You're overstepping the protected bounds of your own program, which is why you get the access violation.


...Here's where we're getting somewhere. I undertstand that char* name, is just a pointer that' can hold "only" address's...
Such as and only the first address of a char of a string and the rest are stored one by one contigiously to form a string.
That I get. What get's me confused is when working with pointers and arrays. Oluseyi, said what I was looking for but I don't quite get how I'm "overstepping the protected bounds of your own program", could you elaborate a little bit more on this please? It would also be very helpful "if" you could somehow do a little diagram showing how it works.

Thanks everyone for your input. If you have anything else to add, please do so.


Share this post


Link to post
Share on other sites
Quote:
Original post by xeddiex
Quote:
The first example sets a pointer to your program's string table, which is arbitrarily stored (ie, you can't make any guarantees as to how or where they're stored; we usually expect it to be hard-coded at the top of the executable). You're overstepping the protected bounds of your own program, which is why you get the access violation.


...Here's where we're getting somewhere. I undertstand that char* name, is just a pointer that' can hold "only" address's...
Such as and only the first address of a char of a string and the rest are stored one by one contigiously to form a string.
That I get. What get's me confused is when working with pointers and arrays. Oluseyi, said what I was looking for but I don't quite get how I'm "overstepping the protected bounds of your own program", could you elaborate a little bit more on this please? It would also be very helpful "if" you could somehow do a little diagram showing how it works.

Thanks everyone for your input. If you have anything else to add, please do so.



Your program is given a very specific part of memory. This memory is for your program only, and is given by the OS. This set of memory is entirely based off of the variables you declare and use. When you dynamically allocate memory, you are asking the OS for more memory. When you use char * str = "dog" hard-coded, the compiler notices this, and puts in char str[3] = "dog" (in essence).

What this means is, you have only been allocated a set amount, and when you try to run the program, you are reaching beyond this line into some other program's memory (maybe no other program..but can be). The OS won't let that happen, so it crashes.

The above is different than char str[4] = "dog" because now the compiler allocated enough memory for 4 characters (4 bytes), and you are able to put the null character on the end. Before, the compiler allocated 3 bytes and you were trying to reach for a fourth, which was not your program's memory, thus the error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!