Link to home
Start Free TrialLog in
Avatar of terajiv
terajiv

asked on

Char pointer Manupulation

When u declare a pointer to char and then in next line you initialize it then where it is stored?
Some times it gives Error and some times it works. Why it is so?

Ex.:
char *str;
str = "This in My Question";
 
ASKER CERTIFIED SOLUTION
Avatar of ScottyDawg
ScottyDawg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rbr
rbr

To ScottyDaw: Wrong char str[] doesn't delcare any memory too.

To terajiv:
This should work but is a little bit dangerous. Many compiler/OS use this a const char * and readonly memory and you are not able to change it. Better use

str = strdup ("This is My Question");
To ScottyDaw: Sorry. Your code is right but it won't work if you use

char str[];

str = "This is my question";

But if you use strdup you can use char *str;

Well I suppose the real question is do you need to allocate storage on the stack or the heap.

yes you can use strdup but the pointer returned has been malloc'd so remember you have to free it.
Avatar of terajiv

ASKER

My Actual Question was about Storage of memory... Where exactly it is stored.... I think ScottyDawg is going near to the answer. but still I need clarification about storage of memory..
Avatar of terajiv

ASKER

That statement is going to store the memory in stack it seems
char []="abdsbsb"; use the stack (i.g,)


char *str;
str=strdup("ananan"); use the heap (i.g.)
Avatar of ozo
char str[]="abdsbsb";  is static storage
Avatar of terajiv

ASKER

I got the Ans for 1st Que.
 Now can anybody pl. tell me why Some times it gives Error and some times it works?
can you post your full code, pls!
what error does it sometimes give?
If you code

double a_global = 3.1414926536
int f()
{
   char* str = "alkjsflkjsdfjasldjfsadlfjalsdjfljasdlfja"
  /* etc */
  strcat(str, "ouch!");   // will almost certainly fail
  strcpy(str, "ouch?");  // not garanteed to work
}

Then what happens in most compilers is that the character pointer str is stored on stack (it is an automatic) but the actual string ("alkjsflkjsdfjasldjfsadlfjalsdjfljasdlfja") is stored in a piece of memory the compiler reserves for statics (the global section). This is the same piece of memory that is used to store the double a_global.
This also means that the string exists during the whole lifetime of the program, although it is only is accessable in the function f.

Now, the exact way the compiler stores such strings is not part of the standard, that means, it is not garanteed that str can be treated as a regular piece of memory such as the heap, so any actions on the string that change the string are undefined. Thats the reason that in C++ this kind of definitions is preferably done as const char, although the upper definition is accepted for backwards compatibility issues:

const char* str = "this is ok"; // read only!!

By the way, notice that there is no difference between:

char *str  = "alksjflkasjfi";
char str[] =  "alksjflkasjfi";

these result in exacly the same assembler statements: str as auto and the string as global storage
---------------------------------------------------------------------------------------------
Two possible ways of defining storage for strings that is fully rewritable:

// This is my non-rewritable string in global storage
const char *my_string = "This is the string we want to be rewritable"

// allocate automatic stack storage for the string
char str[strlen(MY_STRING)+1];  // explicitly allocate stack storage
strcpy(str, MY_STRING);              // copy the string

// allocate heap storage for the string
char *str = strdup(MY_STRING); // from the heap, strdup allocates and copies
free(str);                                     // when you're done with the str

Luc
Avatar of terajiv

ASKER

I got the Ans... Thanx for it...
Actually When u declare
Char *str;
and initialize it with
str = "This is my string";

then if memory is available in stack it will store it into stack if it is not available then it will give run time error...
char *str;
 str = "This is my string";
by itself should not give a run time error
But what are you then doing with that *str?
trying to write to that *str might give an error.
There is no difference between:

char *str = "An example";

or

char *str2;
str2 = "Another example";

Both result in exactly the same assembly statements.
(i.e. pointer str, str2 as automatic on stack (4 bytes), and the string as global storage.)

Luc
Avatar of terajiv

ASKER

>>char *str;
>>str = "This is my string";

and
>>char *str = "An example";

are one and the same ur rt. Luc. But if there is no memory in stack it gives an error.
Avatar of terajiv

ASKER

>>char *str;
>>str = "This is my string";

and
>>char *str = "An example";

are one and the same ur rt. Luc. But if there is no memory in stack it gives an error.
What do you mean if there is no memory in stack?  and what error does it give?
Avatar of terajiv

ASKER

Imean if stack is full...then it gives run time memory referancing error....
If you're stack overflows all fails... not only the declaration, but also functioncalls, interrupts...
How do you know the stack overflows?
In any case, the string itself is not stored on stack.
Luc
Avatar of terajiv

ASKER

Hi Luc,

I thought the string is stored on stack So Can u pl. tell me where it is stored when I declare as:
char *str= "My String";
Actually my first Que was that only. pl. clarify...
If you define

char *str = "My String";

The pointer str is stored on stack (4 bytes in win32) and the character "My String" is stored in global memory together with all the other statics and globals.
There are three different memomry locations a program uses:
stack (automatics), heap (malloc) and global (statics)
Luc
Avatar of terajiv

ASKER

Thanx  LucHoltkamp
I was not knowing this...

Avatar of terajiv

ASKER

Thanx  LucHoltkamp
I was not knowing this...