RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 4405 | Replies: 7
Reply
Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

[Warning] address of local variable returned???

  #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.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

int arrayEqu(char array1[65535], char array2[65535], int length)
{
    int i,ret = 1;
    for (i = 0; i < length; i++) {
        if (array1[i] != array2[i]) {
           ret = 0;
        }
    }
    return ret;
}

void copyArr(char array1[65535], char array2[65535])
{
      int i;
      for (i = 0; i < 65535; i++) {
          array2[i] = array1[i];
      }
}

char* subArr(char array[65535], int length)
{
    int i, j = 0;
    ****    char temp[65535 - length + 1];    ****
    for (i = length - 1; i < 65535 - length - 1; i++) {
        temp[j] = array[i];
        j++;
    }
    return temp;
}

int main(int argc, char *argv[])
{
    int i, com = 0;
    char fileName[30], input[65535], secInput[65535];
    printf("Welcome to parker's HTML creator.  Type a name for your HTML document...\nFile name: ");
    scanf("%s",fileName);
    printf("\nNow type a command (type 'help' for help)");
    strcat(fileName, ".html");
    ofstream toHtml (fileName);
    loop:;
    printf("\nCommand: ");
    scanf("%s",input);
    com = 0;
    if (arrayEqu(input, "quit", 5)) {
       return 0;
    }
    if (arrayEqu(input, "help", 5)) {
       printf("\nThe possible directive commands are: 'open:', 'close:', and 'comp:'.");
       printf("\n\nType an operand directly after the ':' to write it to the HTML file.");
       printf("\n\nTo type in a paragraph or similar text block, just type the text.");
       printf("\n\nNote: no word can be more than 65535 characters long, and cannot begin with a");
       printf("\ndirective command.");
       printf("\n\nNote: you can enter multiple lines of code in 1 statement by seperating");
       printf("\ncommands with spaces (several 'Command:' lines will appear).");
       printf("ignore them.");
       printf("\n\nType 'quit' to quit.  Closing the DOS window will not save your file.\n");
       goto loop;
    }
    if (arrayEqu(input, "open:", 5)) {
       toHtml << "<" << subArr(input, 6) << ">\n";
       com = 1;
    }
    if (arrayEqu(input, "close:", 6)) {
       toHtml << "</" << subArr(input, 7) << ">\n";
       com = 1;
    }
    if (arrayEqu(input, "comp:", 5)) {
       copyArr(subArr(input, 6), secInput);
       if(arrayEqu(secInput, "clear")) {
          for (i = 0; i < 28; i++) {
              printf("\n");
          }
       }
       if(arrayEqu(secInput, "run")) { 
          system(fileName);
       }
       com = 1;
    }
    if (!com) {
       toHtml << input << " ";
    }
    goto loop;
}

When I comment out the
if (arrayEqu(input, "comp:", 5)) {
       copyArr(subArr(input, 6), secInput);
       if(arrayEqu(secInput, "clear")) {
          for (i = 0; i < 28; i++) {
              printf("\n");
          }
       }
       if(arrayEqu(secInput, "run")) { 
          system(fileName);
       }
       com = 1;
    }
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)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: Bangalore, India
Posts: 88
Reputation: dilip.mathews is an unknown quantity at this point 
Rep Power: 3
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???

  #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  
Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

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

  #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  
Join Date: Jul 2006
Posts: 4
Reputation: bossier330 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bossier330 bossier330 is offline Offline
Newbie Poster

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

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

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

  #5  
Jul 26th, 2006
I fixed it. I made the temp array a global varibale.
Reply With Quote  
Join Date: Jun 2006
Location: Bangalore, India
Posts: 88
Reputation: dilip.mathews is an unknown quantity at this point 
Rep Power: 3
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???

  #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  
Join Date: Jun 2006
Location: India
Posts: 7,054
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 372
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

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

  #7  
Jul 26th, 2006
YOur function is like

char* subArr(char array[65535], int length)
{
    int i, j = 0;
    ****    char temp[65535 - length + 1];    ****
    for (i = length - 1; i < 65535 - length - 1; i++) {
        temp[j] = array[i];
        j++;
    }
    return temp;
}


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


char* subArr(char array[65535], int length)
{
    int i, j = 0;
    char*  temp = (char*) malloc (65535 - length + 1);
    for (i = length - 1; i < 65535 - length - 1; i++) 
    {
        temp[j] = array[i];
        j++;
    }
    return temp;
}

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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,923
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 452
Colleague
Salem's Avatar
Salem Salem is offline Offline
Senior Poster

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

  #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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:47 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC