Hi Keith,
Thanks for your help!
Okay, I tried it again, this time with realloc():
I haven't studied the entire thing, but a few things jumped out at me.
This header is non-standard. I don't think you're using it in your
posted code (though you may be using it in the full version).
right, I tried something earlier and forgot to take it back out -
thanks
int AppendToFIFO(char* locQueue, void* newData, int newDataLength)
{ [...]
/
***********************************************************************************
function: AppendToFIFO
appending newDataLength bytes from newData to MyQueue.
return value: 0 on success
1 on failure
attributes: void* newData - pointer to data to be added to queue
int newDataLength - number of bytes from newData to be
added to MyQueue
***********************************************************************************/
char* tmp;
/* Verifying data length is set > 0*/
if (newDataLength<=0)
{
printf("Error: data length is 0\n");
return 1;
}
Normally a function should return an error indication and let the
caller decided how to deal with it. Printing an error message within
a function like this is ok during debugging, but a bad idea in
production code.
Yup, I'm aware of this but this is code in development and things
gotta go quickly sometimes
[...]
printf("strlen(locQueue):%d\n",strlen(locQueue));
strlen() returns a result of type size_t; "%d" expects an argument of
type int. Try this:
printf("strlen(locQueue):%zu\n", strlen(locQueue));
or, if your printf doesn't support "%zu" (it's new in C99):
printf("strlen(locQueue):%lu\n", (unsigned long)strlen(locQueue));
Since locQueue is of type char*, *locQueue is of type char. Do you
really want to assign the void* value returned by realloc() to a char
object?
Oh yeah, you're right...hoops...
realloc is declared in <stdlib.h>, but I don't see a "#include <stdlib.h>".
Your compiler should have warned you about this. If it doesn't, crank
up the warning level.
gcc actually did give me a warning but i just ignored it....added
stdlib now
There's no need for the cast.
Not sure if here's another possibility than using strcat/strncat in
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <
http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Ok and after applying these changes my code looks like this:
/*****
SOME HEAD
fifo.c
****/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int AppendToFIFO(char *locQueue, void* newData, int newDataLength);
int main (int argc, char* argv[])
{
char *MyQueue;
char* str1="TheseAre16chars";
/*appending data to the queue*/
if(AppendToFIFO(MyQueue, str1, 16)==0)
{
printf("1st Conent of Queue:%s\n",MyQueue);
}
else
printf("Problem appending data to Queue\n");
char* str2="hello";
/*appending data loaded in number to the queue*/
if(AppendToFIFO(MyQueue, str2, 5)==0)
{
printf("2nd Conent of Queue:%s\n",MyQueue);
}
else
printf("Problem appending data to Queue\n");
return 0;
}
int AppendToFIFO(char* locQueue, void* newData, int newDataLength)
{
/
***********************************************************************************
function: AppendToFIFO
appending newDataLength bytes from newData to MyQueue..
return value: 0 on success
!0 on failure
attributes: void* newData - pointer to data to be added to queue
int newDataLength - number of bytes from newData to be
added to MyQueue
return values:1 newDataLength is equal or smaller 0
2 length is newData is 0
***********************************************************************************/
char* tmp;
/* Verifying data length is set > 0*/
if (newDataLength<=0)
{
return 1;
}
/*checking if newData is unqual 0*/
if (strlen(newData)==0)
{
return 2;
}
printf("strlen(locQueue):%zu\n",strlen(locQueue));
/*allocating new memory size */
locQueue = realloc(locQueue, (strlen(locQueue) + newDataLength));
tmp=locQueue;
/* How can i add the new content aligned to the left withou a
atrcat? strcat somehow doesn't seem the right solution for this, does
it? */
strcat(newData, locQueue);
/* copying new string into allocated memory */
memcpy(locQueue, newData, strlen(locQueue));
return 0;
}
and i get all weird terminal output, look at this:
reg@reg-laptop:~$ gcc -o fifo fifo.c
reg@reg-laptop:~$ ./fifo
strlen(locQueue):9
*** glibc detected *** ./fifo: realloc(): invalid pointer: 0xb7faff50
***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e9d3f4]
/lib/tls/i686/cmov/libc.so.6(realloc+0x242)[0xb7ea1ec2]
/lib/tls/i686/cmov/libc.so.6(realloc+0x42)[0xb7ea1cc2]
./fifo[0x80485da]
./fifo[0x80484f6]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e44685]
./fifo[0x8048431]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:03 408296 /home/reg/fifo
08049000-0804a000 r--p 00000000 08:03 408296 /home/reg/fifo
0804a000-0804b000 rw-p 00001000 08:03 408296 /home/reg/fifo
09c42000-09c63000 rw-p 09c42000 00:00 0 [heap]
b7e2d000-b7e2e000 rw-p b7e2d000 00:00 0
b7e2e000-b7f86000 r-xp 00000000 08:01 1133723 /lib/tls/i686/cmov/
libc-2.8.90.so
b7f86000-b7f88000 r--p 00158000 08:01 1133723 /lib/tls/i686/cmov/
libc-2.8.90.so
b7f88000-b7f89000 rw-p 0015a000 08:01 1133723 /lib/tls/i686/cmov/
libc-2.8.90.so
b7f89000-b7f8c000 rw-p b7f89000 00:00 0
b7f90000-b7f9d000 r-xp 00000000 08:01 1116350 /lib/libgcc_s.so.1
b7f9d000-b7f9e000 r--p 0000c000 08:01 1116350 /lib/libgcc_s.so.1
b7f9e000-b7f9f000 rw-p 0000d000 08:01 1116350 /lib/libgcc_s.so.1
b7f9f000-b7fa2000 rw-p b7f9f000 00:00 0
b7fa2000-b7fbc000 r-xp 00000000 08:01 1116307 /lib/ld-2.8.90.so
b7fbc000-b7fbd000 r-xp b7fbc000 00:00 0 [vdso]
b7fbd000-b7fbe000 r--p 0001a000 08:01 1116307 /lib/ld-2.8.90.so
b7fbe000-b7fbf000 rw-p 0001b000 08:01 1116307 /lib/ld-2.8.90.so
bfeaa000-bfebf000 rw-p bffeb000 00:00 0 [stack]
Aborted
reg@reg-laptop:~$
I'm not sure why it dumps the memory map tho...