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

Problem with garbage value in string

94 64KB
Hello. I wrote a program that tries to tokenize a mathematical expression, inserting the tokens in a list of strings. The list is as follows:

Expand|Select|Wrap|Line Numbers
  1. typedef struct listOfStrings {
  2.     char **array;
  3.     int size;
  4. } ListOfStrings;
There is even a function to initialize the listOfStrings. The thing is: I'm printing a token every time it is complete and every time it is inserted in the list. The output is okay. However, when all tokens are processed and I call function print_list_of_strings to print the tokens again, the first token is printed with a leading garbage value if the input for the program is "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3". How is this possible? The code for printing the list is as follows:

Expand|Select|Wrap|Line Numbers
  1. void print_list_of_strings( const ListOfStrings *const lPtr )
  2. {
  3.     int i;
  4.     int numberOfElements = lPtr->size;
  5.  
  6.     if ( numberOfElements != 0 )
  7.     {
  8.         for ( i = 0 ; i < numberOfElements ; ++i )
  9.         {
  10.             printf( "%s\n", *( ( lPtr->array ) + i ) );
  11.         }
  12.     }
  13.     else
  14.     {
  15.         printf( "---\n" );
  16.     }
  17. }
The list just prints --- if it's empty, although this isn't the case for the program I'm writing. Also, if the input is "1 + 2", everything goes fine.

The code for inserting at the list is:

Expand|Select|Wrap|Line Numbers
  1. int insert_at_end_of_list_of_strings( ListOfStrings *lPtr, const char *const str )
  2. {
  3.     int lengthOfStr = strlen( str );
  4.     int numberOfElements = lPtr->size;
  5.  
  6.     if ( ( ( *( lPtr->array + numberOfElements ) ) = ( char * )malloc( ( lengthOfStr + 1 ) *
  7.         sizeof( char ) ) ) == NULL )
  8.     {
  9.         return 0;
  10.     }
  11.  
  12.     memcpy( *( lPtr->array + numberOfElements ), str, ( lengthOfStr + 1 ) );
  13.  
  14.     ++( lPtr->size );
  15.  
  16.     printf( "%s inserted!\n", *( lPtr->array + numberOfElements ) );
  17.  
  18.     return 1;
  19. }
Feb 14 '14 #1
5 5196
weaknessforcats
9,208 Expert Mod 8TB
You didn't post the code to initialize listOfStrings.

I assume it does something like a) malloc an array of char* and b) set listOfStrings.size to the number f char* allocated.
Presumably, all allocated char* are set to 0.

You would add a string (str) to this array by:

Expand|Select|Wrap|Line Numbers
  1. array[0] = str;
so I'm not sure what all the malloc and memcpy is about. Are you making a copy in addition to adding the string to the array? It looks like that would give you headaches in memory management and knowing which string to use (the original or the copy).

Maybe you could post your initialization function.

Also, I am not seeing how you know how much of the array is used. All I see is the size, which is most likely the number of elements but nothing about which elements are available.
Feb 15 '14 #2
stdq
94 64KB
Hello! I'm using malloc to allocate space to the new string to be added to the list. To be honest, I'm not sure I'm doing it right. Here is the code for the initialization - it returns 1 on success or 0 on failure:

Expand|Select|Wrap|Line Numbers
  1. int initialize_list_of_strings( ListOfStrings *lPtr )
  2. {
  3.     if ( ( lPtr->array = ( char ** )malloc( sizeof( char * ) ) ) == NULL )
  4.     {
  5.         return 0;
  6.     }
  7.  
  8.     lPtr->size = 0;
  9.  
  10.     return 1;
  11. }
Feb 15 '14 #3
stdq
94 64KB
I think I figured it out. I added the following statement to the beginning of the function of insertion:

Expand|Select|Wrap|Line Numbers
  1. lPtr->array = ( char ** )realloc( lPtr->array, ( numberOfElements + 1 ) * sizeof( char * ) );
Feb 15 '14 #4
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. lPtr->array = ( char ** )malloc( sizeof( char * ) ) )
This code will allocate one pointer. You can have one string in this array.

The allocating code does not initialize the pointer to zero. In the programming busimess, a zero pointer is OK a non-zero pointer is assumed to point to valid data.

You might consider allocating enough pointers so that you don't need to realloc every time. Maybe you start off with 30 pointers and you initialize them all to zero.

You would add a string just by traversing the array looking for the first zero pointer and replace it with the address of your string. If there are no zero pointers, the array is full so you can now reallocate and add more pointers.

You insert in the list by:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < lPtr->size; ++i)
  2. {
  3.     if (lPtr->array[i] == 0)
  4.     {
  5.        lPtr->array[i] = str;
  6.     }
  7. }
  8.  
You delete from the list by:

Expand|Select|Wrap|Line Numbers
  1.  free(lPtr->array[i]);
  2.      lPtr->array[i] = 0;
  3.  
You might consider a linked list of char* and eliminate the array altogether. This way additions and deletions are handled by adding and removing elements of the list. This method is the one most commonly used.
Feb 16 '14 #5
stdq
94 64KB
Thanks for your input! I guess I still have much to learn. I was trying to use a data structure of my own, but I intend to study data structures, in particula the book from Sedgewick. Thanks again!
Feb 16 '14 #6

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

Similar topics

11
by: dave | last post by:
void CalcPortGrossRet(Funds tf,int fsize,PortFolio tp,int cmonth) { int i=0; float totgrossdlrval=0; char converter; while(i<fsize){ tf.enddlrval=(tf.dlrval*(tf.ret/100.0)+tf.dlrval);...
9
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) {...
2
by: Jarek Bednarz | last post by:
Hi all, Following code causes strAddress to contain garbage data. When I decrease number of characters that appear before '\\' then everything is ok. 12 chars before '\\' seems to be a magic...
1
by: noor | last post by:
Hi, I'm trying to connect to my Oracle 10g Database i m facing the problem of connection string, following code is that i use, please tell me how to use the connection string for accessing the...
2
by: andrew browning | last post by:
this program is a binary tree using nodes. it should do an in-order print, which it does. however, it inserts a garbage value at the end of the print. ex: cin >> 1 2 3 4 5 prints 1 2 3 4 5...
1
by: caterpillar | last post by:
Hi All, I have a Visual C++ 6.0 App. The compiler is running on Win98. Recently all of my String fields started displaying a (?A) -- with a tilde over the A -- whenever it tries to get a string...
0
by: aton1 | last post by:
Hi, i have some questions regarding the use of datatype string in C++. I use g++ compiler and also use .ccmalloc to test for the memory leak. Here is a simple program that i have wrote which...
8
by: Aftabpasha | last post by:
I have seen a simple program for handling Link List operations. The following is the part of the code that contains the problem. The code is modified but contains all necessary information regarding...
3
by: Anchal Laller | last post by:
wht is garbage value?? how it generates and why it generates?? why default value is not null in c/c++
2
by: Shubhangi24 | last post by:
please see the following code... why the garbage value for all the variables is same for same data type? #include<stdio.h> int main() { int a,b,c; float f1,f2;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
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.