[Warning] address of local variable returned???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

[Warning] address of local variable returned???

 
0
  #1
Jul 26th, 2006
Hi. I picked up Dev C++ the other day after a while of not using it. Here's my question: I can't figure out what's wrong with my code Basically, the program prompts for a command. If the command starts with 'open:', then it puts the following character(s) within "<>"s. This is a C++-oriented HTML writing program. It's not too usefull as of now, but I'm planning on making it able to debug. If you think you can help, but you need more info, please email me at pbossier (at) cox (dot) net. Attached is my source file.
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. int arrayEqu(char array1[65535], char array2[65535], int length)
  10. {
  11. int i,ret = 1;
  12. for (i = 0; i < length; i++) {
  13. if (array1[i] != array2[i]) {
  14. ret = 0;
  15. }
  16. }
  17. return ret;
  18. }
  19.  
  20. void copyArr(char array1[65535], char array2[65535])
  21. {
  22. int i;
  23. for (i = 0; i < 65535; i++) {
  24. array2[i] = array1[i];
  25. }
  26. }
  27.  
  28. char* subArr(char array[65535], int length)
  29. {
  30. int i, j = 0;
  31. **** char temp[65535 - length + 1]; ****
  32. for (i = length - 1; i < 65535 - length - 1; i++) {
  33. temp[j] = array[i];
  34. j++;
  35. }
  36. return temp;
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41. int i, com = 0;
  42. char fileName[30], input[65535], secInput[65535];
  43. printf("Welcome to parker's HTML creator. Type a name for your HTML document...\nFile name: ");
  44. scanf("%s",fileName);
  45. printf("\nNow type a command (type 'help' for help)");
  46. strcat(fileName, ".html");
  47. ofstream toHtml (fileName);
  48. loop:;
  49. printf("\nCommand: ");
  50. scanf("%s",input);
  51. com = 0;
  52. if (arrayEqu(input, "quit", 5)) {
  53. return 0;
  54. }
  55. if (arrayEqu(input, "help", 5)) {
  56. printf("\nThe possible directive commands are: 'open:', 'close:', and 'comp:'.");
  57. printf("\n\nType an operand directly after the ':' to write it to the HTML file.");
  58. printf("\n\nTo type in a paragraph or similar text block, just type the text.");
  59. printf("\n\nNote: no word can be more than 65535 characters long, and cannot begin with a");
  60. printf("\ndirective command.");
  61. printf("\n\nNote: you can enter multiple lines of code in 1 statement by seperating");
  62. printf("\ncommands with spaces (several 'Command:' lines will appear).");
  63. printf("ignore them.");
  64. printf("\n\nType 'quit' to quit. Closing the DOS window will not save your file.\n");
  65. goto loop;
  66. }
  67. if (arrayEqu(input, "open:", 5)) {
  68. toHtml << "<" << subArr(input, 6) << ">\n";
  69. com = 1;
  70. }
  71. if (arrayEqu(input, "close:", 6)) {
  72. toHtml << "</" << subArr(input, 7) << ">\n";
  73. com = 1;
  74. }
  75. if (arrayEqu(input, "comp:", 5)) {
  76. copyArr(subArr(input, 6), secInput);
  77. if(arrayEqu(secInput, "clear")) {
  78. for (i = 0; i < 28; i++) {
  79. printf("\n");
  80. }
  81. }
  82. if(arrayEqu(secInput, "run")) {
  83. system(fileName);
  84. }
  85. com = 1;
  86. }
  87. if (!com) {
  88. toHtml << input << " ";
  89. }
  90. goto loop;
  91. }

When I comment out the
  1. if (arrayEqu(input, "comp:", 5)) {
  2. copyArr(subArr(input, 6), secInput);
  3. if(arrayEqu(secInput, "clear")) {
  4. for (i = 0; i < 28; i++) {
  5. printf("\n");
  6. }
  7. }
  8. if(arrayEqu(secInput, "run")) {
  9. system(fileName);
  10. }
  11. com = 1;
  12. }
it works fine. But when I leave it in, I get an error at the line marked with an *** that says 31 "[Warning] address of local variable `temp' returned."
Attached Files
File Type: cpp HTML.cpp (2.6 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: [Warning] address of local variable returned???

 
0
  #2
Jul 26th, 2006
The address of a local variable cannot be returned from a function. I think the problem is with the function
char* subArr(char array[65535], int length)

At the end of this function you are returning "temp" which is the starting address of the array temp which is created in this function.
Local variables are placed in the stack. Once the closing braces of that function is executed, the stack is deallocated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

Re: [Warning] address of local variable returned???

 
0
  #3
Jul 26th, 2006
So how do I fix that? So are all of my functions that return arrays (or their pointers) incorrect?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

Re: [Warning] address of local variable returned???

 
0
  #4
Jul 26th, 2006
Neveremind. I only have that one function. But how do i fix it?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

Re: [Warning] address of local variable returned???

 
0
  #5
Jul 26th, 2006
I fixed it. I made the temp array a global varibale.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: [Warning] address of local variable returned???

 
0
  #6
Jul 26th, 2006
Originally Posted by bossier330
So how do I fix that? So are all of my functions that return arrays (or their pointers) incorrect?
All the functions are not incorrect. It will only be a problem if u have defined the variable locally and trying to return the address of it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: [Warning] address of local variable returned???

 
0
  #7
Jul 26th, 2006
YOur function is like

  1.  
  2. char* subArr(char array[65535], int length)
  3. {
  4. int i, j = 0;
  5. **** char temp[65535 - length + 1]; ****
  6. for (i = length - 1; i < 65535 - length - 1; i++) {
  7. temp[j] = array[i];
  8. j++;
  9. }
  10. return temp;
  11. }

To be in control of the array returned by the function you can try allocating the array or placing it on heap memory using the call to "malloc" somewat like this


  1.  
  2. char* subArr(char array[65535], int length)
  3. {
  4. int i, j = 0;
  5. char* temp = (char*) malloc (65535 - length + 1);
  6. for (i = length - 1; i < 65535 - length - 1; i++)
  7. {
  8. temp[j] = array[i];
  9. j++;
  10. }
  11. return temp;
  12. }

So here u are in command of the array returned. But do take care to free the allocated memory when u are done with the returned array.

free (temp)

Hope it helped,
Bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: [Warning] address of local variable returned???

 
0
  #8
Jul 26th, 2006
Since you've already included string, why not use std::string for all your strings rather than messing about with char arrays, with their fixed length, no overflow protection, do it yourself alloc and free problems etc etc.

> char temp[65535 - length + 1];
C++ doesn't have variable length arrays, you're relying on a compiler-specific extension to do this.

static char temp[65535]; would be a quick fix hack in this particular case.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC