hi,

#include <stdio.h>

int* get_int()
{
	int arr[100];
	arr[0] = 5;
	printf("arr %p\n",arr);
	return arr;
}

int main()
{
	int* p = get_int();
	printf("got %p\n",p);
	return 0;
}

for the code above I get this warning message:

test-heap.c: In function ‘int* get_int()’:
test-heap.c:5: warning: address of local variable ‘arr’ returned

My question is whether this above approach is right or wrong. ie getting the storage allocation for the pointer in main function done indirectly through the function get_int. If I call get_int repeatedly in a loop then is it necessary to free p before the beginning of each iteration as for different iteration it is supposed to get different allocation but I am not sure what happens to the previous allocation. Is there any possibility of memory leak? Suppose I allocated a pointer in get_int instead of creating an array then would that be treated differently from within main I mean allocation/deallocation(calling malloc/free) wise?

Thanks.

hi,

#include <stdio.h>

int* get_int()
{
	int arr[100];
	arr[0] = 5;
	printf("arr %p\n",arr);
	return arr;
}

int main()
{
	int* p = get_int();
	printf("got %p\n",p);
	return 0;
}

for the code above I get this warning message:

test-heap.c: In function ‘int* get_int()’:
test-heap.c:5: warning: address of local variable ‘arr’ returned

My question is whether this above approach is right or wrong. ie getting the storage allocation for the pointer in main function done indirectly through the function get_int. If I call get_int repeatedly in a loop then is it necessary to free p before the beginning of each iteration as for different iteration it is supposed to get different allocation but I am not sure what happens to the previous allocation. Is there any possibility of memory leak? Suppose I allocated a pointer in get_int instead of creating an array then would that be treated differently from within main I mean allocation/deallocation(calling malloc/free) wise?

Thanks.

Its wrong because the local variable 'int arr[100];' is only current/valid until the function returns.

Its wrong because the local variable 'int arr[100];' is only current/valid until the function returns.

Okay but sometimes it works and sometimes not. Why?

For instance this code fragment:

#include <stdio.h>
#include <string.h>

char* func()
{
	char arr[50];
	arr[0] = '1';
	arr[1] = '2';
	arr[2] = '3';
	arr[3] = '\n';
	arr[4] = '\n';
	arr[5] = '\0';

	arr[3] = '\0';
	//printf("arr %s\n",arr);

	return arr;
}

int main()
{
	char* p = func();
	printf("p: %s\n",p);
	return 0;
}

if the line is uncommented then the output is
arr: 123
p: 123

if the line is commented then the output is:
p:

Why is it sometimes work and not work in other times? Besides if this is the wrong approach then what would be the right approach? Do you mean to say whenever I have to return a pointer I should malloc first before returning?

I am really confounded with this stack/heap thing. I wonder from where can I get more information regarding memory allocation mechanism of c.

The simple answer for you unpredictable program is - bad programming practices.

Don't have functions return pointers to variables on the stack

The behavior you describe is compiler dependent. If I had to venture a guess - Its because the compiler is optimizing away the char array when the printf function is commented out...but who knows without an audit of your compiled program..

The simple answer for you unpredictable program is - bad programming practices.

Don't have functions return pointers to variables on the stack

The behavior you describe is compiler dependent. If I had to venture a guess - Its because the compiler is optimizing away the char array when the printf function is commented out...but who knows without an audit of your compiled program..

I guess you mean to say that instead of returning a pointer which is to be assigned to a variable in caller, pass the variable in caller to the callee function. I understand when we need to modify the given string inside the callee function and instead of returning it is better to pass the string(which should be done in this trim case). But if suppose I dont know the size of the variable in caller beforehand, only after some calculation in the callee function (such as database operation or socket/io operations) the size is known(callee also have the data from database or socket). So in this case it seemed to be the only choice to get callee get the data and return it to the caller. I dont know what other choice there might be to do that.

From what I've read over the last twenty-four hours I can only assume your trying to learn C piece meal - one post at a time...Get yourself a good book on C and read. This trying to learn C by trail and error will never amount to anything...

If you need some good titles then try

The C Programming Lanaguge
The Complete Reference C

Please continue with C its a great language once you get a few things straightened out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.