473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

free()ing a pointer

Markus
6,050 Expert 4TB
If we free() a pointer, is the pointer free()d or the memory the pointer points to?

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "stack.h"
  4.  
  5. void stack_new(stack *st) 
  6. {
  7.     st->log_len = 0;
  8.     st->alloc_len = 4;
  9.     st->elems = malloc(4 * sizeof(int));
  10. }
  11.  
  12. void stack_dispose(stack *st)
  13. {
  14.     free(st->elems);
  15. }
  16.  
  17. stack *stack_newx(void)
  18. {
  19.     stack *st = (stack *)malloc(sizeof(stack));
  20.     stack_new(st);
  21.     return st;
  22. }
  23.  
  24. void stack_disposex(stack *st)
  25. {
  26.     free(st); // Does this free the pointer or memory?
  27. }
Edit: since this pointer points to the memory, I guess it will free the memory. *stupid*.

Mark.
Oct 24 '09 #1
6 1933
when a pointer is freed the memory it pointing to is freed...after freeing also the pointer points to that particular location in memory.
Oct 24 '09 #2
Markus
6,050 Expert 4TB
@sumeshnb
Right. I get that. So, after the memory pointed to is freed, the pointer still points to that memory location?
Oct 24 '09 #3
drhowarddrfine
7,435 Expert 4TB
Yes. The pointer still exists and still points to that location.
Oct 24 '09 #4
Markus
6,050 Expert 4TB
@drhowarddrfine
Thanks, Doc.
Oct 24 '09 #5
donbock
2,426 Expert 2GB
Some of the confusion comes from using the phrase "freeing a pointer". This phrase certainly gives the impression that the pointer itself is being freed. Since that is not the case, I suggest you avoid using that phrase.

The code snippet in the OP "called function free(), passing it the value of a pointer". The result was to free the memory pointed to by the pointer variable.
Oct 24 '09 #6
drhowarddrfine
7,435 Expert 4TB
And to add the obvious, the same pointer can be used to point to new allocated memory or anything else of the same type.
Oct 24 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Peter Ammon | last post by:
Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
6
by: Code Raptor | last post by:
Folks, I am hitting a segfault while free()ing allocated memory - to make it short, I have a linked list, which I try to free node-by-node. While free()ing the 28th node (of total 40), I hit a...
13
by: santosh | last post by:
Hi, If I call free() with a uninitialised pointer, will the program state become undefined, or will free() return harmlessly? Incidentally, is there a way in Standard C to determine weather a...
76
by: dbansal | last post by:
Hi group, I have a question to ask you all. I have allocated some chunk of memory using ptr=(int*)malloc(). now I am trying to free that memory using free((void*)ptr). My question is does free()...
13
by: Chad | last post by:
Excercise 6-5 Write a functon undef that will remove a name and defintion from the table maintained by lookup and install. Code: unsigned hash(char *s); void undef(char *s)
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.