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 3665072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:43:02+00:00 2026-05-19T01:43:02+00:00

I am trying to learn the basics of C, C++ programming, so I’ve started

  • 0

I am trying to learn the basics of C, C++ programming, so I’ve started with “C”.
I have lots of experience programming with Java and VB. But “C” is what I want to learn.
So I’m having an issue, trying to understand the “malloc” and “free” functions.

I am using Borland C++ 4.5 and Visual C++ 6.0 on Windows 98. – (Just a testing environment, want to learn the very basic and early windows programming).

Refer to this code:

struct String
{
 char *value;
 int length;
};
char *initString(const char *value)
{
 char *str = (char*)malloc( strlen(value)+1 );
 strcpy(str, value);
 return str;
}

struct String *InitString(const char *text)
{
 struct String *str = (struct String*)malloc( sizeof(struct String) );

 str->value = initString(text);
 str->length = strlen( str->value );
 return str;
}
void freeString(struct String *str)
{
 free(str->value);
 free(str);
 str = NULL;
}
int main(int argv, char *argc[])
{
 struct String *theString = InitString("Testring string struct");

 printf("String: %s\n", theString->value);
 printf("String Length: %d\n", theString->length);

 freeString(theString);

 printf("\nData: %s", theString->value);

 return 0;
}

When this program runs, the result is correct.
After I call “freeString(theString)“, the function does free the memory and set the
struct to NULL inside the “freeString()” function, that should free the “theString” inside “main()” as I pass in the pointer to “theString”, but when the function returns: “theString” is not “NULL”.

On Borland 4.5 I can still call the “printf” with “theString->value” and it prints the string.
On Visual C++ the program crashes when calling “printf” – BUT “theString” is still not “NULL”.
When I trace the program in debug mode, the “struct” is being freed inside the “freeString()” function and the struct is set to NULL, but when the function returns the “theString” is not NULL and the “value” is still usable on Borland, but not on Visual C++.

So I’m trying to understand, what is going on here?
Is there some de-reference that should be done?

Thank you in advance!

  • 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-19T01:43:03+00:00Added an answer on May 19, 2026 at 1:43 am

    You’re looking at undefined behavior (you’re using a value after it has been free’d), so really – anything could happen. It could crash, or it could operate “normally”

    What’s likely happening is msvc, atleast in debug mode, zeros out or writes a special byte pattern to the memory you free so the str->value pointer becomes invalid and crash when you dereference it, while borland just releases the memory back to a memory pool but leaves it untouched.

    Your str=NULL in the function

    void freeString(struct String *str)
    {
     free(str->value);
     free(str);
     str = NULL;
    }
    

    has no real effect. It just sets the local str value to NULL, the caller is not affected.

    If you want to set the pointer of the caller to NULL you have to pass in a pointer to that pointer, or in the case of C++, pass a reference to the pointer.

    void freeString(struct String *&str) //c++
    {
     free(str->value);
     free(str);
     str = NULL;
    }
    
    
    void freeString(struct String **str) //c, call it as freeString(&theString);
    {
     free((*str)->value);
     free(*str);
     *str = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to learn javascript and, having some basic programming experience already (from C++),
I'm just starting to learn Javascript; I do however have experience in programming using
I'm trying to learn the basics of iOS programming and I was wondering how
I'm trying to learn Java but it just seem like there are too many
I'm trying to learn how to use the library vcglib (http://vcg.sourceforge.net/index.php/Main_Page) but I'm having
I’m new to visual basic.net . I have experience in C++ programming, but never
I'm trying to understand the basics of practical programming around character encodings. A few
I'm new to Java-based web programming and am trying to learn JSF from the
I am trying to learn the basics of THREE.js. I have read a couple
Ok, so I'm trying to learn gdb. I know most of the basics but

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.