0

I am given a task which is as follows: User enters a string of symbols which can be only < (less), > (greater) or = (equal) and string ends with semicolon. The task is to generate numbers between those symbols while the very first number is 1. An example: if user enters <>>>=<; the program should generate numbers according to those symbols, like this: 1<2>1>0>-1=-1<0. In my program everything is working...sort of. Sometimes if I enter 15 symbols, everything works just fine, but when I enter 20 symbols, my program crashes so my guess is I have issues with memory allocation, but not sure where... Any help would be appreciated! Here is my code:

Problem solved!
3
  • 2
    Big wall of Code with non-english names. Please take a look at MCVE, and remember this site is english. Commented Jun 6, 2014 at 20:19
  • Have You tried debugging? valgrind and GDB for starters. Also, asking for doing Your homework --- isn't the best suited question for StackOverflow. Commented Jun 6, 2014 at 20:20
  • I don't see any problem with the variables having non-English names. The problem is clearly stated. I also don't see this as a "do my homework" question - the poster has made a pretty clear and reasonably competent effort to do the work and is asking about a specific problem. Commented Jun 6, 2014 at 20:27

1 Answer 1

1

sizeof(simboliai) returns size of the pointer (usually 4) not the actual length of allocated array. You need to keep track of that separately. For example, in another variable.

Sign up to request clarification or add additional context in comments.

3 Comments

I am fairly new to memory allocation and sorry if I might seem stupid, but if I understand you correctly, I should instead of: simboliai=(char*)realloc(simboliai, sizeof(simboliai)+sizeof(char)); just use simboliai=(char*)realloc(simboliai, suma+sizeof(char));? Here suma calculates symbols in string
Yes. You can also type just simboliai=realloc(simboliai, suma+1);. That cast and sizeof are redundant.
Well, thank you good sir! Tested it out a couple of times with tons of symbols and it works perfectly so far. I guess my problem is solved! :thumbup:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.