473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

char * !!

well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
{
char *ss;
ss = foo()
}
a. is p statically allocated memory in
1>. C++
2>. C

b. if no, then is this undefined behaviour ??

thanking all those who might answer this ,
maadhuu.

Jul 23 '05 #1
10 1651
maadhuu wrote:
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
{
char *ss;
ss = foo()
}
a. is p statically allocated memory in
1>. C++
2>. C
p is an auto variable. It is not statically allocated.

b. if no, then is this undefined behaviour ??


The string literal "hello" is statically allocated.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

"maadhuu" <ma************@yahoo.com> wrote in message
news:f9******************************@localhost.ta lkaboutprogramming.com...
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
{
char *ss;
ss = foo()
not compileable, the preceding line is not a statement.
}
a. is p statically allocated memory in
1>. C++
2>. C

b. if no, then is this undefined behaviour ??

thanking all those who might answer this ,
maadhuu.


Jul 23 '05 #3
maadhuu wrote:
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
main() must have a return type.
{
char *ss;
ss = foo()
;
}
a. is p statically allocated memory in
1>. C++
2>. C
This is the same in C and C++. In both cases, p is stored in the automatic
storage, not the static one. However, note that p is only a pointer.
b. if no, then is this undefined behaviour ??


No. The pointer is returned by value, so it gets copied into main.
I guess what you actually wanted to know is whether the string literal that
p points to is statically allocated. The answer to that would be yes.
Jul 23 '05 #4
The following code can output "hello".
__________________________________________________ _
#include <stdio.h>
char* foo()
{
char * p="hello";
//do something.
return p;
}
int main()
{
char *ss;
ss = foo();
printf("%s\n",ss);
}
__________________________________________________ _

maadhuu wrote:
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
{
char *ss;
ss = foo()
}
a. is p statically allocated memory in
1>. C++
2>. C

b. if no, then is this undefined behaviour ??

thanking all those who might answer this ,
maadhuu.

Jul 23 '05 #5
I know the code works with many compilers, but what do the ANSI I
and C++ specifications say?

The string "hello" is statically allocated, but it's statically allocated
within a *function*; and AFAIK functions are *dynamically* allocated.
If this is the case, then the pointer that main() gets back from foo() might
even be NIL!

Does anyone know the answer to maadhuu's question? I'd also be interested.

Cheers Simon

Raymond wrote:
The following code can output "hello".
__________________________________________________ _
#include <stdio.h>
char* foo()
{
char * p="hello";
//do something.
return p;
}
int main()
{
char *ss;
ss = foo();
printf("%s\n",ss);
}
__________________________________________________ _

maadhuu wrote:
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
{
char *ss;
ss = foo()
}
a. is p statically allocated memory in
1>. C++
2>. C

b. if no, then is this undefined behaviour ??

thanking all those who might answer this ,
maadhuu.


Jul 23 '05 #6
Simon wrote:
I know the code works with many compilers, but what do the ANSI I
and C++ specifications say?
What's I?
The string "hello" is statically allocated, but it's statically allocated
within a *function*;
Either it's statically allocated or it is not. Whether that's in a function
or not doesn't matter.
and AFAIK functions are *dynamically* allocated.
There are three kinds of memory in standard C++. Static, dynamic and
automatic. Non-static local variables in a function are automatic. Dynamic
memory is memory allocated by new or the *alloc family.
If this is the case, then the pointer that main() gets back from foo()
might even be NIL!

Does anyone know the answer to maadhuu's question? I'd also be interested.


Yes. String literals are statically allocated. They live until the program
ends. So IOW the example below has a well-defined behavior.
The following code can output "hello".
__________________________________________________ _
#include <stdio.h>
char* foo()
{
char * p="hello";
//do something.
return p;
}
int main()
{
char *ss;
ss = foo();
printf("%s\n",ss);
}


Jul 23 '05 #7


maadhuu wrote:
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p;
}
main()
{
char *ss;
ss = foo()
}
a. is p statically allocated memory in
1>. C++
2>. C
No, p itself is in stack, but the memory that p pointed to is in data
segment which is always available. b. if no, then is this undefined behaviour ?? No. But it would be better if 'ss' is 'const char*'.
Because your main() cannot modify that memory(it's read-only).
thanking all those who might answer this ,
maadhuu.


Jul 23 '05 #8


Simon schreef:
I know the code works with many compilers, but what do the ANSI I
and C++ specifications say?

The string "hello" is statically allocated, but it's statically allocated
within a *function*; and AFAIK functions are *dynamically* allocated.


As far as C++ cares, functions are not allocated at all. They exist
before
any object exists, and are not destroyed. In fact, on many embedded
systems,
compiled C++ code runs from ROM. That's memory allocated by the linker!

In any case, "hello" is a char[6] which exists even before the function
containing it is called. Of course you cannot determine *how* much
earlier it is created. ISO C++ offers no way to peek into a function
without calling it.

HTH,
Michiel Salters

Jul 23 '05 #9
Shouldn't this be returning a pointer to const? Whatever else is
happening, you sure don't want the caller to think it can modify the
content of the string literal.

Bill

Raymond wrote:
The following code can output "hello".
__________________________________________________ _
#include <stdio.h>
char* foo()
{
char * p="hello";
//do something.
return p;
}
int main()
{
char *ss;
ss = foo();
printf("%s\n",ss);
}
__________________________________________________ _

maadhuu wrote:
well,consider this function foo which is called from main.
char* foo()
{
char * p="hello";
//do something.
return p; }
main()
{
char *ss;
ss = foo()
}
a. is p statically allocated memory in 1>. C++
2>. C

b. if no, then is this undefined behaviour ??

thanking all those who might answer this ,
maadhuu.

Jul 23 '05 #10
>> Shouldn't this be returning a pointer to const? Whatever else is
happening, you sure don't want the caller to think it can modify the
content of the string literal.


As far as I can remember, assigning string literals to non-const
pointers is a leftover from C and, although technically legal, it's not
very good practice. If the caller attempts to modify the memory pointed
to by the returned pointer then the behaviour is undefined. If you
instead declare the local pointer p to be const, then the return type
and main's copy also have to be const and the problem disapears.

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.