Valgrind error


 
Thread Tools Search this Thread
Top Forums Programming Valgrind error
# 1  
Old 06-10-2010
Valgrind error

I get this Valgrind error while using malloc but if I use calloc then there is no error.

I allocate 8 bytes for the string inside sprintf, 12 for the ip and 1 for the string terminator. This totals 21, so why is it talking about 20 bytes and 18 bytes ?

Quote:
==10228== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==10228== at 0x4EF65B5: send (in /lib/libc-2.7.so)
==10228== by 0x4030B4: functionname (includefile.h:249)
==10228== by 0x403976: main (program.c:28)
==10228== Address 0x517d89a is 18 bytes inside a block of size 20 alloc'd
==10228== at 0x4C2260E: malloc (vg_replace_malloc.c:207)
==10228== by 0x40307C: functionname (includefile.h:247)
==10228== by 0x403976: main (program.c:28)
Partial code:

Code:
char ip[] = "80.80.80.80";
int buffer = 8 + strlen(ip); // 8 for headers fixed text
char *headers = malloc(buffer + 1);
sprintf(headers, " -r %s\r\n", ip);
send(foobar, headers, buffer, 0);

# 2  
Old 06-10-2010
Your fixed-lenght text is only 6 characters, not 8. The substring "\r\n" is two bytes - carriage return and linefeed, not four bytes.
# 3  
Old 06-11-2010
Quote:
Originally Posted by achenle
Your fixed-lenght text is only 6 characters, not 8. The substring "\r\n" is two bytes - carriage return and linefeed, not four bytes.
Yes, it seems gedit does not count the bytes correctly (and me neither). strlen states 6 bytes.
# 4  
Old 06-11-2010
Quote:
Originally Posted by cyler
I get this Valgrind error while using malloc but if I use calloc then there is no error.

I allocate 8 bytes for the string inside sprintf, 12 for the ip and 1 for the string terminator. This totals 21, so why is it talking about 20 bytes and 18 bytes ?
Valgrind is reporting the storage required correctly...
Quote:
Originally Posted by cyler
Partial code:

Code:
char ip[] = "80.80.80.80";
int buffer = 8 + strlen(ip);         // buffer=19 becoz strlen excludes the terminating null byte
char *headers = malloc(buffer + 1);  // malloc allocates 20 bytes
sprintf(headers, " -r %s\r\n", ip);  // this is 18 bytes becoz sprintf adds a null byte at the end
send(foobar, headers, buffer, 0);

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using Valgrind with already running process

I have a process(c program) which runs as daemon and is causing memory leak. Is there any way to detect memory leak in this already running process? Just like we attach gdb to a live process to debug it, can I use valgrind to detect memory leak in such live process? When I tried using valgrind... (3 Replies)
Discussion started by: rupeshkp728
3 Replies

2. Ubuntu

valgrind

Hello, I want to install and run a tool called Daikon on my Ubuntu(latest version) While compiling I got this error--------> checking the GLIBC_VERSION version... unsupported version configure: error: Valgrind requires glibc version 2.2 - 2.11 make: *** Error 1 make: Leaving directory... (3 Replies)
Discussion started by: paramad
3 Replies

3. Programming

Valgrind Error : Conditional jump or move depends on uninitialised value(s)

Hi Friends, I am using valgrind, to check errors, there are no errors in my code but this the output put i get, i need to suppress these errors, please help me do so... Conditional jump or move depends on uninitialised value(s) ==2350== at 0x400AF0F: (within /lib/ld-2.11.1.so)... (0 Replies)
Discussion started by: niranjanvg
0 Replies

4. AIX

valgrind - pthread memory leaks on AIX

Hi all, I have written a small code just to invoke main and return immediately. When built with libpthread on AIX box, valgrind throws lots of memory leak errors. But when built without libpthread, no issues at all. Here is the sample run for your look. Any idea where I might be going wrong?... (3 Replies)
Discussion started by: visionofarun
3 Replies

5. UNIX for Dummies Questions & Answers

valgrind Conditional jump or move depends on uninitialised value(s)

Hi, I know similar questions appeared here already, still i didnt find answer to my problem yet. Im getting the following message from valgrind (version 2): ==15414== 1 errors in context 1 of 6: ==15414== Conditional jump or move depends on uninitialised value(s) ==15414== at... (2 Replies)
Discussion started by: evasz
2 Replies

6. Programming

valgrind (Conditional jump or move depends on uninitialised value(s))

Hi everybody, I wrote a small subroutine 'GetStringDelim()' to return a substring from a string enclosed by a string 'Limit1' and a char 'Limit2'. It works perfectly and I don't get an error message together with valgrind, if I set the 3rd parameter in GetStringDelim() to NULL (see below). ... (3 Replies)
Discussion started by: MIB_Maik
3 Replies

7. AIX

Can Valgrind work well on AIX?

As Valgrind announced, 3.3.0 and 3.3.1 version can support AIX 5.3. But I met a block issue when I used Valgrind on AIX After installing Valgrind3.3.1 successfully on AIX5.3, I typed the following command of Valgrind: valgrind -d --tool=memcheck ls Then, the following result from Valgrind is... (0 Replies)
Discussion started by: adasong
0 Replies
Login or Register to Ask a Question