corrupted stack?

This is a discussion on corrupted stack? within the C Programming forums, part of the General Programming Boards category; Hi I'm a complete C newbie. I do have some experience in Java, though. Anyway, I've been working on a ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    corrupted stack?

    Hi

    I'm a complete C newbie. I do have some experience in Java, though. Anyway, I've been working on a small project in C (mostly for leaning purposes), and it compiles, but when I run it I get an error message. I've been reading around online, but haven't found anything that works in fixing it. Here's the error message and the offending code:




    Code:
    C:\c_programs\test2>up2.exe
          7 [main] up2 1280 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIO
    LATION
        831 [main] up2 1280 open_stackdumpfile: Dumping stack trace to up2.exe.stack
    dump
     876694 [main] up2 1280 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIO
    LATION
     898744 [main] up2 1280 _cygtls::handle_exceptions: Error while dumping state (p
    robably corrupted stack)


    Code:
    //up2.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    
    struct Data{
    	int currentAmount;
    	int isactive;
    };
    
    
    struct Data* data;
    static char buffer[5000];
    
    
    void updateCurrentAmount(void){
    	static char lookFor[] = "amount=";  //this string immediatly preceads the amount
    	char* startOfExpression = strstr(buffer, lookFor);
    	
    	if (startOfExpression == NULL){  //we have problems
    		printf("ERROR - Error parsing charpane.php");
    		exit(-1);
    	}	
    	
    	char* startOfNumber = startOfExpression + strlen(lookFor);
    	data->currentAmount = atoi(strncpy("", startOfNumber, 5));
    }
    
    
    
    //tests updateCurrentLevel function
    int main(){
    	strcpy(buffer, "name=fred amount=12345 isactive=1");
    	data = (struct Data*) malloc(sizeof(struct Data));
    	
    	
    	updateCurrentAmount();
    	
    	printf("current level = %i", data->currentAmount);
    	return 0;
    }
    Any help you can give me would be much appriciated. Thank you.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    strncpy("", startOfNumber, 5)
    "" should instead be a buffer large enough to hold startOfNumber (5).

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    thanks

    Thanks for the help. Such a simple little mistake but I never would have found it.

    Thanks again.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > char* startOfNumber = startOfExpression + strlen(lookFor);
    Standard C doesn't allow mixed declarations and statements.
    You must be using
    - C99 (the new C standard)
    - Some compiler speciifc extension
    - C++

    > data = (struct Data*) malloc(sizeof(struct Data));
    You don't need to cast the result of malloc in C, you already included stdlib.h which is all you need.
    If you still need the cast, it probably means you're using a C++ compiler to compile your C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21