Hello to all.
Is this program licking memory?
Code:int main()
{
while(i<100)
{
...
printf("Example %s\n",function());
...
i++;
}
return 0;
}
char* function ()
{
char* data;
...
data = "memory";
....
return data;
}
Thanks!
Printable View
Hello to all.
Is this program licking memory?
Code:int main()
{
while(i<100)
{
...
printf("Example %s\n",function());
...
i++;
}
return 0;
}
char* function ()
{
char* data;
...
data = "memory";
....
return data;
}
Thanks!
No.
If it were to actually compile and run, it would only be using the program stack.
isn't 100,000 out of bounds for an integer? thought it was 16,000, unless i'm the dinosaur;->
The code is flawed.
1) It never calls the function, instead it prints the actual function address which is not a string. You may very well get junk or a crash.
2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.
3) It should be const char*, not char*.
4) i is never defined.
Do I need to malloc()?Quote:
2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.
you need to call function like
function()
Sorry. typing mistakeQuote:
you need to call function like
function()
Does this solve my problems?
Code:int main()
{
gchar *string;
while(i<100)
{
...
string = function();
printf("Example %s\n",string);
free (string);
...
i++;
}
return 0;
}
char* function ()
{
char* data = "";
...
data = concatenate(data,"memory");
....
return data;
}
char * concatenate (char* str1, char* str2)
{
char *result;
if ( str2 != NULL )
{
result = malloc(strlen(str1) + strlen(str2) + 1);
if ( result != NULL )
{
strcpy(result, str1);
strcat(result, str2);
}
}
return result;
}
I'm trying to solve this:
Quote:
1) It never calls the function, instead it prints the actual function address which is not a string. You may very well get junk or a crash.
2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.
For signed, yes. For unsigned, that would be 0 - 65535.
to solve the 1st - just add () to function to indicate the call of the function
About the second - when you return something like
return "some const string";
you returning pointer to const string that will not be destructed, so its OK...
If you are creating the string inside your function - its another story
I understand. So this would be fine
Thanks for you help!Code:
const char *text[] = {"memory","information"};
int main()
{
while(i<100)
{
...
printf("Example %s\n",function());
...
i++;
}
return 0;
}
char* function ()
{
char* data;
...
data = text[0];
....
return data;
}
Yes, it would work fine, but not the best solution. To return constant data, consider making it static to your function instead.
That way you avoid global variables.
Thanks.
What about
static const char* string[] = .... ?
> static const char* string[] = .... ?
Did you really want an array of read-only strings?