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

dynamic space allocation problem

10
hello peeps,

i just startet to coding in c, and dont have a big background.

now i have a problem witch i dont understand. i hope you guys can help me out here.

the problem is malloc. i try to allocate space for a padded string.i get the lenght of the new string with strlen and then try to allocate space with that value.but this does not work.the buffer had the size of 16 instead of 8, or 24 instead of 16 and so on.


this is my code:

Expand|Select|Wrap|Line Numbers
  1. int tfis()
  2. {
  3.     ERRCODES Status = 0;
  4.     int I;
  5.     const char PaddingByte = 0x24;     //dollar sign
  6.     char TestString[30] = "AAAAXAAA";
  7.     int StringLength = strlen(TestString);
  8.     unsigned int StringLengthNew;
  9.     unsigned short int TheCounter = 0;
  10.     char C;
  11.     unsigned char *StringTable = 0;
  12.  
  13.  
  14.     for(I=0; I<=StringLength; I++)   
  15.     {
  16.         C = TestString[i];
  17.  
  18.         if(! (C == 0))
  19.         {
  20.             printf("%c", C);
  21.         }
  22.         else 
  23.         {
  24.             printf(" End of File..\n");
  25.             if((I%8 == 0)&&(I != 0))
  26.             {
  27.                 //add at least 8 bytes..
  28.                 printf("1. the string had a length of %d bytes\n", I );
  29.                 printf("1. padding not needed, add at least 8 bytes\n");
  30.                 do
  31.                 {
  32.                     TestString[i] = PaddingByte;
  33.                     printf("%s\n", TestString);
  34.                     I++;
  35.                     TheCounter++;
  36.                 }while(I%8 != 0);
  37.             }
  38.             else 
  39.             {
  40.                 //add ++ bytes
  41.                 printf("2. the string had a length of %d bytes\n", I);
  42.                 printf("2. padding needed, add ++ bytes\n");
  43.                 do
  44.                 {
  45.                     TestString[i] = PaddingByte;
  46.                     printf("%s\n", TestString);
  47.                     I++;
  48.                     TheCounter++;
  49.                 }while(I%8 != 0);
  50.             }
  51.         }
  52.     }
  53.  
  54.     StringLengthNew = strlen(TestString);
  55.  
  56.     printf("the new string has a length of %d bytes. the value: %s\n", StringLengthNew, TestString);
  57.     printf("%d bytes added..\n", TheCounter);
  58.  
  59.     TheCounter = 0;
  60.     StringTable = malloc(StringLengthNew);             //allocate space for the new output buffer
  61.     printf("output size: %d\n", strlen(StringTable));
  62.  
  63.  
  64.     if(! StringTable)
  65.     {
  66.         printf("error..\n");
  67.         Status = BUFFER_SIZE;
  68.         return Status;
  69.     }
  70.     else
  71.     {
  72.         printf("success..\n");
  73.         strncpy(StringTable, TestString, StringLengthNew);  //copy the value to the new buffer
  74.         printf("Output String: %s\n", StringTable);
  75.     }
  76.  
  77.     free(StringTable);
  78.     StringTable = 0xc;
  79.  
  80.     return Status;
  81. }
  82.  
Sep 20 '07 #1
1 1665
weaknessforcats
9,208 Expert Mod 8TB
First, strlen returns the number of characters in the string. Your initial string is
"AAAAXAAA" which is 8 characters. strlen will return 8.

However, you loop:
for(I=0; I<=StringLength; I++)
{
C = TestString[i];
etc...
will cycle 9 times.

Your array is 30. Don't you mean to cycle 30 times??
Expand|Select|Wrap|Line Numbers
  1. for(I=0; I<30; I++)   
  2.     {
  3.         C = TestString[i];
  4.  etc...
  5.  

Can you use your debugger?? If you can you can see that your array phas been padded correctly without relying on a bunch of printf() statements that may not tell you what you need to know.
Sep 21 '07 #2

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

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
14
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
9
by: danil52 | last post by:
I have dynamic memory allocation in my constructor and deallocation in my destructor. However if there is exception being thrown in constructor, the destructor does not get called and memory leaks...
0
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.