dynamic memory allocation in strings

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 3342 Views - Last Post: 29 October 2007 - 02:50 AM Rate Topic: -----

#1 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

dynamic memory allocation in strings

Posted 25 October 2007 - 03:10 AM

i am using the following code to dynamically allocate memory to a string but this is not working...
#include <iostream.h>
#include <string.h>
int *s;
int main () 
{
  int i = 0;
  float perc;
  int appearances = 1;
  int size;
  s= new int[size];
  cout<<"Enter the string: ";
  char str[s];
  cin.getline(str,size);
  int a=strlen(str);
  for(i=0;str[i]!='\0';i++)
  {
	  if(str[i]=='t')
	  {
		  if(str[i+1]=='h')
		  {
			  if(str[i+2]=='e')
			  {
				  
				 perc=(100*i)/(a);
				 cout<<appearances<<" 'the' found when "<<perc<<" % of the file is read"<<endl;
				 appearances++;
						
			  }
		  }
	  }
			
  }
  cout<<"total of "<<appearances-1 <<" 'the' found"<<endl;
  return 0;
}



*1lacca edit: to close a tag, you need a / not a \

Is This A Good Question/Topic? 0
  • +

Replies To: dynamic memory allocation in strings

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 03:19 AM

Hi, well the only problem is that the size variable has no value assigned from you. You're doing an ok job with the new keyword. So assign value to size first, then allocate memory.

and this is how you should use code tags:

:code:

This post has been edited by PennyBoki: 25 October 2007 - 03:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 03:30 AM

View PostPennyBoki, on 25 Oct, 2007 - 03:19 AM, said:

Hi, well the only problem is that the size variable has no value assigned from you. You're doing an ok job with the new keyword. So assign value to size first, then allocate memory.

and this is how you should use code tags:

:code:

i have tried that also.....but it's not working
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 03:42 AM

well post the errors you are receiving...
Another tip: place this line: s= new int[size]; right below this one:
cin.getline(str,size); see what happens. And play around with the code you'll eventually get it.
Was This Post Helpful? 0
  • +
  • -

#5 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 03:46 AM

View PostPennyBoki, on 25 Oct, 2007 - 03:42 AM, said:

well post the errors you are receiving...
Another tip: place this line: s= new int[size]; right below this one:
cin.getline(str,size); see what happens. And play around with the code you'll eventually get it.

problem is occuring in the expression
char str[size];
i am not getting how to solve this...
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 03:55 AM

I don't see such expression in your code above. And as I said you need to assign value to size so that you can allocate memory. Post the updated code, and post the exact compiler messages ... thx.
Was This Post Helpful? 0
  • +
  • -

#7 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 04:00 AM

View PostPennyBoki, on 25 Oct, 2007 - 03:55 AM, said:

I don't see such expression in your code above. And as I said you need to assign value to size so that you can allocate memory. Post the updated code, and post the exact compiler messages ... thx.

include <iostream.h>
#include <string.h>
int *s;
int main () 
{
  int i = 0;
  float perc;
  int appearances = 1;
  int size;
  s= new int[size];
  char str[s];
  cout<<"Enter the string: ";
  cin.getline(str,size);
  int a=strlen(str);
  for(i=0;str[i]!='\0';i++)
  {
	if(str[i]=='t')
	{
	  if(str[i+1]=='h')
	  {
		if(str[i+2]=='e')
		{
			perc=(100*i)/(a);
			cout<<appearances<<" 'the' found when "<<perc<<" % of the file is read"<<endl;
			appearances++;
		}
	  }
	}
  }
  cout<<"total of "<<appearances-1 <<" 'the' found"<<endl;
  return 0;
}



C:\testprograms\assi1.cpp(51) : error C2057: expected constant expression
C:\testprograms\assi1.cpp(51) : error C2466: cannot allocate an array of constant size 0
C:\testprograms\assi1.cpp(51) : error C2133: 'str' : unknown size
C:\testprograms\assi1.cpp(63) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of dat

*1lacca same thing here
Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 04:06 AM

