अभ्यास:
मैं वर्तमान में मेरा एक दोस्त सी की बुनियादी अवधारणाओं शिक्षण रहा हूँ - और यह बहुत ही सरल और सीधी-सपाट के साथ आया था (मुझे उम्मीद है) कोड उदाहरण जो मूल रूप से सबकुछ बताता है। आप लोगों से इसे छिपाना नहीं चाहते थे! :)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* function header definitions */
char* getString(); //<- with malloc (good practice)
char * getStringNoMalloc(); //<- without malloc (fails! don't do this!)
void getStringCallByRef(char* reference); //<- callbyref (good practice)
/* the main */
int main(int argc, char*argv[]) {
//######### calling with malloc
char * a = getString();
printf("MALLOC### a = %s \n", a);
free(a);
//######### calling without malloc
char * b = getStringNoMalloc();
printf("NO MALLOC### b = %s \n", b); //this doesnt work, question to yourself: WHY?
//HINT: the warning says that a local reference is returned. ??!
//NO free here!
//######### call-by-reference
char c[100];
getStringCallByRef(c);
printf("CALLBYREF ### c = %s \n", c);
return 0;
}
//WITH malloc
char* getString() {
char * string;
string = malloc(sizeof(char)*100);
strcat(string, "bla");
strcat(string, "/");
strcat(string, "blub");
printf("string : '%s'\n", string);
return string;
}
//WITHOUT malloc (watch how it does not work this time)
char* getStringNoMalloc() {
char string[100] = {};
strcat(string, "bla");
strcat(string, "/");
strcat(string, "blub");
//INSIDE this function "string" is OK
printf("string : '%s'\n", string);
return string; //but after returning.. it is NULL? :)
}
// ..and the call-by-reference way to do it (prefered)
void getStringCallByRef(char* reference) {
strcat(reference, "bla");
strcat(reference, "/");
strcat(reference, "blub");
//INSIDE this function "string" is OK
printf("string : '%s'\n", reference);
//OUTSIDE it is also OK because we hand over a reference defined in MAIN
// and not defined in this scope (local), which is destroyed after the function finished
}
जब यह संकलन, आप [इरादा] चेतावनी मिलती है:
[email protected]:~$ gcc -o example.o example.c
example.c: In function ‘getStringNoMalloc’:
example.c:58:16: warning: function returns address of local variable [-Wreturn-local-addr]
return string; //but after returning.. it is NULL? :)
^~~~~~
... मूल रूप से हम यहाँ चर्चा कर रहे हैं!
मेरी उदाहरण चल रहा यह उत्पादन पैदावार:
[email protected]:~$ ./example.o
string : 'bla/blub'
MALLOC### a = bla/blub
string : 'bla/blub'
NO MALLOC### b = (null)
string : 'bla/blub'
CALLBYREF ### c = bla/blub
सिद्धांत:
इस उपयोगकर्ता @phoxis से बहुत अच्छी तरह से जवाब दिया गया है। मूल रूप से इसके बारे में सोचो इस तरह से: सब कुछ inbetween { और }स्थानीय गुंजाइश है, इस प्रकार सी स्टैंडर्ड कर रहा है बाहर "अनिर्धारित" है। मॉलोक का उपयोग करके आप HEAP (प्रोग्राम स्कोप) से स्मृति लेते हैं और STACK (फ़ंक्शन स्कोप) से नहीं - इस प्रकार यह बाहर से 'दृश्यमान' है। ऐसा करने का दूसरा सही तरीका कॉल-बाय-रेफरेंस है। यहां आप पैरेंट-स्कोप के अंदर var को परिभाषित करते हैं, इस प्रकार यह स्टैक का उपयोग कर रहा है (क्योंकि मूल दायरा मुख्य() है)।
सारांश:
3 यह करने के लिए तरीके, उनमें से एक झूठी। सी एक फंक्शंस के लिए एक गतिशीलता आकार स्ट्रिंग वापस करने के लिए बेकार है। या तो आपको इसे मॉलोक करना होगा और फिर इसे मुक्त करना होगा, या आपको कॉल-बाय-रेफरेंस करना होगा। या सी ++ का उपयोग करें;)
के संभावित डुप्लिकेट [सी चेतावनी: समारोह रिटर्न स्थानीय चर का पता] (http://stackoverflow.com/questions/6897914/c-चेतावनी-फ़ंक्शन-रिटर्न-पता-स्थानीय-परिवर्तनीय) – netcoder
जब आप अपना प्रश्न लिखते हैं, तो यह इसके आधार पर कुछ डुप्लिकेट सुझाता है। आपको शायद उनको जांचना चाहिए था। – netcoder
मुझे लगता है कि यह है कि यह सहायक हो सकता है http://stackoverflow.com/a/6897993 –