Jump to content
  • Advertisement
Sign in to follow this  
ManaStone

Is there a problem with doing this?

This topic is 4349 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

say you write the function: void textFunc(char text[]); and then you call the like this: textFunc("Hello World!"); Is that going to cause any problems with overwriting memory?

Share this post


Link to post
Share on other sites
Advertisement
(Assuming I remember my specs)
Eh, no. But certain uses of that string within textFunc will lead to badness.

Make the parameter const and there shouldn't be any problems, I think.

Share this post


Link to post
Share on other sites
What exactly happens when you do that though? Is pointer space allocated for that function call and then destroyed after it is finished?

Share this post


Link to post
Share on other sites
No. If I remember correctly, Type VarName[] is a legal, but less common synonym for Type *VarName and behaves identically. (double check: yes, K&R use this syntax quite a bit for pre-allocated char arrays of arbitrary length)

Share this post


Link to post
Share on other sites
array syntax for parameters to functions is 100% identical to pointer passing, unlike array syntax for variable declarations ... read this:

void textFunc(char text[]);

as:

void textFunc(char *text);

Share this post


Link to post
Share on other sites
What part of the memory is the "hello world!" part located though?

My memory about this is a little rusty. Is the memory allocated for each function called a stack?

Share this post


Link to post
Share on other sites
Each function has its own local variables on the top of the stack.

In this case, the "Hello World" string is probably not given its own space each time you call the function.

Instead it is placed in some special read-only section of the executable and your program just passes a pointer to it. Try changing the first character and see if it crashes.

Share this post


Link to post
Share on other sites
I think string literals are stored in some static type of storage. So when you pass a string literal to a function, it is passing a pointer to wherever the string is actually stored.

The documentation for the GCC says that string literals are stored in read-only storage. I think it does that so it can reuse the same memory if more than one string literal are the same. Other compilers might be different, I'm not sure.

Share this post


Link to post
Share on other sites
The type of a string literal in C++ source is defined as const, so the compiler is free to and normally would store this in read-only data segment of your program.

So really, if you are passing it into a function your parameter should be (const char[]) rather than (char[]). My compilers would issue warnings if I omitted the const then passed in a literal string (I think).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!