Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3759892
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:23:14+00:00 2026-05-19T10:23:14+00:00

So, I have 2 pieces of code, one that works and one that doesn’t.

  • 0

So, I have 2 pieces of code, one that works and one that doesn’t. The first piece was just a test to find out if a char pointer would remain valid after it is returned from a local allocation. For some reason this works:

    char* test(){
        char* rawr="what";
        return rawr;  
    }

But this one doesn’t work:

    char* folderfromfile(char* filz) //gets the folder path from the file path
    {
            //declarations
            int  lastslash=-1;
            int  i        =0;
            char rett[256];

        for(i;(int)filz[i]!=0;i++)
                if(filz[i]=='\\')
                    lastslash=i;     //records the last known backslash

        if(lastslash==-1)
                return "";           //didn't find a backslash

        for(i=0;i<=lastslash;i++)
                rett[i]=filz[i];     // copies to new string

        rett[i]    =0; //end of string
        cout << &rett << "====" << rett << endl;

        system("pause>nul");//pause so i can watch over the memory before it deallocates
        return rett;  
    }

I bet that there is a better way to accomplish this task of removing the file name from the full path but for now I’m just trying to figure out why this char pointer gets deleted while the other one doesn’t. If I had to guess I would say its because I declared it differently, or because its larger. Yes, I could just pass another char pointer as an argument to this function, but that wouldn’t answer my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:23:14+00:00Added an answer on May 19, 2026 at 10:23 am
    char* test(){
        char* rawr="what";
        return rawr;  
    }
    

    The string literal "what" is not allocated on the stack – it remains valid throughout the life of the program. However it must NOT be modified. The pointer rawr itself is on the stack, but this is only a problem if you write things like &rawr, getting a pointer-to-a-pointer.

    char* folderfromfile(char* filz){ //gets the folder path from the file path
        int lastslash=-1,i=0;char rett[256]; //declarations
        ///// ...
        return rett;  
    }
    

    This, however, puts an array on the stack. rett here is implicitly &rett[0], that is, gets a pointer to the first element of the array, which is very much on the stack, and is invalid after returning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a three pieces of code that are giving me trouble. This one
I have this piece of code that basically checks if one or more files
I have problems using preventDefault. The problem is that this piece of code works
I have two pieces of code in Jsfiddle. One works and the other one
I still need your help. I have this piece of code that doesn't want
I have written a piece of code by mistake that works. Now i need
I have these two pieces of code, wich one is more readable? foreach decimal
I have a lot of pieces of code which has to be run one
I have a piece of code VERY similar to this one http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailFetching I the
Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.