PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Interesting char *

I've recently been having issues with chars...
I have the following struct

struct player_attribs {
char *name;
};

there is other stuff in it as well, anyhow....
I then:

player_attribs player[20];

do I still have to

player[5].name = new char[10] = "1234567890";

or not?

it "seems" I can just:

player[5].name = "fred";

your comments?
you still have to do it.
Possibly your code is going very bad somewhere and the char *name structure is pointing to a part of memory you're using (ee... one of the player_attribs struct, maybe?:-)
So each time you save something there you're wildly writing all over this structure... or maybe somewhere else.. god knows... just don't do it;-)
you have to either have something like

char name[256];
char *namepointer;
namepointer=name;

or like you said, use new/malloc/whatever


btw think about it a little.. you create a pointer without giving size and your poor PPC should know how large your array is? :-)
Dan East · 24 Jan 2003, 04:52 UTC — Interesting char *
brendan wrote:
player[5].name = new char[10] = "1234567890";


That is wrong, and shouldn't even compile. Think of "new char[10]" as a function returning a value. You can't assign something to it. Next, you aren't allocating an extra character for the null terminator. You need one more than the number of characters in the string.

This is correct:
player[5].name = new char[11];
strcpy(player[5].name, "1234567890");

If you do something like this:

player[5].name = new char[11];
player[5].name = "1234567890";

Then you have a memory leak. You just threw away the pointer returned by "new char[11]", as opposed to modifying that chunk of memory.

I would just make the struct player_attrib contain a char array, so you predefine a max length a player name can be. Then strcpy the names in.

struct player_attribs {
char name[16];
};

player_attribs player[20];

strcpy(player[5].name, "Default Name");

Dan East
andys · 24 Jan 2003, 11:20 UTC — Interesting char *
Dan East wrote:
I would just make the struct player_attrib contain a char array, so you predefine a max length a player name can be. Then strcpy the names in.

struct player_attribs {
char name[16];
};

player_attribs player[20];

strcpy(player[5].name, "Default Name");

Dan East


Unless he's doing input maxed to 15 characters he should use strncpy and add a terminating null or something horrible can result (or at least it would look hideously unprofessional if somebody entered something bigger

#define NAME_LENGTH 16
struct player_attribs {
char name[NAME_LENGTH];
};

player_attribs player[20];

strncpy(player[5].name, "Default Name",NAME_LENGTH);
player[5].name[NAME_LENGTH-1] = '\0';

Dunno, there is a strncpy in WinCE right? (at work atm)
hmmm, yes Dan, you could be right... I didn't test the new char[5] = "12345"; idea etc....

I was having issues when pre defining them to a size...

i.e the command

player[5].name = "12345"; failed, because cant assign char[5] to *char[5] which now makes perfect sence.... lol

but the code was "working" by just leaving it as char *name

then just saying

player[5].name = "fred";

but I think I'll just "fix it" to the prefered method... and yes the null (\0) is a valid point....

it could be the the point was just renamed to the "name"?
brendan wrote:
but the code was "working" by just leaving it as char *name

then just saying

player[5].name = "fred";


It would work because its valid, however unless the string "fred" was global, that space would be cleaned up after you left that function.

If by chance you were doing something like this:
char buf[32];
<get player name and put it in the buffer>
player[5].name = buf;

I'd just go with the fixed size array for the name, although 16 is a bit lean. And then just prevent going over that size in the UI.
Code:
player[5].name = "fred";


Isn't that the same as:

Code:

//
SomeFunction(_T("HELLO!!!"));
//

void SomeFunction(TCHAR* str)
{
//str now points to the string literal.
}


By doing what you are currently doing player[].name is pointing to the string literal you 'hard-coded', which is fine, but you wont be able to change it without allocating memory for the string. Also as sm!rk mentioned, the value will be lost on the function return (unless it is static).

If you are not sure about what is happening to your variables, why not step through your code in the debugger using the Variable Watch. You'll be able to see exactly what is going on.

Hope this helps...
You could always just allocate it dynamically or roll your own string class or even use the MFC string class or STL string class. Takes care of all the memory management and lets you treat strings half-hazardly.
Sm!rk":2gx5wrz8]
[quote="brendan wrote:

but the code was "working" by just leaving it as char *name

then just saying

player[5].name = "fred";


It would work because its valid, however unless the string "fred" was global, that space would be cleaned up after you left that function.



String constants are always "global". They are typically allocated in their own read-only portion of memory when the executable is loaded. So the above code should not be able to cause the problem you mention.

This code would however, as in this case the characters will be stored on the stack of the function and thus "disappear" when the function returns.

Code:

void somefunction()
{
char s[] = "fred";

player[5].name = s;
}


/Orpe