473,399 Members | 3,656 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,399 software developers and data experts.

memory dellocation

Hi there,

I've written a function that 'converts' a structure of type timeval to
a c-string:

char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;

sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);

cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);

return cpResult;
}

I use it like this:

printf("%s", tv2Str((struct timeval){123, 456}) );

Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?

Thanks,
Cornel
Nov 13 '05 #1
4 2483
>char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;

sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);

cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);

return cpResult;
}

I use it like this:

printf("%s", tv2Str((struct timeval){123, 456}) );

Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?


It does not. That's your responsibility, and you blew it.
You have a memory leak.

Gordon L. Burditt
Nov 13 '05 #2
Gordon Burditt wrote:
char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;

sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);

cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
1) Lose the cast. It's not necessary -- and can possibly hide errors
(see the FAQ for further details).

2) sizeof(char) is _defined_ to be 1 -- so you don't need/want this term

3) If you're copying a C-string, always allocate strlen(orig) + 1, to
account for the trailing null.
strcpy(cpResult, caStr);

return cpResult;
}

I use it like this:

printf("%s", tv2Str((struct timeval){123, 456}) );

Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?

It does not. That's your responsibility, and you blew it.
You have a memory leak.

Yup. You do!

HTH,
--ag


--
Artie Gold -- Austin, Texas

Nov 13 '05 #3
Gordon Burditt wrote:

[attribution to c.*****@bluewin.ch re-inserted]:

char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;

sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);

cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);

return cpResult;
}

I use it like this:

printf("%s", tv2Str((struct timeval){123, 456}) );

Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?


It does not. That's your responsibility, and you blew it.
You have a memory leak.


Worse: he has undefined behavior because the memory
allocated to `cpResult' is one byte too small to hold the
data copied into it by strcpy(). And that's even ignoring
the possibility of malloc() failure, for which he's written
no test.

--
Er*********@sun.com
Nov 13 '05 #4

<c.*****@bluewin.ch> wrote in message news:16**************************@posting.google.c om...
Hi there,

I've written a function that 'converts' a structure of type timeval to
a c-string:

char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;

sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);

cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);

return cpResult;
}

I use it like this:

printf("%s", tv2Str((struct timeval){123, 456}) );

Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?

Thanks,
Cornel


No, it doesn't. It is memory leak

I think it is not common to allocate a block of memory and return it (because you can't free it).
If you want to retrun a string result, you can request a char* in the function argument and fill it.

char *tv2Str(struct timeval tvX, char *strResult)
{
sprintf(strResult, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);

return strResult;
}

You have to provide the buffer to the call

char somebuffer[50];
printf("%s", tv2Str((struct timeval){123, 456}, somebuffer) );
--
BC


Nov 13 '05 #5

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
4
by: c.arnet | last post by:
Hi there, I've written a function that 'converts' a structure of type timeval to a c-string: char *tv2Str(struct timeval tvX) { char caStr; char *cpResult;
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...

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.