473,405 Members | 2,415 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,405 software developers and data experts.

swapping pointers returned by malloc

Will the following code always work ?

#include <stdio.h>
#include <stdlib.h>

# define SIZE 50

void myswap(char **name1, char **name2);

int main(void)
{
char *s1;
char *s2 ;

s1 = malloc(SIZE);

if (s1 == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}

s2 = malloc(SIZE);

if (s2 == NULL)
{
free(s1);
s1 = NULL;
printf("malloc failed\n");
exit(EXIT_FAILURE);
}

printf("Before swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);

myswap(&s1, &s2);

printf("After swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);

free(s1);
s1 = NULL;
free(s2);
s2 = NULL;

return 0;
}

void myswap(char **name1, char **name2)
{
char *temp;

temp = *name1;
*name1=*name2;
*name2=temp;
}

Mar 14 '07 #1
1 2099
su**************@yahoo.com, India wrote:
Will the following code always work ?

#include <stdio.h>
#include <stdlib.h>

# define SIZE 50

void myswap(char **name1, char **name2);

int main(void)
{
char *s1;
char *s2 ;

s1 = malloc(SIZE);

if (s1 == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}

s2 = malloc(SIZE);

if (s2 == NULL)
{
free(s1);
s1 = NULL;
Totally redundant.
printf("malloc failed\n");
exit(EXIT_FAILURE);
}

printf("Before swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);

myswap(&s1, &s2);

printf("After swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);

free(s1);
s1 = NULL;
free(s2);
s2 = NULL;

return 0;
}

void myswap(char **name1, char **name2)
{
char *temp;

temp = *name1;
*name1=*name2;
*name2=temp;
}
Yes. It's the pointer *value* that's important, when you pass it to
free, (i.e. it must be a value previously returned by *alloc), not the
actual pointer object itself, as long as it's of a compatible type.

Mar 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! I question about understanding. I have read a book that says. If you have an array of integers you know that one element past the end of the array is legal as address but not to...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
26
by: Materialised | last post by:
Hi everyone, I seen the post by Rob Morris, and thought that I would double check that I was using pointers in the correct way. So I written the following string functions to test. I know soem can...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
13
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
11
by: krreks | last post by:
Hi. I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet......
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.