yes.. as suspected. I believe I've said enough in my previous posts, and again this is how you should use code tags:
:code:
Was This Post Helpful? 0
  • +
  • -

#9 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 04:15 AM

View PostPennyBoki, on 25 Oct, 2007 - 04:06 AM, said:

yes.. as suspected. I believe I've said enough in my previous posts, and again this is how you should use code tags:
:code:

you are not getting what i m trying to do.... i want the user to give the string...so how can i give the size in advance.....if i can do that then that can be done without using pointers also....the problem with me is that the concept of pointers are not so clear to me...
Was This Post Helpful? 0
  • +
  • -

#10 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 04:58 AM

If you read aloud all the types of variabeles you have defined, you will find that some of them are messed up.

s for example is a global pointer to an int.
no idea why you would need a pointer to an int.
Was This Post Helpful? 0
  • +
  • -

#11 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 05:03 AM

View PostTrogdor, on 25 Oct, 2007 - 04:58 AM, said:

If you read aloud all the types of variabeles you have defined, you will find that some of them are messed up.

s for example is a global pointer to an int.
no idea why you would need a pointer to an int.

I am not getting what's wrong in the variable definitions and declarations....i am trying to improve my skills in pointers but till now i am not able to find the solution of it...so tel me what is wrong with this coding and what can be done to remove the given errors....thanks
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 05:32 AM

  int size;
  s= new int[size];
  char str[s];


You have declared a variable named size, but it has not been initialized, so it does not hold a valid value.

You then try to declare an array using that uninitialized variable as the size.

You are then trying to do that again.

how will size be defined? who will decide it's value?
Was This Post Helpful? 0
  • +
  • -

#13 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 25 October 2007 - 10:00 PM

View PostAmadeus, on 25 Oct, 2007 - 05:32 AM, said:

  int size;
  s= new int[size];
  char str[s];


You have declared a variable named size, but it has not been initialized, so it does not hold a valid value.

You then try to declare an array using that uninitialized variable as the size.

You are then trying to do that again.

how will size be defined? who will decide it's value?


Now i am doing it this way and it's working also.....Just tel me if it is the write way to do the coding or not...

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
int main () 
{
  int i;
  int *s;
  float perc;
  int appearances = 1;
  int size=1000;
  s= new int[size];
  cout<<"Enter the string: ";
  char str[1000];
  cin.getline(str,size);
  int a=strlen(str);
  for(i=0;str[i]!='\0';i++)
  {
	if(str[i]=='t')
	{
	  if(str[i+1]=='h')
	  {
		if(str[i+2]=='e')
		{
			perc=(100*i)/(a);
			cout<<appearances<<" 'the' found when "<<perc<<" % of the file is read"<<endl;
			appearances++;
		}
	  }
	}
  }
  cout<<"total of "<<appearances-1 <<" 'the' found"<<endl;
  return 0;
}   

Was This Post Helpful? 0
  • +
  • -

#14 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: dynamic memory allocation in strings

Posted 26 October 2007 - 01:52 AM

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
int main () 
{
  int i;
  int *s;
  float perc;
  int appearances = 1;
  int size=1000;
  s= new int[size];
  cout<<"Enter the string: ";
  char str[1000];
  cin.getline(str,size);
  int a=strlen(str);
  for(i=0;str[i]!='\0';i++)
  {
	if(str[i]=='t')
	{
	  if(str[i+1]=='h')
	  {
		if(str[i+2]=='e')
		{
			perc=(100*i)/(a);
			cout<<appearances<<" 'the' found when "<<perc<<" % of the file is read"<<endl;
			appearances++;
		}
	  }
	}
  }
  cout<<"total of "<<appearances-1 <<" 'the' found"<<endl;
  return 0;
}



this is working properly but do tell me if the coding done for new and pointers is correct or not....or if something else can be done.......
Was This Post Helpful? 0
  • +
  • -

#15 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: dynamic memory allocation in strings

Posted 26 October 2007 - 02:14 AM

Please stop creating new threads that deal with the same source code or project. Be patient and someone will respond to your previous thread.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3